Question

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

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

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

`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));
  
}

}
}
}

M Inbox - gurkaranpreet.singh.m x C Write A Complete Java Prograr X </> Online Java Compiler - Online x C My Q&A | Chegg.com

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a complete Java program, including comments in both the main program and in each method,...
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
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