Consider the following class Grade: public class Grade { private char letterGrad
ID: 655009 • Letter: C
Question
Consider the following class Grade:
public class Grade
{
private char letterGrade;
public Grade( char 1g )
{
letterGrade = 1g;
}
public char getLetterGrade ( )
{
return letterGrade;
}
public void setletterGrade{ char 1g )
{
letterGrade = 1g;
}
}
Question:
In the main method of the class GradeClient you have coded
Grade g = new Grade("A"); // line 10
When you compile, you get the following message:
GradeClient.java:10: constructor Grade in class Grade cannot be applied
to given types
Grade g = new Grade ("A"); // line 10
^
required: char
found: String
1 error
Explain what the problem is and how to fix it
This is programming in java and questions are in bold thank you
Explanation / Answer
The compilation error indicates the class Grade does not have a constructor that takes in an argument of type "String". So, while trying to instantiate the class Grade by calling a constructor, it is expecting a parameter of type "char" as per the code.
Fix:
Grade g = new Grade('A'); //Pass 'A' as a char and not as "A" which is treated as a String
Corrected code here here:
public class Grade
{
private char letterGrade;
public Grade(char lg)
{
letterGrade = lg;
}
public char getLetterGrade( )
{
return letterGrade;
}
public void setletterGrade(char lg)
{
letterGrade = lg;
}
public static void main(String args[]) {
Grade grade = new Grade('A');
System.out.println("The grade is: "+grade.getLetterGrade());
}
}
Output:The grade is: A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.