Question

In: Computer Science

Write a complete Java program, including comments in both the main program and in each method,...

Write a complete Java program, including comments in both the main program and in each method, which will do the following:

0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.

     This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the program does (it should also say how to end the set of data—see step 4).

1. Then the main program asks the user to type in an integer value which the main program calls n; n could be positive, negative, or zero (see step 4). The main program prints the number n after it is read in. Use a negative value to end.

2. The main program calls a method named isiteven, sending it the integer value n as a parameter. The method determines whether or not n is even, sending the answer back to main. (Hint: if n is even, the remainder when n is divided by 2 is 0; this works for all even numbers, including 0.) You may return an integer (like 0 or 1) to represent even or odd, or return a character (like ‘e’ or ‘o’), or a boolean.

The main program prints the answer returned together with a message. For example, if you send 6, the method determines it is even, and the main program prints that 6 is an even number.

3a. If n is even, the main program calls a method named sumEvenSquares, sending n to the method as a parameter. The method computes the sum of the first n even squares (see below). The method returns this sum to the main program. Then the main program prints a message giving n and the sum of the first n even squares.

For example, if you send 4, then the sum of the first 4 even squares is 2*2 + 4*4 + 6*6 + 8*8 = 4+16+36+64 = 120.

Note the method computes the sum of the first n even squares- this will be: 2*2 + 4*4 + 6*6 + ... + (2n)*(2n), where n is the parameter value sent to the method. DO NOT use any other formula for calculating this value; use the sum of series.

b. However, if n is odd, the main program calls a method named sumOddNumbers, sending it n. The method computes the sum of the first n odd numbers (NOT n odd squares - see below). The method returns this sum to the main program. Then the main program prints a message giving n and the sum of the first n odd numbers.

For example, if you send 3, then the sum of the first 3 odd numbers is 1 + 3 + 5 = 9. For n=5, it is 1+3+5+7+9 = 25, etc. (Although this value will be equal to n squared, calculate it as a series; don’t just find n squared.)


Note the method computes the sum of the first n odd numbers- this will be: 1 + 3 + 5 + ... + (2n-1), where n is the parameter value sent to the method.

In either case, the main program calls one of two methods to compute a sum of terms, then prints the method's answer.

4. After calling a method to compute the appropriate sum and printing the result, the main program will skip a few lines and go back to step 1 (not step 0).   At step 1, if the user types in a negative value, the program will go to step 5.

5. At the end, print how many data values were entered and processed. (Make sure that this number is at least 8.)

DATA: Type in a total of at least 8 data values. Have at least four even numbers (make sure 0 is one of the even numbers) and four odd numbers; intersperse the values: odd, then 2 evens, then odd, etc. Have one value of each type that is between 10 and 20 (and the rest smaller).

     You will be judged on the quality of your data.

STYLE: Be sure that each method has a good comment explaining two things: exactly what parameter(s) the method will receive, and exactly what the method will do (and if it returns an answer or prints). Mention parameters by name in the comment.

OUTPUT: Send output to an external file. You will need to pass it as a parameter.

Here is some sample output (ignoring the introduction):

the original integer is 5

5 is an odd number

the sum of the first 5 odd numbers is 25

the original integer is 4

4 is an even number

the sum of the first 4 even numbers is 120

OPTIONAL: If the user enters a negative value, say this is no good, ask the user to type in a new value that is good (greater than or equal to 0). The program continues until the user enters a special value (not just any negative value) that signals the end.

Solutions

Expert Solution


import java.util.Scanner;
public class Main{
        // this method prints some
        // introductory message
        public void introduction(){
                System.out.println("\nWelcome to Program");
                System.out.println("My name is XXX YYY");
                System.out.print("This method takes an integer from the user."); 
                System.out.print("If the input is even it prints the square sum of first n even numbers. ");
                System.out.println("If the input is odd it prints the sum of first n odd numbers.\n");
                
        }
        // this function checks if a number n is 
        // even or odd. 
        // If even then return true
        // else return false
        public boolean isiteven(int n){
                if(n%2==1)
                        return false;
                return true;
        }
        // this methods returns  sum of 
        // first n odd integers
        public int sumOddNumbers(int n){
                int sum=0, i;
                // run from 1 to 2*n with 
                // increment value=2
                for(i=1; i<=2*n;i+=2)
                        sum+=i;
                // return result
                return sum;
        }
        
