rewrite the code so that inheritance is used to represent different types of stu
ID: 3836000 • Letter: R
Question
rewrite the code so that inheritance is used to represent different types of students instead of the "type" parameter.this will result in making the student class an abstract class and creating 2 new classes Wstudent and RegularStudent derived from student class. in the new classes the grade will be claculated differently. all attributes should be made private.
Heres my code:
public class Student {
public static final int WStudent = 0;
public static final int RegularStudent = 1;
public int type; //stores type of student
public String name;
public double score;
public String Grade;
public Student(int type, String name, double score)
{
this.type = type;
this.name = name;
this.score = score;
CalculateGrade();
}
private void CalculateGrade()
{
if(type == 1)
{
if (score >= 90 ) Grade = "A";
else if (score >=80) Grade = "B";
else if (score >= 70) Grade = "C";
else if (score >= 60) Grade = "D";
else Grade = "F";
}
else
{
if(score < 70) Grade = "F" ; else Grade = "P";
}
}
public void DisplayStudent()
{
System.out.println("name = " + name + ", score = " + score + " grade = " + Grade );
}
public static void main(String[] args)
{
Student s1 = new Student(0,"Wstudent1",80);
Student s2 = new Student(0,"Wstudent2",60);
Student s3 = new Student(1,"Regularstudent1",85);
Student s4 = new Student(1,"Regularstudent2",92);
s1.DisplayStudent();
s2.DisplayStudent();
s3.DisplayStudent();
s4.DisplayStudent();
}
}
Explanation / Answer
public abstract class Student{
private String name;
private double score;
private String Grade;
public Student(String name, double score)
{
this.name = name;
this.score = score;
}
public String getName(){
return name;
}
public double getScore(){
return score;
}
}
class WStudent extends Student{
private String Grade;
public WStudent(String name, double score){
super(name,score);
DisplayStudent();
}
private void CalculateGrade()
{
if(getScore() < 70) Grade = "F" ; else Grade = "P";
}
private void DisplayStudent()
{
CalculateGrade();
System.out.println("name = " + getName() + ", score = " + getScore() + ", grade = " + Grade );
}
}
class RegularStudent extends Student{
private String Grade;
public RegularStudent(String name, double score){
super(name,score);
DisplayStudent();
}
private void CalculateGrade()
{
double score = getScore();
if (getScore() >= 90 ) Grade = "A";
else if (getScore() >=80) Grade = "B";
else if (getScore() >= 70) Grade = "C";
else if (getScore() >= 60) Grade = "D";
else Grade = "F";
}
private void DisplayStudent()
{ CalculateGrade();
System.out.println("name = " + getName() + ", score = " + getScore() + ", grade = " + Grade );
}
}
public class StudentDemo{
public static void main(String[] args)
{
WStudent s1 = new WStudent("Wstudent1",80);
WStudent s2 = new WStudent("Wstudent2",60);
RegularStudent s3 = new RegularStudent("Regularstudent1",85);
RegularStudent s4 = new RegularStudent("Regularstudent2",92);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.