Question

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

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] );

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    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...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of...

    (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of the array is you cannot add or delete elements from the array. The size is fixed and you can only do workarounds. An example is the following: public class Ex_7_5_Prep { public static void main(String[] args) { int[] intArray = { 5, 10, 15, 20 }; printArray(intArray); intArray = addNewElement(intArray, 25); printArray(intArray); } public static int[] addNewElement(int[] originalArray, int newInt) { // Create new...

  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • Arrays and Methods Worksheet 2 (10 pts) Copy the following into a new file called ArrayMethods2.java....

    Arrays and Methods Worksheet 2 (10 pts) Copy the following into a new file called ArrayMethods2.java. Write the methods one-by-one. Compile and run your code after you write each method to make sure it was written properly. When your methods pass all of the tests, upload your file to Canvas. /** * Assignment 20.2 * @author Your Name * CIS 36A */ public class ArrayMethods2 {     /**     * Given an array of ints, return the index of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT