Question: Write the Weighted class which is a subclass of the Course class. The Weighted class: Has one private instance variable, a double, which is the
| Write the Weighted class which is a subclass of the Course class. The Weighted class:
| public class Course { private String name; private int grade; public Course( String str, int g ) { name = str; grade = g; } public int getGrade() { return grade; } @Override public String toString() { return name + ": " + grade + "%"; } } |
Weighted w = new Weighted( "APCSA", 88, 1.2 );
System.out.println( w.getGrade() ); // 106
System.out.println( w ); // APCSA: 88% (106% weighted)
Course.Java:
public class Course { private String name; private int grade; public Course( String str, int g ) { name = str; grade = g; } public int getGrade() { return grade; } @Override public String toString() { return name + ": " + grade + "%"; } }
Main.Java:
public class Main { public static void main(String[] args) { Course c = new Course( "History", 88 ); System.out.println( c ); // History: 88% Weighted w = new Weighted( "APCSA", 88, 1.2 ); System.out.println( w.getGrade() ); // 106 System.out.println( w ); // APCSA: 88% (106% weighted) } }
Weighted.Java:
/* The Course class is perfect; do not change it.
Write the Weighted class which is a subclass of the Course class. The Weighted class: - Has one private instance variable, a double, which is the course's weight (e.g. 1.1 ) - Its constructor has three parameters: course name (String), grade (int), weight (double) - Override the getGrade method so that it returns the weighted grade (as an int). For example, if the grade is 80 and the weight is 1.1, then it should return 88. When converting the double to an int, it should round the decimals correctly (add 0.5 to the double and then cast to an int). - Override the toString method so that it returns a string in this format shown in the main method. The toString method should call the Weighted class's getGrade method. */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
