Java Program: Write a method that prompts for an input value and call it promptF
ID: 3909698 • Letter: J
Question
Java Program:
Write a method that prompts for an input value and call it promptForInput.
The method takes no arguments and returns valid input, i.e., an integer between 0 and 30.
The method should be called 5 times from the main method. Each time the method is called, it is used to set one of the counts.
That is, the logic of a while loop and corresponding condition statement from the code below
___________________________________________________________________________________________________
import java.util.Scanner;
public class CourseEvalLoopsAdded
{
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
int error1 = 1;
int error2 = 1;
int error3 = 1;
int error4 = 1;
int error5 = 1;
int input1 = 1;
int input2 = 1;
int input3 = 1;
int input4 = 1;
int input5 = 1;
int eval1 = 1;
int eval2 = 1;
int eval3 = 1;
int eval4 = 1;
int eval5 = 1;
//1 as a rating is Very Good
while (error1 == 1)
{
System.out.print ("Please enter the total number of students who entered 1:");
input1 = sc.nextInt();
if (input1 >= 0 && input1 <= 30 )
{
eval1 = input1 * 5;
error1 = -1;
}
}
//2 as a rating is Good
while (error2 == 1)
{
System.out.println ("Please enter the total number of students who entered 2:");
input2 = sc.nextInt();
if (input2 >= 0 && input2 <= 30 )
{
eval2 = input2 * 4;
error2 = -1;
}
}
// 3 is Neutral
while (error3 == 1)
{
System.out.println("Please enter the total number of students who entered 3:");
input3 = sc.nextInt();
if (input3 >= 0 && input3 <= 30 )
{
eval3 = input3 * 3;
error3 = -1;
}
}
// 4 is Poor
while (error4 == 1)
{
System.out.println("Please enter the total number of students who entered 4:");
input4 = sc.nextInt();
if (input4 >= 0 && input4 <= 30 )
{
eval4 = input4 * 2;
error4 = -1;
}
}
// 5 is Very Poor
while (error5 == 1)
{
System.out.println("Please enter the total number of students who entered 5:");
input5 = sc.nextInt();
if (input5 >= 0 && input5 <= 30 )
{
eval5 = input5 * 1;
error5 = -1;
}
}
System.out.println("The total number of students who entered 1 is " + eval1);
System.out.println("The total number of students who entered 2 is " + eval2);
System.out.println("The total number of students who entered 3 is " + eval3);
System.out.println("The total number of students who entered 4 is " + eval4);
System.out.println("The total number of students who entered 5 is " + eval5);
float total = input1 +input2 +input3 + input4 + input5;
System.out.println("The final evaluation is " +(eval1 + eval2 + eval3 + eval4 + eval5)/total);
}
}
Explanation / Answer
The modified program with modifications that are in BOLD is given below
import java.util.Scanner;
public class CourseEvalLoopsAdded
{
private static Scanner sc;
// promptForInput method prompts for an input
public static int promptForInput()
{
int inputStudent=30, valid=0;
sc = new Scanner(System.in);
//loop till a valid number(number between 0 and 30) is entered
while (valid==0)
{
System.out.print ("Please enter the Input:");
inputStudent = sc.nextInt();
if (inputStudent >= 0 && inputStudent <= 30 )
{
valid = 1;
}
else //Print the error message continue the loop until a valid number is entered
System.out.print ("Number should be between 0 and 30. Please enter a valid number ");
}
return inputStudent; // return the valid number entered from input
}
public static void main(String[] args)
{
int input1,input2,input3,input4,input5;
int eval1 = 1;
int eval2 = 1;
int eval3 = 1;
int eval4 = 1;
int eval5 = 1;
//1 as a rating is Very Good
input1 = promptForInput(); //Function promptForInput is called to get the count of students entered 1
System.out.println ("Total number of students who entered 1:"+input1);
eval1 = input1 * 5;
//2 as a rating is Good
input2 = promptForInput(); //Function promptForInput is called to get the count of students entered 2
System.out.println ("Total number of students who entered 2:"+input2);
eval2 = input2 * 4;
// 3 is Neutral
input3 = promptForInput(); //Function promptForInput is called to get the count of students entered 3
System.out.println ("Total number of students who entered 3:"+input3);
eval3 = input3 * 3;
// 4 is Poor
input4 = promptForInput(); //Function promptForInput is called to get the count of students entered 4
System.out.println ("Total number of students who entered 4:"+input4);
eval4 = input4 * 2;
// 5 is Very Poor
input5 = promptForInput(); //Function promptForInput is called to get the count of students entered 5
System.out.println ("Total number of students who entered 5:"+input5);
eval5 = input5 * 1;
System.out.println("The total number of students who entered 1 is " + eval1);
System.out.println("The total number of students who entered 2 is " + eval2);
System.out.println("The total number of students who entered 3 is " + eval3);
System.out.println("The total number of students who entered 4 is " + eval4);
System.out.println("The total number of students who entered 5 is " + eval5);
float total = input1 +input2 +input3 + input4 + input5;
System.out.println("The final evaluation is " +(eval1 + eval2 + eval3 + eval4 + eval5)/total);
}
}
Output:
Please enter the Input:50
Number should be between 0 and 30. Please enter a valid number
Please enter the Input:2
Total number of students who entered 1:2
Please enter the Input:3
Total number of students who entered 2:3
Please enter the Input:5
Total number of students who entered 3:5
Please enter the Input:4
Total number of students who entered 4:4
Please enter the Input:2
Total number of students who entered 5:2
The total number of students who entered 1 is 10
The total number of students who entered 2 is 12
The total number of students who entered 3 is 15
The total number of students who entered 4 is 8
The total number of students who entered 5 is 2
The final evaluation is 2.9375
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.