in java structure please Problem 2 Create a java program tha
***in java structure please***
Problem 2: Create a java program that takes the input as student name, grade point Avg. Ask the user to enter five student 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
import java.util.Scanner;
import java.util.Stack;
//Class for defining student
class StudentInfo{
public String name;//name attribute;
public float avgGrade;//avgGrade attribute;
//accessor method to get avgGrade
public float getAvgGrade() {
return avgGrade;
}
//accessor method to set avgGrade
public void setAvgGrade(float avgGrade) {
this.avgGrade = avgGrade;
}
//accessor method to get name
public String getName() {
return name;
}
//accessor method to set name
public void setName(String name) {
this.name = name;
}
}
//driver class to test the program
public class StudentInfoTest {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);//scanner to read values from user
System.out.println(\"Enter the 5 student details\");
Stack<StudentInfo> studentStack= new Stack<StudentInfo>();//stack class to hold student info
for(int i=0;i<5;i++){
StudentInfo s1= new StudentInfo();
System.out.print((i+1)+\") Enter Student Name :\");
s1.name=scan.next();//reading name;
System.out.print((i+1)+\") Enter Student avgGrade :\");
s1.avgGrade=scan.nextFloat();//reading avgGrade
studentStack.add(s1);//add to student info to stack
}
boolean shdContinue=false;//boolean to continue the program
do{
shdContinue=false;
System.out.println(\"Choose below option\");
System.out.println(\"1) Min Grade\");
System.out.println(\"2) Avg Grade\");
int choice=scan.nextInt();
//switch to choose option
switch (choice) {
case 1:
displayMinGrade(studentStack);//method to display min grade
break;
case 2:
displayAvgGrade(studentStack);//method to display avg grade
break;
default:
System.out.println(\"Wrong choice\");
break;
}
System.out.println(\"do you want to continue? (y or Y)\");
String y=scan.next();// decide to continue or not
if(y.equals(\"y\") || y.equals(\"Y\")){
shdContinue=true;
}
}while(shdContinue);
}
// method to calculate the avg grade
private static void displayAvgGrade(Stack<StudentInfo> studentStack) {
float avggrade=0;//
for(StudentInfo eachStudentInfo : studentStack){
avggrade=avggrade+eachStudentInfo.getAvgGrade();// adding all the avg grade
}
avggrade=avggrade/studentStack.size();// dividing the sum by the size of the stack
System.out.println(\"Avg Grade is: \"+avggrade);
}
// method to calculate the min grade
private static void displayMinGrade(Stack<StudentInfo> studentStack) {
float mingrade=studentStack.get(0).getAvgGrade();// assigning 1st as min.
for(StudentInfo eachStudentInfo : studentStack){
if(eachStudentInfo.getAvgGrade()<mingrade){
mingrade=eachStudentInfo.getAvgGrade();// if the current min is less than mingrade than assign mingrade as current grade
}
}
System.out.println(\"Min Grade is: \"+mingrade);
}
}
--------------output------------
Enter the 5 student details
1) Enter Student Name :A
1) Enter Student avgGrade :5
2) Enter Student Name :B
2) Enter Student avgGrade :6
3) Enter Student Name :C
3) Enter Student avgGrade :7
4) Enter Student Name :D
4) Enter Student avgGrade :8
5) Enter Student Name :E
5) Enter Student avgGrade :9
Choose below option
1) Min Grade
2) Avg Grade
1
Min Grade is: 5.0
do you want to continue? (y or Y)
y
Choose below option
1) Min Grade
2) Avg Grade
2
Avg Grade is: 7.0
do you want to continue? (y or Y)
j
--------------out ends--------
//note: feel free to ask question/doubts. God Bless you!!


