Question 5 Complete the missing method implementation in the following Result cl
ID: 3846239 • Letter: Q
Question
Question 5 Complete the missing method implementation in the following Result class so that the following program run produces the output as mentioned below:
Total = 82.5 Grade is Pass
public class Result implements Gradable { double total; //constructor
public Result(double total) { this.total = total;
}
/* Implement the method available from the implementing interface (Gradable) to print appropriate grade based on the following:
The grade is “Pass” when the total is more than or equal to 50 and it (grade) is “Fail” if the total is less than 50 */
//Missing method has to be completed
//toString method
@Override
public String toString() { return "Total = " + total + " Grade is "+calculateGrade();
}
public static void main(String [] args){ Result myMarks = new Result(82.5);
System.out.println(myMarks);
}
}
interface Gradable{
String calculateGrade();
}
Explanation / Answer
// this is the method which is taking no parameters and returning a string
public String calculateGrade(){
// checking if total is greater than or equal to 50
if(total>=50)
// returning Pass
return "Pass";
// if it is not, then returning Fail
else
return "Fail";
}
// Prints Total = 82.5 Grade is Pass
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.