In: Computer Science
This is 1 java question with its parts. Thanks so much!
Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point average field by dividing points by credit hours earned. Write methods to display the values in each Student field.
Use class named ShowStudent that instantiates a Student object to test your class. Compute the Student grade point average, and then display all the values associated with the Student.
Create a constructor for the Student class you created. The constructor should initialize each Student’s ID number to 9999, his or her points earned to 12, and credit hours to 3 (resulting in a grade point average of 4.0). Write a program that demonstrates that the constructor works by instantiating an object and displaying the initial values.
Source code
Student class
public class Student { //instance variables declaration to hold student information private int IdNumber; private int noCreditsHours; private double noPointsEarned; private double gradePointAverage; //Non parameterized constructor public Student() { IdNumber = 9999; noCreditsHours = 3; noPointsEarned = 12; } //display methods for each student field public void displayIdNumber() { System.out.println("Student ID: "+IdNumber); } public void displayNoCreditsHours() { System.out.println("Number of credit hours earned: "+noCreditsHours); } public void displayNoPointsEarned() { System.out.println("Number of points earned: "+noPointsEarned) ; } public void displayGradePointAverage() { System.out.println("Grade point average: "+gradePointAverage); } //setter method for each student field public void setIdNumber(int idNumber) { IdNumber = idNumber; } public void setNoCreditsHours(int noCreditsHours) { this.noCreditsHours = noCreditsHours; } public void setNoPointsEarned(double noPointsEarned) { this.noPointsEarned = noPointsEarned; } //method to calculate grade point average public double computeGradePointAvg() { gradePointAverage =noPointsEarned/noCreditsHours; return gradePointAverage; } }
ShowStudent class
public class ShowStudent { public static void main(String[] args) { //instance of student s1 is created Student s1 = new Student(); //setting its fields s1.setIdNumber(5456); s1.setNoCreditsHours(4); s1.setNoPointsEarned(12); //computing grade point average s1.computeGradePointAvg(); System.out.println("\nStudent s1 information: "); //displaying student s1 fields s1.displayIdNumber(); s1.displayNoCreditsHours(); s1.displayNoPointsEarned(); s1.displayGradePointAverage(); //instance of student s2 is created using default constructor Student s2 = new Student(); //computing grade point average s2.computeGradePointAvg(); System.out.println("\nStudent s2 information: "); //displaying student s2 fields s2.displayIdNumber(); s2.displayNoCreditsHours(); s2.displayNoPointsEarned(); s2.displayGradePointAverage(); } }
Screen shot of the code
Screen shot of the output