Question
Hello, This program works I want to attempt to correct the output. My task is to create a program in java to enter, grades, calculate
Hello,
This program works I want to attempt to correct the output. My task is to create a program in java to enter, grades, calculate average and letter grade.
Output:
Name: GinaHarrell
ID:123
Grade1: 90
Grade2:98
Grade3: 100Average grade: 96.0
Letter grade:A,
Name: JamesStagg
ID:678
Grade1: 98
Grade2:56
Grade3: 98Average grade: 84.0
Letter grade:B,
Name: TeresaAdam
ID:676
Grade1: 78
Grade2:-1
Grade3: 98Average grade: 58.333333333333336
Letter grade:F, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]Thank you.
Corrections:
I want a space between the last and first name. It appears to be the the program just not printing out. I like to get rid of the comas after lettergrade. Last name on output has null, null....would like to fix.
Also in option B I would like to be able to enter the grade using the student ID.
I would like my array to implement without set number. I tried arraylist but my implementation was incorrect.
See Code below.
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 last 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;
}
// Sort array by last name
Arrays.sort(students,0,count);
for (int i = 0; i < count; i++) {
System.out.print(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
public class Student implements Comparable
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();
}
@Override
public int compareTo(Student o) {
String name1 = this.lastName + " " + this.firstName;
String name2 = o.lastName + " " + o.firstName;
if(name1.compareTo(name2) < 0) {
return -1;
} else if(name1.compareTo(name2) > 0) {
return 1;
}
return 0;
}
}// end of class
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