Question
Hello I have two java classes 1. student ========= public class Student { public static final int FRESHMAN = 1; public static final int SOPHOMORE
Hello I have two java classes 1. student ========= public class Student { public static final int FRESHMAN = 1; public static final int SOPHOMORE = 2; public static final int JUNIOR = 3; public static final int SENIOR = 4; public static final int NONTRADITIONAL = 5; private String firstName; private String lastName; private double gpa; private int year; private static int numClasses; private static int numGradePoints; public Student() { firstName = ""; lastName = ""; gpa = 0.0; year = 1; } public Student(String firstName, String lastName, double gpa,int year) { this.firstName = firstName; this.lastName = lastName; this.gpa = gpa; this.year = year; } public Student(String firstName, String lastName, int year) { gpa = 0.0; numClasses = 0; numGradePoints = 0; } public static void takeAClass(String grade) { numClasses++; if(grade == "A") { numGradePoints = numGradePoints+4; } if(grade == "B") { numGradePoints = numGradePoints+3; } if(grade == "C") { numGradePoints = numGradePoints+2; } if(grade == "D") { numGradePoints = numGradePoints+1; } if(grade == "F") { numGradePoints = numGradePoints+0; } } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public double getGpa() { return this.gpa; } public String getYear() { if(this.year == FRESHMAN) { return "FRESHMAN"; } else if(this.year == SOPHOMORE) { return "SOPHOMORE"; } else if(this.year == JUNIOR) { return "JUNIOR"; } else if(this.year == SENIOR) { return "SENIOR"; } else { return "NONTRADITIONAL"; } } } 2. Driver ==============
import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner;
public class Driver { private static Scanner scanner;
public static void main(String[] args) { scanner = new Scanner(System.in); Student[] myClass = new Student[5]; Arrays.fill(myClass, new Student()); System.out.println(Arrays.toString(myClass)); for(int i = 0; i < 5; i++) { getStudentInfo(myClass, i); } System.out.println(Arrays.toString(myClass)); int highestGpa = findMaxGpa(myClass); System.out.println(myClass[highestGpa].getFirstName() + " " + myClass[highestGpa].getLastName() + " has the highest GPA of " + myClass[highestGpa].getGpa()); int lowestGpa = findMinGpa(myClass); System.out.println(myClass[lowestGpa].getFirstName() + " " + myClass[lowestGpa].getLastName() + " has the lowest GPA of " + myClass[lowestGpa].getGpa()); System.out.println("The average GPA is: " + averageGpa(myClass)); //int year = getYear(myClass); System.out.println("The Year of Student is: " + getYear(myClass)); scanner.close(); } private static void getStudentInfo(Student[] students, int index) { String firstName, lastName; double gpa; int year; System.out.print("Please enter the student's first name: "); firstName = scanner.next();
while (firstName.isBlank()) { System.out.println("Name can't be blank."); System.out.print("Please enter the student's first name: "); firstName = scanner.next(); }
System.out.print("Please enter the student's last name: "); lastName = scanner.next();
while (lastName.isBlank()) { System.out.println("Name can't be blank."); System.out.print("Please enter the student's last name: "); lastName = scanner.next(); }
try { System.out.print("Please enter the student's GPA: "); gpa = scanner.nextDouble(); } catch (InputMismatchException e) { gpa = -1.0; }
while (gpa < 0.0 || gpa > 4.0) { System.out.println("GPA needs to be between 0.0 and 4.0."); try { System.out.print("Please enter the student's GPA: "); gpa = scanner.nextDouble(); } catch (InputMismatchException e) { gpa = -1.0; } } boolean inputIsValid = false; try { System.out.print("Please enter the student's year (1, 2, 3, 4, or 5 for Fresh, Soph, Jun, Sen, NT): "); year = scanner.nextInt(); } catch (InputMismatchException e) { year = -1; scanner.nextLine(); }
while (!inputIsValid) { if (year == Student.FRESHMAN || year == Student.SOPHOMORE || year == Student.JUNIOR || year == Student.SENIOR || year == Student.NONTRADITIONAL) { inputIsValid = true; } else { System.out.println("The year needs to be 1, 2, 3, 4, or 5."); try { System.out.print("Please enter the student's year (1, 2, 3, 4, or 5 for Fresh, Soph, Jun, Sen, NT): "); year = scanner.nextInt(); } catch (InputMismatchException e) { year = -1; scanner.nextLine(); } } }
while (firstName.isBlank()) { System.out.println("Name can't be blank."); System.out.print("Please enter the student's first name: "); firstName = scanner.next(); } students[index] = new Student(firstName, lastName, gpa,year);
} private static int findMaxGpa(Student[] students) { double largestSoFar = -1.0; int lsfIndex = -1; for(int i = 0; i < students.length; i++) { if(students[i].getGpa() >= largestSoFar) { largestSoFar = students[i].getGpa(); lsfIndex = i; } } return lsfIndex; } private static int findMinGpa(Student[] students) { double smallestSoFar = 5.0; int ssfIndex = -1; for(int i = 0; i < students.length; i++) { if(students[i].getGpa() <= smallestSoFar) { smallestSoFar = students[i].getGpa(); ssfIndex = i; } } return ssfIndex; } private static double averageGpa(Student[] students) { int count = 0; double sum = 0.0; for(Student student : students) { count++; sum += student.getGpa(); } return sum / count; } private static String getYear(Student[] students) { return "remain"; } private static void takingAClass() { Student[] myClass = new Student("vanita","patel",1); }
} Question: Can you please correct my code to print Year of students with gpa ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started