Create a java program that takes the input as student name g
Create a java program that takes the input as student name, grade point Avg. Ask the user to enter fivestudent names with grades using the scanner class. Please use the concept of stack to store all the student information. Once you store five students, display two options 1 MinGrade 2 Avg grade.
If the user enters 1, then print min grade and if the user enters 2, then print Avg class grade.
Solution
Please follow the code and comments for description :
CODE :
import java.util.*; // required imports
public class minAndAvgGrades { // class to run the code
public static void main(String[] args) { // driver method
 Scanner sc = new Scanner(System.in); // scanner class to get the data
 Stack<String> namestack = new Stack<>(); // required stack objects
 Stack<Integer> gradestack = new Stack<>();
 for (int i = 0; i < 5; i++) { // iterate over the data and save the values
 System.out.println(\"Enter the Name of the Student : \"); // message prompt
 String name = sc.nextLine(); // get the data
 sc.nextLine();
 namestack.push(name); // add the dat to the stack
 System.out.println(\"Enter the Grade of the Student : \"); // messaeg prompt
 int number = sc.nextInt(); // get the data
 gradestack.push(number); // add the data to the stack
 }
 System.out.println(\"\ You have the Below Choices..\"); // print the choices
 System.out.println(\"1.Minimum Grade.\");
 System.out.println(\"2.Average Grade.\");
 int choice = sc.nextInt(); // get the choice
switch (choice) { // switch over the choices
 case 1:
 System.out.println(\"Calculating the Minimum Grade of the Students..\"); // message
 int min = gradestack.firstElement(); // get the first element
 for (int i = 1; i <= 5; i++) {
 if (gradestack.peek() < min) { // check for the minimu value
 min = gradestack.pop(); // pop the value
 } else {
 gradestack.pop();
 }
 }
 System.out.println(\"The Minimum Grade of the Information is : \" + min); // message
 break;
 case 2:
 System.out.println(\"Calculating the Average Grade of the Students..\"); // message
 int sum = 0; // local variables
 while (!gradestack.isEmpty()) { // check for the empty
 sum = sum + gradestack.pop(); // sum up the values
 }
 double avg = sum / 5;
 System.out.println(\"The Average grades for the Information is : \" + avg); // get the avrage to console
 break;
 default:
 System.out.println(\"Invalid Selection.!\"); // default choice
 System.exit(0);
 break;
 }
 }
 }
 OUTPUT :
Run 1 :
Enter the Name of the Student :
 John
 Enter the Grade of the Student :
 65
 Enter the Name of the Student :
 Mike
 Enter the Grade of the Student :
 54
 Enter the Name of the Student :
 Carter
 Enter the Grade of the Student :
 86
 Enter the Name of the Student :
 Michael
 Enter the Grade of the Student :
 68
 Enter the Name of the Student :
 Mark
 Enter the Grade of the Student :
 72
You have the Below Choices..
 1.Minimum Grade.
 2.Average Grade.
 1
 Calculating the Minimum Grade of the Students..
 The Minimum Grade of the Information is : 54
Run 2 :
Enter the Name of the Student :
 Mike
 Enter the Grade of the Student :
 85
 Enter the Name of the Student :
 Jane
 Enter the Grade of the Student :
 65
 Enter the Name of the Student :
 James
 Enter the Grade of the Student :
 84
 Enter the Name of the Student :
 Musk
 Enter the Grade of the Student :
 71
 Enter the Name of the Student :
 Park
 Enter the Grade of the Student :
 59
You have the Below Choices..
 1.Minimum Grade.
 2.Average Grade.
 2
 Calculating the Average Grade of the Students..
 The Average grades for the Information is : 72.0
 Hope this is helpful.



