Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok so I need a Java program that does the following: It needs to ask the user to

ID: 3621159 • Letter: O

Question

Ok so I need a Java program that does the following:
It needs to ask the user to enter an integer and store it in variable numIter:
Please enter number of iterations (integer larger than 0):
If the numIter that the user enters is less then 1 the program exits and displays "Please rerun the program entering an integer larger than 0."
If the number is atleast 1 then the program should repeat (loop) the following as many times as the numIter.
THIS IS THE LOOP
Program should ask the user to enter two integers as follows, and read the numbers entered by the user into variables choice and x:
"Please enter a choice (1 or 2) followed by a positive integer for x:"
If choice is neither 1 or 2, the program should display the following message (counts as one time through the loop)
"Number is an invalid choice. Please enter a choice (1 or 2)
followed by a positive integer for x:"
If the number entered for x is non-positive, the program shall produce the following (counts as one time through the loop)
"Number is less than 1. Please enter a choice (1 or 2) followed by
a positive integer for x:"
The program should write the following in an output file named “outfile.txt”:
If choice is 1, the program shall calculate and print the factorial of x = 1 * 2 * … *x.
If choice is 2, the program shall calculate and print the sum 1 + 2 + … + x.

The program should also handle exceptions.

Explanation / Answer

import java.util.*;
import java.io.*;

public class Loop
{
public static void main(String[] args) throws IOException
{
// open keyboard input
Scanner kb = new Scanner(System.in);

System.out.print("Please enter number of iterations (integer larger than 0): ");

int iterations = kb.nextInt();

if(iterations <= 0)
{
System.out.println("Please rerun the program entering an integer larger than 0.");
System.exit(1);
}

// open file input stream
PrintWriter fileout = new PrintWriter(new File("outfile.txt"));

for(int i = 0; i < iterations; i++)
{
System.out.print("Please enter a choice (1 or 2) followed by a positive integer for x: ");
int choice = kb.nextInt();
int x = kb.nextInt();

if(choice != 1 && choice != 2)
{
System.out.print("Number is an invalid choice. ");

}
else if(x <= 0)
{
System.out.print("Number is less than 1. ");
}
else
{
if(choice == 1)
{
// print factorial
int output = 1;

for(int j = 2; j <= x; j++)
{
output *= j;
}

// print output to file
fileout.println(output);
}
else if(choice == 2)
{
// print sum
int output = 0;

for(int j = 1; j <= x; j++)
{
output += j;
}

// print output to file
fileout.println(output);
}
}


}
// close output file
fileout.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote