Question: Hello, My task is to create a program in java to enter, grades, calculate average and letter grade. There are no errors noted in the

Hello,

My task is to create a program in java to enter, grades, calculate average and letter grade. There are no errors noted in the program however, I am not getting the desired output. Option C is suppose to print the students in alphabetical order using the toString. When I chose option C the program stops working.

Main:

import java.util.Arrays;

import java.util.Scanner;

public class StudentClass {

public static void main(String[] args) {

{

Scanner input = new Scanner(System.in);

Student[] students = new Student[20];

int count = 0; // number of students

char option;

String sName;

String lName;

int sID;

int grade;

int index;

do

{

System.out.println("---MENU---");

System.out.println("a. Add new student");

System.out.println("b. Enter test grades");

System.out.println("c. Display all students");

System.out.println("d. Exit the program");

System.out.print("Enter your option: ");

option = input.next().charAt(0);

switch(option)

{

case 'a':

System.out.print(" Enter the first name of the student: ");

sName = input.next();

System.out.print(" Enter the first name of the student: ");

lName = input.next();

System.out.print(" Enter the id of the student: ");

sID = input.nextInt();

students[count] = new Student(sName,lName,sID);

count++;

break;

case 'b':

if(count == 0)

{

System.out.println("No students are in the list.");

break;

}

System.out.print(" Enter number of student staring with one : ");

index = input.nextInt();

if(index < 0 || index < count)

{

System.out.print("Enter grade 1 out of 3: ");

grade = input.nextInt();

students[index].setGrade(0, grade);

System.out.print("Enter grade 2 out of 3: ");

grade = input.nextInt();

students[index].setGrade(1, grade);

System.out.print("Enter grade 3 out of 3: ");

grade = input.nextInt();

students[index].setGrade(2, grade);

}

else

{

System.out.println("Invalid index! The index should be >=0 and < " +count);

}

break;

case 'c':

System.out.println(" Details of all students:");

if(count == 0)

{

System.out.println("No students are in the list.");

break;

}

Arrays.sort(students);

for (int i = 0; i< students.length-1; i++) {

System.out.printf(Arrays.toString(students));

break;

}

case 'd':

System.out.println("Thank you.");

break;

default:

System.out.println("Invalid Option!");

}

System.out.println();

}while(option != 'd');

}

}

}//end of class

Class:

/ data members

public class Student {

private String firstName;

private String lastName;

private int id;

private int grades[] = new int[3];

// constructor for name and ID

public Student(String studentFirstName, String studentLastName, int rID)

{

firstName = studentFirstName;

lastName = studentLastName;

id = rID;

grades[0] = -1;

grades[1] = -1;

grades[2] = -1;

}

//get private variable student name

public String getStudentName()

{

return firstName + lastName;

}

//get private variable id

public int getrID()

{

return id;

}

// get private index grades

public int getGrade(int index)

{

return grades[index];

}

//get private variable grades

public int[] getGrades()

{

return grades;

}

//method average

public double getAverage()

{

return (grades[0] + grades[1] + grades[2]) / 3.0;

}

//method letter grade

public char getLetterGrade()

{

double avgGrd = getAverage();

if(avgGrd >= 90)

return 'A';

else if(avgGrd >= 80)

return 'B';

else if(avgGrd >= 70)

return 'C';

else if(avgGrd >= 60)

return 'D';

else

return 'F';

}

public void setID(int rID)

{

id = rID;

}

public void setGrade(int index, int grade)

{

grades[index] = grade;

}

public void setGrades(int[] aGrades)

{

grades[0] = aGrades[0];

grades[1] = aGrades[1];

grades[2] = aGrades[2];

}

//Return a string discription of the class

@Override

public String toString() {

return " Name: " + firstName + lastName + " ID:" + id + " Grade1: " +

grades[0] + " Grade2:" + grades [1] + " Grade3: " + grades[2] + "Average grade: " + getAverage() + " Letter grade:" + getLetterGrade();

}

}//end of class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!