1 answer

Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

Question:

Please complete the following programming with clear explanations. Thanks!

Homework 1 – Programming with Java:

What This Assignment Is About?

Classes (methods and attributes) •

Objects

Arrays of Primitive Values

Arrays of Objects

Recursion

for and if Statements

Selection Sort   

Use the following Guidelines:
Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.)

Use upper case for constants. • Use title case (first letter is upper case) for classes.

Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).

Use tabs or spaces to indent code within blocks (code surrounded by braces).

This includes classes, methods, and code associated with if, switch and loop statements. Be consistent with the number of spaces or tabs that you use to indent.

Use white space to make your program more readable.

For each file (class) in your assignment, provide a heading (in comments) which includes:
A description of what this program is doing.


3. Part 1. Primitive Types, Searching, Recursion

a) Create a class Homework (in a file Homework.java)

b) Create a static method initializeArray that receives as a parameter an array of characters. Use a for loop and an if statement to put ‘b’ in the odd positions of the array and ‘a’ in the even positions.

c) Create a static method printArray that receives as a parameter an array of
characters. Use a for statements to print all the elements in the array.

d) Create a static method selectionSort that receives as a parameter an array of characters and order its elements in ascending order. Implement Selection Sort algorithm. It should be Selection Sort, not Bubble Sort, not Quick Sort, etc. If you do not remember selection sort, this link could be useful: https://www.geeksforgeeks.org/java-program-forselection-sort/

e) Create a static recursive method named factorial that calculate and returns the factorial of a number. The method receives a number (integer number) as parameter.

f) Copy the following main method in your class, again the main method,

