Need help with Question 3. I was able to dp question 2. which is import java.uti
ID: 3915961 • Letter: N
Question
Need help with Question 3. I was able to dp question 2. which is
import java.util.Scanner;
public class Exam4B_02{
public static void incrementProblem(int number)
{
number++;
System.out.println("Increment is "+number);
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int n;
System.out.println("Enter Integer (-1 to exit) ? ");
n=scan.nextInt();
while(n!=-1)
{
incrementProblem(n);
System.out.println("Enter Integer (-1 to exit) ? ");
n=scan.nextInt();
}
System.out.println("Thank you for using this program...");
}
}
INCREMENT PROBLEM 2. Write a program that repeatedly prompts the user for an integer and for output displays its increment. Make sure to write the problem using a void method called incrementProblem ). Keep the problem repeating using the sentinel/magic method. 30Pts SAMPLE RUN Enter Integer (-1 to Exit)? 2 Increment is 3 Enter Integer (-1 to Exit)? -1 Thank vou for using this program 3. Write an repeating interactive menu program that prompts the user for a choice to do the GRADE PROBLEM or the INCREMENT PROBLEM as describein the problemsabove. 40pts Here is a sampleMenu output: MAIN MENU 1. GRADE PROBLEM 2. INCREMENT PROBLEM 3. Exit Choice:Explanation / Answer
Answer 3:
package classProg;
import java.util.Scanner;
// Class Exam4B_03 definition
public class Exam4B_03
{
// Scanner class object declared to accept data from console
static Scanner sc;
// Function to accept a number and display its next number
void increment(int num)
{
// Increase the number by one and display it
System.out.printf(" Increment is %d", ++num);
}// End of function
// Function receives marks as parameter and displays grade
void grade(float marks)
{
// Checks if marks is greater than or equals to 90
if(marks >= 90)
// Displays grade 'O'
System.out.printf(" O Grade");
// Otherwise checks if marks is greater than or equals to 80
else if(marks >= 80)
// Displays grade 'E'
System.out.printf(" E Grade");
// Otherwise checks if marks is greater than or equals to 70
else if(marks >= 70)
// Displays grade 'A'
System.out.printf(" A Grade");
// Otherwise checks if marks is greater than or equals to 60
else if(marks >= 60)
// Displays grade 'B'
System.out.printf(" B Grade");
// Otherwise checks if marks is greater than or equals to 50
else if(marks >= 50)
// Displays grade 'C'
System.out.printf(" C Grade");
// Otherwise checks if marks is greater than or equals to 40
else if(marks >= 40)
// Displays grade 'D'
System.out.printf(" D Grade");
// Otherwise marks is less than 40
else
// Displays grade 'F'
System.out.printf("F Grade");
}// End of function
// Function to display menu, accept user choice and return user choice
int menu()
{
// Scanner class object created
sc = new Scanner(System.in);
// To store user choice
int ch;
// Display menu
System.out.printf(" 1. Grade Problem.");
System.out.printf(" 2. Increment Problem.");
System.out.printf(" 3. Exit.");
System.out.printf(" Choice: ");
// Accepts user choice
ch = sc.nextInt();
// Returns user choice
return ch;
}// End of function
// main function definition
public static void main(String ss[])
{
// Scanner class object created
sc = new Scanner(System.in);
// Class Exam4B_03 object created
Exam4B_03 obj = new Exam4B_03();
// Local variable to store the number entered by the user
int num;
// Local variable to store the marks entered by the user
float marks;
// Sentinel loop tills user entered number is not 3
do
{
// Calls the function menu() and calls the appropriate function based on the return value of the function
switch(obj.menu())
{
case 1:
System.out.printf(" Enter Total Marks: ");
marks = sc.nextFloat();
obj.grade(marks);
break;
case 2:
System.out.printf(" Enter integer (-1 to Exit)? ");
num = sc.nextInt();
obj.increment(num);
break;
case 3:
System.exit(0);
default:
System.out.printf(" Invalid choice! Try again......");
}// End of switch - case
}while(true);// End of do - while loop
}// End of function
}// End of class
Sample Output:
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 8
Invalid choice! Try again......
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 1
Enter Total Marks: 78
A Grade
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 1
Enter Total Marks: 92
O Grade
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 2
Enter integer (-1 to Exit)? 11
Increment is 12
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 2
Enter integer (-1 to Exit)? 33
Increment is 34
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 1
Enter Total Marks: 45
D Grade
1. Grade Problem.
2. Increment Problem.
3. Exit.
Choice: 3