Consider the following class Grade: public class Grade { private char letterGrad
ID: 655008 • 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{ 'B' ) ;
g.letterGrade = 'A ' ; // line 10
When you compile, you get the follow ing message:
GradeClient.java:l0: letterGrade has private access in Grade
g.letterGrade = 'A';
^
1 error
Explain what the problem is and how to fix it.
This is programming in Java the actual quesiton is in bold thank you
Explanation / Answer
Solution:
//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.
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());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.