Question

Create a class named Program10. Your program should have the following 4 methods (other helper methods...

  1. Create a class named Program10. Your program should have the following 4 methods (other helper methods are allowed, but these four methods must be implemented as specified).
  2. You need to define a global scanner object and only use it inside the main method and the getValues method, since other methods do not get any values from the input.
  3. getValues:
    • This method does not have any input parameter and returns an array of integers.
    • This method prompts the user with a message and gets a positive number. Method shows “Invalid inputs” message for negative numbers and zero, and askes the user to re-enter the number. This positive number is for how many integers needs to be received from the input.
    • This method uses that positive value to get integer values from the input and saves them inside an array.
    • This method returns an array which is filled up with integer values.
    • Finish this method, test it and then start other methods since you will be calling this method from other methods.
  4. alternatingSum:
    • This method does not have any input parameters.
    • This method gets an array of integers by calling geValues() method.
    • This method returns the alternating sum of all elements.
  5. reverse:
    • This method gets an array of integers by calling geValues() method.
    • This method returns an array with reversed elements.
    • All reversed elements need to be displayed inside the main method.

  1. Run:
    • This method does not have any input parameters.
    • This method generates 20 random dice tosses (no return value).
    • dice tosses with same number eg: 5 5 are in paranthesis (5 5)
    • 20 random dice toss pairs are printed using print statement

Output:

Inside the main method, create a menu and call all methods from the main method.

Optional: you can also create an extra method for the menu and call it inside the main method.

Submit Program10.java file on canvas.

>run Program10

PROGRAM#10

1- alternatingSum

2- reverse

3- Run

4- Exit

Enter a number[1-4]: -1

Invalid input!

Enter a number[1-4]: 5

Invalid input!

Enter a number[1-4]: 1

** alternatingSum **

How many integer values: 0

Invalid input!

How many integer values: -2

Invalid input!

How many integer values: 9

Enter the numbers: 1 4 9 16 9 7 4 9 11

The result is: 12

PROGRAM#10

1- alternatingSum

2- reverse

3- Run

4- Exit

Enter a number[1-4]: 2

** reverse **

How many integer values: 9

Enter the numbers: 1 4 9 16 9 7 4 9 11

The reverse array is: 11 9 4 7 9 16 9 4 1

PROGRAM#10

1- alternatingSum

2- reverse

3- Run

4- Exit

Enter a number[1-4]: 3

** run **

The sequence of numbers is:

1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package HomeworkLib1;
import java.util.Random;
import java.util.Scanner;
public class Program10 {
        static Scanner in;
        public static void printMenu() {
                System.out.println("PROGRAM#10");
                System.out.println("1 - Alternating Sum");
                System.out.println("2 - Reverse");
                System.out.println("3 - Run");
                System.out.println("4 - Exit");
        }
        public static void main(String[] args) {
                int i=0;
                int option;
                do {
                        in = new Scanner(System.in);
                        printMenu();
                        do {
                                System.out.print("Enter a number[1-4]: ");
                                option=in.nextInt();
                                if(!(option==1 || option==2|| option==3|| option==4))
                                        System.out.println("Invalid input!");
                                else
                                        break;
                        }while(true);
                        switch(option) {
                        case 1:
                                int x = alternatingSum();
                                System.out.println("The result is: " + x);
                                break;
                        case 2:
                                int[] y = reverse();
                                System.out.print("The reverse array is: ");
                                for(int m: y)
                                        System.out.print(m+" ");
                                System.out.println();
                                break;
                        case 3:
                                run();
                                break;
                        case 4:
                                System.exit(0);
                        }
                }while(true);
                
        }
        private static int[] getValues() {
                int i;
                do {
                        System.out.print("How many interger values?");
                        in = new Scanner(System.in);
                        i = in.nextInt();
                        if(i<=0) {
                                System.out.println("Invalid input!");
                                continue;
                        }
                        else
                                break;
                }while(true);
                int[] array = new int[i];
                System.out.print("Enter the numbers: ");
                for(int j=0; j<i; j++)
                        array[j]=in.nextInt();
                return array;
        }
        private static void run() {
                Random rand = new Random();
                int[] array = new int[20];
                for(int i=0; i<20; i++)
                        array[i] = rand.nextInt(6+1);
                
                for(int i: array)
                        System.out.print(i+" ");
                System.out.println();
                for(int i=0; i<20; i++) {
                                                //If there are two continuous 5's
                        if(i<19 && array[i]==5 && array[i+1]==5) {
                                System.out.print("(5 5) ");
                                i++;
                        }
                        else
                                System.out.print(array[i] + " ");
                }
                System.out.println();
        }
        private static int[] reverse() {
                int[] array = getValues();
                int j;
                int end = array.length-1;
                                //Reversing an element. Swap elements from star and end
                for(j=0 ;j<=end/2; j++) {
                        int temp = array[end-j];
                        array[end-j]=array[j];
                        array[j]=temp;
                }               
                return array;
        }
        private static int alternatingSum() {
                System.out.println("**alternatingSum");
                int[] array = getValues();
                int sum = 0;
                for(int i=0; i<array.length; i++) {
                        sum += array[i];
                        i++;                    //Skip next element
                }
                return sum;
        }

}

Add a comment
Know the answer?
Add Answer to:
Create a class named Program10. Your program should have the following 4 methods (other helper methods...
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 DO THE PSEUDOCODE FOR THE PROGRAM BELOW Program 3: Design (pseudocode) and implement (source code)...

    PLEASE DO THE PSEUDOCODE FOR THE PROGRAM BELOW Program 3: Design (pseudocode) and implement (source code) a program (name it DistinctValues) to display only district values in an array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method getValues() that takes an integer array and returns another single-dimensional array containing only distinct values in the original (passed) array. Document...

  • This program will take advantage of methods written in the Help.java class First, you will write...

    This program will take advantage of methods written in the Help.java class First, you will write a new method in Help.java. This method will be named: addArray) and will accept an array of integers as input. This method will calculate the sum of all values in the array and return the resulting sum. Remember that the array being passed to the method may be of ANY length. (Use the existing methods in the Help.java class to help you remember how...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • Create a class named HW that has the main method. Leave the main method empty for...

    Create a class named HW that has the main method. Leave the main method empty for now. • Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2,...

  • C++ Arrays & Methods

     #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be 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