You will implement a student class. A student has an ID, a first and last name a
ID: 3829583 • Letter: Y
Question
You will implement a student class.
A student has an ID, a first and last name and is enrolled in a specific program.
They are methods that can register or withdraw the student.
Other used methods relate to student’s academic performance. A weighted total in the course is computed.
Based on his/her attendance the students might receive extra credit.
A studentInfo method will display information about the student.
What are the used variables? Declare them with their type and access modifier.
numberStudents should be updated in the constructor and it represents the current number of created students.
numberRegistered – represents the number of students currently registered in class; initialized to 8.
classCapacity – the class capacity (initialized to 10). The capacity is a constant.
ID
lastName
firstName
program – U for Bachelor, G for Master
Note: make sure that for ID, midterm grade, and final grade you create encapsulation; have these variables declared as private.
Constructors
Write two constructors for the Student class:
1. a constructor that takes as arguments student ID, the last name, first name. 2. a constructor that takes no arguments and initializes the variables to:
999, Doe, Jane
NOTE: make sure that the number of students is incremented with each new created student.
Setters and getters
Create setters and getters for the private variables (ID, midtermGrade, finalGrade)
Methods
What methods would you supply for the student class? List them and give the complete signature for each method based on its description.
register( ), withdraw( ), extraCredit( ), getTotal( ), studentInfo( )
register
if the class is up to its capacity display a message that the student cannot register, otherwise update the number of registered students and display “X registered”
Note: X is student’s lastname.
withdraw
The withdraw method will withdraw the student from the class by decrementing the number of registered student and display “X withdrew”
Note: X is student’s lastname
extraCredit
the extraCredit method generates a random integer between 0 and 10. If the number is
below 5, no extra credit will be assigned and the method returns false; Otherwise it will return true;
getTotal
this method computes the total in the course. It takes the program as argument and returns the total.
If the student is a Bachelor degree student, the total will be computed as: Total = 30%*midtermGrade + 70%*finalGrade + Extra Credit
If the student is a Master degree student the total will be computed as: Total = 45%midtermGrade + 55%finalGrade + Extra Credit
The Extra Credit is 0 if the student receives no extra credit (extraCredit() returns false) or 10 if the students receives extra credit.
studentInfo
The studentInfo method will display student’s name, student’s id, his grades in the midterm and final and total in the course;
studentInfo() might or might not use arguments; if you directly call getTotal() you will need to pass the program.
Next write a class named TestStudent to check the Student class.
Create three students, one generic students and another two with specific information.
Display information about each student.
Display the number of registered students Display the number of created students
Attempt to register all students to class
The second created student decides to withdraw Register the generic student
Set the grade for the remaining students:
First student received a 70 in the midterm and a 97.5 in the final;
Last student to register received a 85 in the midterm and 25.6 in the final.
Display the information for the students. Keep in mind that first student is an Undergraduate student and the last one is a Graduate student.
Explanation / Answer
Below is your program: -
Student.java
import java.util.Random;
public class Student {
private int id;
public String firstName;
public String lastName;
public Character program; // U for Undergraduate , G for Master
public static int numberStudents = 0;
public static int numberRegistered;
public final static int classCapacity = 10; //Capacity of class is constant so made it final
private double midtermGrade;
private double finalGrade;
Student(int studentID,String lName,String fName) {
this.id = studentID;
this.lastName = lName;
this.firstName = fName;
numberStudents ++;
numberRegistered =8;
}
Student() {
this.id = 999;
this.lastName = "Doe";
this.firstName = "Jane";
numberStudents ++;
numberRegistered =8;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getMidtermGrade() {
return midtermGrade;
}
public void setMidtermGrade(double midtermGrade) {
this.midtermGrade = midtermGrade;
}
public double getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(double finalGrade) {
this.finalGrade = finalGrade;
}
public void register() {
if(numberRegistered == classCapacity) {
System.out.println("Class is full.Student cannot registered.");
} else {
numberRegistered++;
System.out.println(this.lastName+" registered.");
}
}
public void withdraw() {
numberRegistered--;
System.out.println(this.lastName+" withdrew.");
}
public boolean extraCredit() {
Random rand = new Random();
int r = rand.nextInt((10 - 0) + 1);
if(r<5) {
return false;
} else {
return true;
}
}
public double getTotal(Character prog) {
if(prog == 'U') {
if(extraCredit()) {
return ((30*this.getMidtermGrade())/100) + ((70*this.getFinalGrade())/100) + 10;
} else {
return ((30*this.getMidtermGrade())/100) + ((70*this.getFinalGrade())/100);
}
} else {
if(extraCredit()) {
return ((30*this.getMidtermGrade())/100) + ((70*this.getFinalGrade())/100) + 10;
} else {
return ((45*this.getMidtermGrade())/100) + ((55*this.getFinalGrade())/100);
}
}
}
public void studentInfo() {
System.out.println("Student Name:"+this.firstName+" "+this.lastName);
System.out.println("Student Id:"+this.getId());
System.out.println("Mid Term Grade:"+this.getMidtermGrade());
System.out.println("Final grade:"+this.getFinalGrade());
System.out.println("Total grades:"+this.getTotal(this.program));
}
}
TestStudent.java
public class TestStudent {
public static void main(String[] args) {
Student stA = new Student();
Student stB = new Student(23, "Winchester", "Dean");
Student stC = new Student(34, "Salvatore", "Damon");
System.out.println("Number of Created Students:"+Student.numberStudents);
System.out.println("Number of Rgistered Students:"+Student.numberRegistered);
stB.register();
stC.register();
stA.register();
System.out.println();
System.out.println("Number of Created Students:"+Student.numberStudents);
System.out.println("Number of Rgistered Students:"+Student.numberRegistered);
stB.withdraw();
stA.register();
System.out.println();
System.out.println("Number of Created Students:"+Student.numberStudents);
System.out.println("Number of Rgistered Students:"+Student.numberRegistered);
stC.program = 'U';
stA.program = 'G';
stC.setMidtermGrade(70);
stC.setFinalGrade(97.5);
stA.setMidtermGrade(85);
stA.setFinalGrade(25.6);
System.out.println();
System.out.println("Student Details:");
stA.studentInfo();
System.out.println();
// stB.studentInfo(); // As grades of this student is not given , the details cannot be shown So commented this.
System.out.println();
stC.studentInfo();
System.out.println();
}
}
Sample Run: -
Number of Created Students:3
Number of Rgistered Students:8
Winchester registered.
Salvatore registered.
Class is full.Student cannot registered.
Number of Created Students:3
Number of Rgistered Students:10
Winchester withdrew.
Doe registered.
Number of Created Students:3
Number of Rgistered Students:10
Student Details:
Student Name:Jane Doe
Student Id:999
Mid Term Grade:85.0
Final grade:25.6
Total grades:52.33
Student Name:Damon Salvatore
Student Id:34
Mid Term Grade:70.0
Final grade:97.5
Total grades:89.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.