In Java, w rite a class Grade with following member variables and method: int mi
ID: 643439 • Letter: I
Question
In Java, write a class Grade with following member variables and method:
int midtermExam, int finalExam: represent the scores of midterm and final exams respectively. The maximum score for each exam is 100.
double getOverallScore(): returns the overall score calculated as follows: 40% midterm and 60% final.
Write another class Grade180 that is a subclass of Grade and has the following member variables and method:
int midtermExam, int finalExam: inherited from Grade super class.
int hw1, int hw2, int project1, int project2 : represent the scores of homeworks, projects respectively. The maximum score for each homework and project is 100.
double getOverallScore(): overrides the method in Grade class and returns the overall score calculated as follows: 20% homeworks (each of equal weight), 30% projects (each of equal weight), 20% midterm and 30% final.
Note: data for member variables should be passed as arguments to their respective constructors and initialized in them.
You may use your main method for testing, but it will not be tested by webcat.
Explanation / Answer
//Code Modified
//Changes are in bold
//Grade clas that sets constrctor with midterm score
//and final exam score and calculate the getOverallScore.
//Grade.java
public class Grade
{
private int midtermExam;
private int finalExam;
public Grade(int midtermExam, int finalExam)
{
this.midtermExam=midtermExam;
this.finalExam=finalExam;
}
//returns the overall score calculated as follows: 40% midterm and 60% final.
public double getOverallScore()
{
return midtermExam*(0.40)+finalExam*(0.60);
}
//returns the midtermExam
public int getMidTermExam()
{
return midtermExam;
}
//returns the midtermExam
public int getFinalExam()
{
return finalExam;
}
}
-----------------------------------------------------------------------------------------------------------------------------
//Grade180.java that extends the super class Grade.
public class Grade180 extends Grade
{
//instance variabels of class Grade180
private int hw1;
private int hw2;
private int project1;
private int project2;
//constructor that sets the mid term exam, final exam
//home work1 and home work2 and project1 and project2 scores
public Grade180(int midtermExam, int finalExam,
int hw1, int hw2, int project1, int project2)
{
super(midtermExam, finalExam);
this.hw1=hw1;
this.hw2=hw2;
this.project1=project1;
this.project2=project2;
}
//getter methods
public int getHw1()
{
return hw1;
}
public int getHw2()
{
return hw2;
}
public int getProject1()
{
return project1;
}
public int getProject2()
{
return project2;
}
// overrides the method in Grade class and returns the overall score
//calculated as follows:
//20% homeworks (each of equal weight means each of 10 percent (0.10))
//30% projects (each of equal weight means each of 15 percent (0.15))
//20% midterm and 30% final.
//10(hw1)+10(hw2)+15(project1)+15(project2)+20(midterm )+30(final)=100 %
@Override
public double getOverallScore()
{
//hw1 and hw2 each of equal weight is 10(0.10)% of 20%
//projec1 and project2 each of equal weight is 15(0.15) % of 30%
return hw1*(0.10)+hw1*(0.10)+project1*(0.15)+project2*(0.15)+
getMidTermExam()*(0.20)+getFinalExam()*(0.30);
}
}
---------------------------------------------------------------------------------------------------
public class TesterProgram
{
public static void main(String[] args)
{
int midtermExam=50;
int finalExam=50;
int hw1=40;
int hw2=40;
int project1=80;
int project2=80;
//Create an instance of Grade180 class
Grade180 grade180=new Grade180(midtermExam, finalExam, hw1, hw2, project1, project2);
//print scores
System.out.println("Home Work1 : "+grade180.getHw1());
System.out.println("Home Work2 : "+grade180.getHw2());
System.out.println("Project 1 : "+grade180.getProject1());
System.out.println("Project 2 : "+grade180.getProject2());
//calling getMidterExam score
System.out.println("Mid term Score : "+grade180.getMidTermExam());
//Calling final score
System.out.println("Final score : "+grade180.getFinalExam());
//calling getOverallScore
System.out.println("Overall Score : "+grade180.getOverallScore());
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
Home Work1 : 40
Home Work2 : 40
Project 1 : 80
Project 2 : 80
Mid term Score : 50
Final score : 50
Overall Score : 57.0
Hope this helps you...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.