In: Computer Science
Create a class named Student. Student 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. 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. Save this class as Student.java
Write a class named ShowStudent that instantiates a Student object from the class you created and assigns values to its fields. Compute the Student grade point average, and then display all the values associated with the Student. Save the application as ShowStudent.java
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. Save the application as ShowStudent2.java
Hey mate, the Java code for above program is provided below with sample output. Here, I have attached 3 Java classes saved in same package with their file name same as their class name. If you are going to run these codes in your system then ensure that all these files are saved in same folder. Do refer the screenshot of each code for proper indentation and error free execution.
Student.java
public class Student{
int idNumber, creditHours, pointsEarned;
float pointAverage;
//constructor for the Student class
public Student()
{
this.idNumber=9999;
this.creditHours=3;
this.pointsEarned=12;
}
//methods to assign values to all fields
void setId(int id)
{
this.idNumber=id;
}
void setCredit(int credit)
{
this.creditHours=credit;
}
void setPoints(int points)
{
this.pointsEarned=points;
}
//method to compute the grade point average
void avg()
{
this.pointAverage=(this.pointsEarned/this.creditHours);
}
//methods to display the values in each Student field
void dispId()
{
System.out.println("ID number : "+this.idNumber);
}
void dispCredit()
{
System.out.println("Number of credit hours earned : "+this.creditHours);
}
void dispPoints()
{
System.out.println("Number of points earned. : "+this.pointsEarned);
}
void dispAverage()
{
System.out.println("Grade point average : "+this.pointAverage);
}
}
ShowStudent.java
class ShowStudent{
public static void main(String[] args)
{
Student Jack=new Student(); // Jack is Student class object
Jack.setId(2321);
Jack.setCredit(2);
Jack.setPoints(10);
Jack.avg();
Jack.dispId();
Jack.dispCredit();
Jack.dispPoints();
Jack.dispAverage();
}
}
ShowStudent2.java
class ShowStudent2{
public static void main(String[] args)
{
Student Sam=new Student(); // Sam is another Student class object
Sam.avg();
Sam.dispId();
Sam.dispCredit();
Sam.dispPoints();
Sam.dispAverage();
}
}
OUTPUT -
1. When ShowStudent.java is executed
2. When ShowStudent2.java is executed