In: Computer Science
Design a class named Student that contains the followingprivate instance variables:
A string data field name for the student name.
An integer data field id for the student id.
A double data field GPA for the student GPA.
An integer data field registeredCredits.
It contains the following methods:
An empty default constructor.
A constructor that creates a student record with a specified id and
name.
The get and set methods for id, name, GPA, and
registeredCredits.
A method called registerCredit that adds specified amount of
credits to the student.
A method called withdrawCredit that withdraws specified
amount of credits from the student.
Implement the class.
Write a test program that creates a student account using your
name and id. Set all the variables information to your student
information (GPA, and registered credits). Use the
registerCreditmethod to add 9 more credits. Use the method
withdrawCreditto withdraw 3 credits. Then prints out all the
student information:name, id, GPA, and registerdCredits.
Sample Output:
Enter your GPA: 3.9
Enter your registered credits: 30Student name: Sara
Student ID: 12345678
GPA: 3.9
Registered credits: 36
(( in java ))
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
Student.java
class Student
{
private String name;
private int id;
private double gpa;
private int registeredCredits;
// Constructor
Student(){
}
public Student(String name, int id) {
this.name = name;
this.id = id;
}
//Setter and getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public int getRegisteredCredits() {
return registeredCredits;
}
public void setRegisteredCredits(int registeredCredits) {
this.registeredCredits = registeredCredits;
}
//methods
public void registerCredit(double amount)
{
registeredCredits += amount;
}
public void withdrawCredit(double amount)
{
registeredCredits -= amount;
}
}
StudentTest.java
import java.util.Scanner; public class StudentTest { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); // create object Student student = new Student("Sara", 12345678); //read GPA System.out.print("Enter Your GPA: "); double gpa= scanner.nextDouble(); student.setGpa(gpa); //read Credits System.out.print("Enter Your registered credits: "); int credits= scanner.nextInt(); student.setRegisteredCredits(credits); // add credits student.registerCredit(9); //remove credits student.withdrawCredit(3); //print data System.out.println("Student name: "+student.getName()); System.out.println("Student ID: "+student.getId()); System.out.println("GPA: "+student.getGpa()); System.out.println("Registered credits: "+student.getRegisteredCredits()); } }
=================================