JAVA Add a name field (type String) to your Student class and convert it into an
ID: 3752831 • Letter: J
Question
JAVA
Add a name field (type String) to your Student class and convert it into an abstract class with an abstract method hoursLeft()
Create another subclass of Student called GraduateStudent with an additional instance field
foundationHours - int
Create default and a fully parameterized constructur for GraduateStudent as well as setter/getter methods for the new instance field.
Override the hoursLeft() method, such that it now shows the hours remaining after the student took the foundation classes.
Create toString() Methods inside the CSStudent and the GraduateStudent classes showing the class and the name.
Create a new StudentTester class
Convince yourself that you cannot create an instance of Student anymore (try it and make sure you understand the error message)
Create an Array of type Student[] with space for five Student instances.
Fill it with 3 CSStudent and 2 GraduateStudent instances.
Write a method private static void showStudents(Student[] students) that loops over the array given as a parameter and prints each student followed by the result of calling hoursLeft().
Call this from the main with the Array you defined in steps 2 and 3.
Here is my code please add to here I can't figure it out:
public class StudentTester {
public static void main(String[] args) {
Student jim = new Student(120);
Student kim = new CSStudent(120,20,24);
System.out.println(jim.getTotalHoursToTake());
System.out.println(kim.getTotalHoursToTake());
System.out.println(jim.hoursLeft());
System.out.println(kim.hoursLeft());
}
}
public class Student {
private int totalHoursToTake;
public Student() {
this(120);
}
public Student(int totalHoursToTake) {
this.totalHoursToTake = totalHoursToTake;
}
public int getTotalHoursToTake() {
return totalHoursToTake;
}
public void setTotalHoursToTake(int totalHoursToTake) {
this.totalHoursToTake = totalHoursToTake;
}
public int hoursLeft() {
return totalHoursToTake;
}
}
public class CSStudent extends Student {
private int genEdHours;
private int mathHours;
public CSStudent() {
this(120);
}
public CSStudent(int totalHoursToTake) {
this(totalHoursToTake,0,0);
}
public CSStudent(int totalHoursToTake, int genEdHours, int mathHours) {
super(totalHoursToTake);
this.genEdHours = genEdHours;
this.mathHours = mathHours;
}
public int getGenEdHours() {
return genEdHours;
}
public void setGenEdHours(int genEdHours) {
this.genEdHours = genEdHours;
}
public int getMathHours() {
return mathHours;
}
public void setMathHours(int mathHours) {
this.mathHours = mathHours;
}
@Override
public int hoursLeft() {
return getTotalHoursToTake() - genEdHours - mathHours;
}
}
Explanation / Answer
Note: Could u plz check the code and let me know if u need any modifications.Thank You
_______________
Student.java
public abstract class Student {
private String name;
private int totalHoursToTake;
public Student() {
this(120);
}
public abstract int hoursLeft();
public Student(String name, int totalHoursToTake) {
this.name = name;
this.totalHoursToTake = totalHoursToTake;
}
public Student(int totalHoursToTake) {
this.totalHoursToTake = totalHoursToTake;
}
public int getTotalHoursToTake() {
return totalHoursToTake;
}
public void setTotalHoursToTake(int totalHoursToTake) {
this.totalHoursToTake = totalHoursToTake;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Student [totalHoursToTake=" + totalHoursToTake + "]";
}
}
________________
GraduateStudent.java
public class GraduateStudent extends Student {
//Declaring instance variables
private int foundationHours;
//Zero argumented constructor
public GraduateStudent() {
}
//Parameterized constructor
public GraduateStudent(String name ,int foundationHours) {
super.setName(name);
this.foundationHours = foundationHours;
}
public GraduateStudent(int foundationHours) {
this.foundationHours = foundationHours;
}
// getters and setters
public int getFoundationHours() {
return foundationHours;
}
public void setFoundationHours(int foundationHours) {
this.foundationHours = foundationHours;
}
@Override
public int hoursLeft() {
return getTotalHoursToTake()-foundationHours;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Graduate Student: Name =" + getName();
}
}
_________________
CSStudent.java
public class CSStudent extends Student {
private int genEdHours;
private int mathHours;
public CSStudent() {
this(120);
}
public CSStudent(int totalHoursToTake) {
this(totalHoursToTake,0,0);
}
public CSStudent(String name,int totalHoursToTake) {
super(name, totalHoursToTake);
}
public CSStudent(int totalHoursToTake, int genEdHours, int mathHours) {
super(totalHoursToTake);
this.genEdHours = genEdHours;
this.mathHours = mathHours;
}
public CSStudent(String name,int totalHoursToTake, int genEdHours, int mathHours) {
super(name,totalHoursToTake);
this.genEdHours = genEdHours;
this.mathHours = mathHours;
}
public int getGenEdHours() {
return genEdHours;
}
public void setGenEdHours(int genEdHours) {
this.genEdHours = genEdHours;
}
public int getMathHours() {
return mathHours;
}
public void setMathHours(int mathHours) {
this.mathHours = mathHours;
}
@Override
public int hoursLeft() {
return getTotalHoursToTake() - genEdHours - mathHours;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "CSStudent: Name =" + getName();
}
}
_________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an Array of 5 Graduate Students
Student st[]={new GraduateStudent("John", 10),
new GraduateStudent("Sam", 7),
new CSStudent("Kane",45),
new CSStudent("Bob",55),
new CSStudent("Ken",50,4, 5)};
//Displaying the output
for(int i=0;i<st.length;i++)
{
System.out.println(st[i]);
}
}
}
_________________
Output:
Graduate Student: Name =John
Graduate Student: Name =Sam
CSStudent: Name =Kane
CSStudent: Name =Bob
CSStudent: Name =Ken
_________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.