        // this methods returns square sum of 
        // first n even integer
        public int sumEvenSquares(int n){
                int sum=0, i;
                // run from 2 to 2*n with 
                // increment value=2
                for(i=2; i<=2*n;i+=2)
                        sum+=i*i;
                // return result
                return sum;
        }
        
        public static void main(String[] args){
                // declare variables
                int n, result,data_count=0, process_count=0;
                
                // declare scenner class object
                Scanner sc=new Scanner(System.in);
                
                //declare object of class
                Main obj=new Main();
                
                //call inroduction()
                // no parameter is passed
                // no return value
                obj.introduction();
                
                while(true){
                        //take input of n
                        System.out.print("Enter a integer: ");
                        n=sc.nextInt();
                        data_count++;
                        System.out.println("The original integer is: "+n);
                        // if negative then break the loop
                        if(n<0)
                                break;
                        // call isiteven()
                        // input value n is passed
                        // if n is even it returns true
                        // else it will return false
                        boolean k=obj.isiteven(n);
                        if(k){
                                System.out.println(n+" is an even number");
                                // call sumEvenSquares()
                                // input value n is passed
                                // it will return square sum of first n 
                                // even numbers
                                result=obj.sumEvenSquares(n);
                                System.out.println("The square sum of first "+n+" even integers is: "+result);

                        }
                        else{
                                System.out.println(n+" is an odd number");
                                // call sumOddNumbers()
                                // input value n is passed
                                // it will return sum of first n 
                                // odd numbers
                                result=obj.sumOddNumbers(n);
                                System.out.println("The sum of first "+n+" odd integers is: "+result);
                        }
                        process_count++;
                }
                
                System.out.println("Total data value entered: "+data_count);
                System.out.println("Total data processed: "+process_count);
        }
}

___________________________________________________________________

Welcome to Program
My name is XXX YYY
This method takes an integer from the user.If the input is even it prints the sq
uare sum of first n even numbers. If the input is odd it prints the sum of first
 n odd numbers.

Enter a integer: 11
The original integer is: 11
11 is an odd number
The sum of first 11 odd integers is: 121
Enter a integer: 12
The original integer is: 12
12 is an even number
The square sum of first 12 even integers is: 2600
Enter a integer: 5
The original integer is: 5
5 is an odd number
The sum of first 5 odd integers is: 25
Enter a integer: 2
The original integer is: 2
2 is an even number
The square sum of first 2 even integers is: 20
Enter a integer: 7
The original integer is: 7
7 is an odd number
The sum of first 7 odd integers is: 49
Enter a integer: 5
The original integer is: 5
5 is an odd number
The sum of first 5 odd integers is: 25
Enter a integer: 8
The original integer is: 8
8 is an even number
The square sum of first 8 even integers is: 816
Enter a integer: 4
The original integer is: 4
4 is an even number
The square sum of first 4 even integers is: 120
Enter a integer: -1
The original integer is: -1
Total data value entered: 9
Total data processed: 8

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
Please write a complete C coding program (NOT C++) that has the following: (including comments) -...
Please write a complete C coding program (NOT C++) that has the following: (including comments) - declares two local integers x and y - defines a global structure containing two pointers (xptr, yptr) and an integer (z) - declares a variable (mst) by the type of previous structure - requests the values of x and y from the user using only one scanf statement - sets the first pointer in the struct to point to x - sets the second...
Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
Write a complete Java program, including import statements, variable declarations and initializations as needed, for the...
Write a complete Java program, including import statements, variable declarations and initializations as needed, for the following problem. DO NOT USE ARRAYS. The program will read from the keyboard a series of records, each record containing a city ID number (integer), morning temperature (integer) and evening temperature (integer). See below for sample input. Sample input: 123 72 79 157 80 100 103 76 56 9999 For each city ID read in, compute the average of the two temperatures. Print a...
In Java: Write a complete main method that does the following: 1. Takes any number, but...
In Java: Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT