For the code below, get rid of all the while loops and instead use recursion, no
ID: 3808521 • Letter: F
Question
For the code below, get rid of all the while loops and instead use recursion, no loops.
import java.io.Console;
import java.util.Scanner;
public class Homework5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number from 1-5: ");
int number = sc.nextInt();
// input validation
while(number<1 || number > 5){
System.out.print("Enter a number from 1-5: ");
number = sc.nextInt();
}
while(number > 0){
number = number - 1;
System.out.println("Enter list of integers (0 to stop): ");
System.out.print("Enter input: ");
int input = sc.nextInt();
if(input == 0){
System.out.println("ERROR: At least one number should be greater than 0");
}else
{
int min = input;
int max = input;
while(true){
System.out.print("Enter input: ");
input = sc.nextInt();
if(input == 0)
break;
if(input > max)
max = input;
if(input < min)
min = input;
}
System.out.println("Max: "+max);
System.out.println("Min: "+min);
System.out.println();
}
}
}
}
1. Prompt for and read a number between 1 and 5. Repeat this step until the input is 1. 5. 2. Repeat the following multiple times according to the number read in step 1. a. Read in a list of integers en with a 0. The 0 marks the end of the input and is not ding considered part of the list b. Print the largest and smallest integers in the list c. If only a zero appears in the list, print an error message Here is a pseudo-code solution. begin repeat prompt user to enter a number from 1 to 5 read number until number is from 1 to 5 repeat number times decrement number read input if input print error message else set min to input set max to input read input repeat while input is not-0 if input min then set min to input else if input max then set max to input end if end if read input end repeat print max print min end if end repeat end For this assignment, you are to convert the algorithm into a non-iterative solution (no loops) and code it in Java following the coding style requirements posted for the class. You may design your solution toExplanation / Answer
package myProject;
import java.io.Console;
import java.util.Scanner;
//Class Definition
public class Homework5
{
int max, min;
//Scanner class object created
Scanner sc = new Scanner(System.in);
//Method to accept a number between 1 and 5 using recursion
Integer validNumber(Integer n)
{
//Checks whether the number is less than 1 or greater than 5 then asks to reenter
if(n < 1 || n > 5)
{
System.out.print("Enter a number from 1-5: ");
n = sc.nextInt();
validNumber(n);
}//End of if
//Returns the number
return n;
}//End of method
//Method to accept numbers and display max and min till zero entered by the user using recursion
void acceptNumber()
{
//Accepts a number
System.out.print("Enter input: ");
Integer input = sc.nextInt();
//Checks if the number is not zero
if(input != 0)
{
//Checks number is greater than the max than update the max by the current number
if(input > max)
max = input;
//Checks number is less than the min than update the min by the current number
if(input < min)
min = input;
//Recursion method call till 0 is entered
acceptNumber();
}//End of if
//Otherwise if number is zero
else
{
//Display the maximum and minimum number
System.out.println("Max: "+max);
System.out.println("Min: "+min);
System.out.println();
}//End of else
return;
}//End of method
//Method to repeat till number of times entered by the user between 1 and 5
void maxMin(Integer number)
{
//Checks if the number is zero
if(number > 0)
{
//Accepts a number
System.out.println("Enter list of integers (0 to stop): ");
System.out.print("Enter input: ");
int input = sc.nextInt();
//Checks the first number should not be zero
if(input == 0)
{
System.out.println("ERROR: At least one number should be greater than 0");
}//End of if
//Otherwise
else
{
//Set the min and max to the current input number
min = input;
max = input;
//Calls the acceptNumber() method to accept number till zero entered.
acceptNumber();
//Recursive function call till number becomes zero
maxMin(number-1);
}//End of else
}//End of if
}//End of method
//Main method
public static void main(String[] args)
{
//Class object created
Homework5 mw = new Homework5();
Scanner sc = new Scanner(System.in);
//Creates an integer class object
Integer number = new Integer(0);
System.out.print("Enter a number from 1-5: ");
number = sc.nextInt();
number = mw.validNumber(number);
System.out.println("Recent number = " + number);
mw.maxMin(number);
}//End of method
}//End of class
Output:
Enter a number from 1-5: 2
Recent number = 2
Enter list of integers (0 to stop):
Enter input: 45
Enter input: 11
Enter input: 3
Enter input: 0
Max: 45
Min: 3
Enter list of integers (0 to stop):
Enter input: 55
Enter input: 22
Enter input: 11
Enter input: 6
Enter input: 0
Max: 55
Min: 6