In: Computer Science
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.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
NOTE I HAVE BOTH THE TXT FILE AND JAVA FILE IN THE SAME FOLDE, INCASE IF YOU DON'T HAVE THE FILE IT IS AUTOMATICALLY CREATED. IF YOU WANT TO WRITE THE DATA INTO ANOTHER FILE IN DIFFERENT DIRECTORY, YOU CAN GIVE THE FILE PATH BY REMOVING / WITH \\ FROM THE FILE PATH AS D:\\PATH\\output.txt
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CODE TO COPY
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class evenOrOdd {
static void introduction() {
System.out.println(
"Introduction:\nYou can enter a number greater
than 0 to calculate the sum of first n even numbers for
input\n"
+ "of even
number or calculate sum of first n odd number incase of odd
\n"
+ "number
input you can EXIT AT ANY TIME BY ENTERING a NON NUMERIC
CHARACTER");
}
public static void main(String[] args) throws
IOException {
introduction();
Scanner sc = new
Scanner(System.in);
int n = 0;
FileWriter fw = new FileWriter(new
File("Output.txt"));
while (true) {
try {
n = sc.nextInt();
} catch
(InputMismatchException e) {
System.out.println("Exiting...");
break;
}
System.out.println("the original integer is " + n);
fw.write("the
original integer is " + n + "\n");
boolean even =
isiteven(n);
if (even)
{
System.out.println(n + " is an even
number.");
fw.write(n + " is an even number." +
"\n");
int x = sumEvenSquares(n);
System.out.println("The sum of first " + n + "
even numbers is " + x);
fw.write("The sum of first " + n + " even
numbers is " + x + "\n");
} else if (n
< 0) {
System.out.println("Please enter a valid Input
which is greater than or equal to 0");
fw.write("Please enter a valid Input which is
greater than or equal to 0");
} else {
System.out.println(n + " is an odd
number.");
fw.write(n + " is an odd number." + "\n");
int x = sumOddNumbers(n);
System.out.println("The sum of first " + n + "
odd numbers is " + x);
fw.write("The sum of first " + n + " odd numbers
is " + x + "\n");
}
}
fw.close();
}
private static int sumOddNumbers(int n) {
int i = 1;
int sum = 0;
while (i <= n) {
sum += (2 * i -
1);
i++;
}
return sum;
}
private static int sumEvenSquares(int n) {
int i = 1;
int sum = 0;
while (i <= n) {
sum += (2 * i *
2 * i);
i++;
}
return sum;
}
private static boolean isiteven(int n) {
if (n % 2 == 0) {
return
true;
} else {
return
false;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EXPLANATION
import java.io.File; // FOR FILE OPERATIONS
import java.io.FileWriter; // FOR WRITING INTO FILE
import java.io.IOException; // FOR ANY INPUT EXCEPTION
import java.util.InputMismatchException; // FOR WRONG INPUT
import java.util.Scanner; // TO TAKE USER INPUT
public class evenOrOdd { // CREATING A CLASS NAMED EVENORODD
static void introduction() { //CREATING A METHOD NAMED INTRODUCTION
//PRINTING INFO ABOUT INTRODUTION PLEASE CHANGE THE DESCRIPTION
AND ADD YOUR NAME
System.out.println(
"Introduction:\nYou can enter a number greater
than 0 to calculate the sum of first n even numbers for
input\n"
+ "of even
number or calculate sum of first n odd number incase of odd
\n"
+ "number
input you can EXIT AT ANY TIME BY ENTERING a NON NUMERIC
CHARACTER");
}
public static void main(String[] args) throws
IOException { //CREATING A MAIN METHOD
introduction(); /// CALLING THE
METHOD
Scanner sc = new
Scanner(System.in); // CREATING A SCANNER OBJECT NAMED SC FOR
TAKING USER INPUT
int n = 0; // FOR STORING USER
INPUT IN N
FileWriter fw = new FileWriter(new
File("Output.txt")); // OPEN THE FILE USING FILE WRITER TO ENTER
THE DETAILS FROM USER
while (true) { // UNTIL USER ENTERS
AN INVALID CHARACTER
try {
n = sc.nextInt(); // TAKE USER INPUT
} catch
(InputMismatchException e) {
System.out.println("Exiting..."); // IF INVALID
CHARACTER IS ENTERED SHOW THIS
break; // STOP THE LOOP
}
System.out.println("the original integer is " + n); // PRINTING THE
NUMBER
fw.write("the
original integer is " + n + "\n"); //WRITING INTO THE FILE
boolean even =
isiteven(n); // CHECKING IF THE NUMBER IS EVEN
if (even) { //
IF THE NUMBER IS EVEN
System.out.println(n + " is an even number.");
// PRINT THIS
fw.write(n + " is an even number." + "\n"); //
WRITE THIS INTO THE FILE
int x = sumEvenSquares(n); // CALCULATE THE
SQUARE OF EVEN NUMBER
System.out.println("The sum of first " + n + "
even numbers is " + x); // PRINT THIS
fw.write("The sum of first " + n + " even
numbers is " + x + "\n"); // WRITE THIS INTO THE FILE
} else if (n
< 0) { // IF THE NUMBER IS LESS THAN 0
System.out.println("Please enter a valid Input
which is greater than or equal to 0"); // SHOW THIS
fw.write("Please enter a valid Input which is
greater than or equal to 0"); // WRITE THIS INTO THE FILE
} else {
System.out.println(n + " is an odd number."); //
IF THE NUMBER IS ODD AND GREATER THAN 0
fw.write(n + " is an odd number." + "\n"); //
WRITE THIS INTO THE FILE
int x = sumOddNumbers(n); // CALCULATE THE SUM
OF ODD NUMBERS
System.out.println("The sum of first " + n + "
odd numbers is " + x); // PRINT THIS STATEMENT
fw.write("The sum of first " + n + " odd numbers
is " + x + "\n"); // WRITE THIS INTO THE FILE
}
}
fw.close(); // CLOSE THE FILE
}
private static int sumOddNumbers(int n) { //
CREATING A METHOD SUM ODD NUMBER
int i = 1; //SET I AS 1
int sum = 0; // SET SUM AS 0
while (i <= n) { // COUNT UNTIL
N
sum += (2 * i -
1); // ADD THIS SUM
i++; // INCREASE
I
}
return sum; //RETURN THE SUM
}
private static int sumEvenSquares(int n) {
//CREATING A METHOD NAMES SUM EVEN SQAURES
int i = 1; //SET I AS 1
int sum = 0; // SET SUM AS 0
while (i <= n) { // INFINTE LOOP
UNTIL USER NUMBER N
sum += (2 * i *
2 * i); // CALCULATE THE SUM
i++; //INCREASE
I
}
return sum; //RETURN THE SUM
}
private static boolean isiteven(int n) { //TO CHECK
IF THE NUMBER IS EVEN OR ODD
if (n % 2 == 0) { // IF THE
REMAINDER IS 0 WHEN DIVIDED BY 2
return true;
//RETURN TRUE
} else {
return false;
//ELSE RETURN FALSE
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////