Complete the Grade class, byDeclaring the private int percentage instance variableWriting the constructor method with input parameters for percentage. If the input parameter does not store a value in the range of 0 and 100 then initialise this.percentage to 0Writing the get method for percentage
Question
Complete the Grade class, byDeclaring the private int percentage instance variableWriting the constructor method with input parameters for percentage. If the input parameter does not store a value in the range of 0 and 100 then initialise this.percentage to 0Writing the get method for percentage
Solution
Here is the Java code for the Grade class:
public class Grade {
// Declaring the private int percentage instance variable
private int percentage;
// Writing the constructor method with input parameters for percentage
public Grade(int percentage) {
if (percentage >= 0 && percentage <= 100) {
this.percentage = percentage;
} else {
// If the input parameter does not store a value in the range of 0 and 100 then initialise this.percentage to 0
this.percentage = 0;
}
}
// Writing the get method for percentage
public int getPercentage() {
return this.percentage;
}
}
In this code:
- We first declare a private integer variable
percentage. - We then write a constructor for the
Gradeclass that takes an integerpercentageas an argument. If thepercentageis between 0 and 100 (inclusive), we assign it to the instance variablethis.percentage. If it's not in this range, we assign0tothis.percentage. - Finally, we write a getter method
getPercentage()that returns the value ofthis.percentage.
Similar Questions
Java Program To Accept the Marks of a Student and Find the Total Marks and Percentage
Write a Java program that determines a student’s grade. The program will read three types ofscores(quiz, mid-term, and final scores) and determine the grade based on the following rules: -if theaverage score >=90% =>grade=A -if the average score >= 70% and <90% => grade=B -if theaverage score>=50% and <70% =>grade=C -if the average score<50% =>grade=F
Accept the grades from the user and display the grade according to the following criteria:Below 25 --D25 to 45 -- C45 to 50 -- B50 to 60 --B+60 to 80 -- AAbove 80 -- A+
Develop the Internal mark calculation system based on the attendance percentage using Java. Get the student name, register number, total number of working days in the semester and Number of days present. Calculate attendance percentage of the students and award attendance mark based on the following condition. Attendance percentage >=90 – 5 MarksAttendance percentage >=80 and < 90 – 4 MarksAttendance percentage >=75 and < 80 – 3 MarksAttendance percentage < 75 - 0 MarksImplement using inheritance
Check the following Java program carefully. What should be written in the blank? Choose from the given options. public class Test { public static void main(String[] args) { System.out.print("The grade is " + getGrade(78.5)); System.out.print("\nThe grade is " + getGrade(59.5)); } public static ________ getGrade(double score) { if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B'; else if (score >= 70.0) return 'C'; else if (score >= 60.0) return 'D'; else return 'F'; }}Group of answer choicesvoidStringintchar
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.