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’).
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: You may send output to standard System.out. If you choose to 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
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
import java.util.Scanner;
public class Test{
public static boolean isiteven(int n)
{
if(n%2==0)
return true;
return false;
}
public static int sumEvenSquares(int n)
{
int s=0;
for(int i=1;i<=n;i++)
{
s=s+2*i*2*i;
}
return s;
}
public static int sumOddNumbers(int n)
{
int s=0;
for(int i=1;i<=n;i++)
{
s=s+(2*i-1);
}
return s;
}
public static void main(String []args){
Scanner sc=new Scanner(System.in);
while(true)
{ System.out.println("Enter an integer (negative to end): ");
int n=sc.nextInt();
if(n<0)
break;
System.out.println("the original integer is "+n);
if(isiteven(n))
{
System.out.println(n+" is an odd number");
System.out.printf("the sum of square of first %d even numbers is
%d\n",n,sumEvenSquares(n));
}
else
{
System.out.println(n+" is an even number");
System.out.printf("the sum of the first %d odd numbers is
%d\n",n,sumOddNumbers(n));
}
}
}
}
Kindly revert for any queries
Thanks.
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...
Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...
write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...
In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...
In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...
Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...
Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...
c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...
C++ Program Int Main First Please Write one program that does the following: 1. 1. Ask the user for ten (10) grades, and store the data in an array. Compute the average of all the grades. Print the original ten grades and the average. a. Declare an integer array with the name of “grades” of size 10 in the main function. b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array. i. The function will receive...
In Java* Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...