public class Homework {

public static void main (String [] arg) {

char [] a = {'a', 'b', 'c', 'd', 'x', 'y', '1', '2', '3', '4'};

char [] b = {'p', 'q', '9', '8' ,'7', '6'};

int [] c = {6, 0, 1};

// Testing initializeArray

printArray(a);

initializeArray(a);

printArray(a);

// Testing selectionSort

printArray(b);

selectionSort (b);

printArray(b);

// Testing factorial

System.out.println ( factorial (5) );

System.out.println ( factorial (c[0] );

System.out.println ( factorial (c[2] );

}

}


Answers

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Homework.java

public class Homework {

   public static void main(String[] arg) {

       char[] a = { 'a', 'b', 'c', 'd', 'x', 'y', '1', '2', '3', '4' };

       char[] b = { 'p', 'q', '9', '8', '7', '6' };

       int[] c = { 6, 0, 1 };

       // Testing initializeArray

       printArray(a);

       initializeArray(a);

       printArray(a);

       // Testing selectionSort

       printArray(b);

       selectionSort(b);

       printArray(b);

       // Testing factorial

       System.out.println(factorial(5));

       System.out.println(factorial(c[0]));

       System.out.println(factorial(c[2]));

   }

   private static int factorial(int num) {
       int res = 0;

       if (num == 0) {
           res = 1;
       } else if (num == 1) {
           res = 1;
       } else if (num > 1) {

           if (num - 1 == 1) {

           }

           res = num * factorial(num - 1);
       }
       return res;

   }

   private static void selectionSort(char[] b) {
       int minIndex;
       char temp;
       int SIZE = b.length;
       for (int i = 0; i < SIZE - 1; i++) {
           minIndex = i;
           for (int j = i + 1; j < SIZE; j++) {
               if (b[j] < b[minIndex])
                   minIndex = j;
           }
           // swap array
           if (minIndex != i) {
               temp = b[i];
               b[i] = b[minIndex];
               b[minIndex] = temp;
           }
       }

   }

   private static void initializeArray(char[] a) {
       for (int i = 0; i < a.length; i++) {
           if ((i + 1) % 2 == 0) {
               a[i] = 'b';
           } else {
               a[i] = 'a';
           }
       }

   }

   private static void printArray(char[] a) {
       for (int i = 0; i < a.length; i++) {
           System.out.print(a[i] + " ");
       }
       System.out.println();

   }

}

============================

output;

a b c d x y 1 2 3 4
a b a b a b a b a b
p q 9 8 7 6
6 7 8 9 p q
120
720
1

=========================Thank You

.

Similar Solved Questions

1 answer
Can you do problem (c)? Suppose that blood chloride concentration (mmol/L) has a normal distribution with...
can you do problem (c)? Suppose that blood chloride concentration (mmol/L) has a normal distribution with mean 105 and standard deviation 5 What is the probability that chloride concentration equals 106? Is less than 106? Is at most 106? (Round your answers to four decimal places.) equals 106 less t...
1 answer
What is the zero to peak amplitude in (N/C) of an electric field if there is...
What is the zero to peak amplitude in (N/C) of an electric field if there is an electromagnetic wave in free space with a time average intensity of 60 watts/meter squared?...
1 answer
Please show cash flows, NPV, IRR, and payback period Sneaker 2013 The business case team had...
Please show cash flows, NPV, IRR, and payback period Sneaker 2013 The business case team had compiled the following baseline information surrounding the Sneaker 2013 project: 1. The life of the Sneaker 2013 project was expected to be six years. Assume the analysis took place at the end of 2012. 2. ...
1 answer
Consider the following sample data drawn independently from normally distributed populations with equal population variances. Use...
Consider the following sample data drawn independently from normally distributed populations with equal population variances. Use Table 2. Sample 1 12.7 11.7 7.8 11.6 10.8 10.4 94 10.7 Sample 2 8.7 10.8 13.5 11.8 11.5 95 10.8 11.8 Click here for the Excel Data File a. Construct the relevant hypothes...
1 answer
Mark up your resume so that the different sections of the resume are styled using the...
Mark up your resume so that the different sections of the resume are styled using the box attributes we discussed in class on 4/4/2018. For example, create different box styles, colors, thicknesses, margins, padding, alignments, etc. Make sure each section is style differently so that you have exper...
1 answer
How fast would something have rotate in order to create earth like gravity from centrifugal force?
How fast would something have rotate in order to create earth like gravity from centrifugal force?...
1 answer
Using the following 3 securities calculate: 1. Expected return 2. Variance 3. Standard deviation 4. Correlation...
Using the following 3 securities calculate: 1. Expected return 2. Variance 3. Standard deviation 4. Correlation between all possible pairs 5. Covariance between all possible pairs Probability Stock A .10 .10 .30 .20 .30 5% 5% 12% 6% 18% Stock B 35% 31% 30% 25% 17% Stock C 2% 6% 10% 15% 20%...
1 answer
Problem 14.60 Review1 Constants 1 Periodic Table Part As we've seen, astronauts measure their mass by...
Problem 14.60 Review1 Constants 1 Periodic Table Part As we've seen, astronauts measure their mass by measuring the period of oscilation when siting in a chair connected to a spring The Body Mass Measurement Device on Skylab, a 1970s space station, had a spring constant of 606 N/m. The empty cha...
1 answer
(3) Suppose that a stock is trading at 50 and you want to buy it when...
(3) Suppose that a stock is trading at 50 and you want to buy it when the price becomes 55, what order type should you submit? And at what price level (or range)?...
1 answer
Unions in developed nations often oppose imports from low-wage countries and advocate trade barriers to protect...
Unions in developed nations often oppose imports from low-wage countries and advocate trade barriers to protect jobs from what they often characterize as “unfair” import competition. Is such competition “unfair”? Do you think that this argument is in the best interests of (a)...
1 answer
2a², where [Fixed Point Iterations, 15 pts). Let g(2) = -22 + 3x + a a...
2a², where [Fixed Point Iterations, 15 pts). Let g(2) = -22 + 3x + a a is a parameter. (a) Show that a is a fixed point of g(x). (b) For what values of a does the iteration scheme On+1 = g(n) converge linearly to the fixed point a (provided zo is chosen sufficiently close to a)? (c) Is there a ...
1 answer
Let and be two finite measures on . Prove that if and only if the condition...
Let and be two finite measures on . Prove that if and only if the condition implies   , for each . Thank you for your explanations. We were unable to transcribe this imageWe were unable to transcribe this image(N, P (N)) μ<<φ 6({n})=0 ({n}) = 0 neN...
1 answer
Homework: Chapter 6 Homework Score: 0 of 1 pt 8 of 8 (0 complete) Problem 6-24...
Homework: Chapter 6 Homework Score: 0 of 1 pt 8 of 8 (0 complete) Problem 6-24 (similar to) (Portfolio beta) Assume you have the following portfolio Ο STOCK STOCK WEIGHT ΒΕΤΑΙ Apple 35% 1.59 Green Mountain Coffee 15% 1.41 Disney 28% 111 Target 22% 1.22 (Click on t...
1 answer
Pureform, Inc., uses the FIFO method in its process costing system. It manufactures a product that...
Pureform, Inc., uses the FIFO method in its process costing system. It manufactures a product that passes through two departments. Data for a recent month for the first department follow: Materials $ 84,400 Labor $ 42,200 Overhead $ 55,900 Work in process inventory, beginning Units started in proces...
1 answer
The pressure at sea level is generally considered to be 1 atm. Pressure increases the deeper...
The pressure at sea level is generally considered to be 1 atm. Pressure increases the deeper you dive in the ocean by roughly 1 atm every 10 meters. At a depth of 2, what is this pressure in PSI?...