Chapter 7 - Debugging Flashcards
- When you compile, you get the following message:
GradeClient.java:10: error: letterGrade has private access in Grade
g.letterGrade = ‘A’; // line 10
^
1 error
Explain what the problem is and how to fix it.
letterGrade is a private instance variable of class Grade and cannot be accessed directly from another class. The mutator method should be used instead.
- When you compile, you get the following message:
GradeClient.java:10: error: incompatible types: String cannot be converted to char
Grade g = new Grade ( “A” ); // line 10
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Explain what the problem is and how to fix it.
There is no constructor in the Grade class taking a String as its only argument. The only constructor in the Grade class takes a char as its only argument.
- You coded the following definition for the class Grade:
public class Grade { private char letterGrade; public char Grade( char startLetter ) { letterGrade = startLetter; } // line 8 } When you compile, you get the following message:
Grade.java:8: error: missing return statement
} // line 8
^
1 error
Explain what the problem is and how to fix it.
The compiler assumes that Grade is a method since its header says it returns a
char. It looks as if it is intended to be a constructor so the keyword char should
be deleted from the constructor header.
- You coded the following definition for the class Grade:
public class Grade { private char letterGrade; public Grade( char lg ) { letterGrade = lg; } public String toString( ) // line 10 { // line 11 return letterGrade; // line 12 } // line 13 } When you compile, you get the following message:
Grade.java:12: error: incompatible types: char cannot be converted to String
return letterGrade; // line 12
^
1 error
Explain what the problem is and how to fix it.
The toString method should return a String, according to its header. Instead, it
returns letterGrade, which is a char. You could replace the return statement with
return “letterGrade: “ + letterGrade
- You coded the following definition for the Grade class:
public class Grade { private char letterGrade; public Grade( char lg ) { letterGrade = lg; } public String toString( ) // line 10 { // line 11 return lg; // line 12 } // line 13 } When you compile, you get the following message:
Grade.java:12: error: cannot find symbol return lg; // line 12 ^ symbol : variable lg location: class Grade 1 error Explain what the problem is and how to fix it.
The toString method uses the variable lg; lg is not declared inside toString and is therefore unknown (the variable lg used in the constructor is local to the constructor). You could replace the return statement with return "letterGrade: " + letterGrade;
- You coded the following definition for the Grade class:
public class Grade { private int numberGrade; public Grade( int numberGrade ) { numberGrade = numberGrade; } public int getGrade( ) { return numberGrade; } } In the main method of the GradeClient class, you have coded:
Grade g1 = new Grade( 95 ); System.out.println( g1.getGrade( ) ); The code compiles properly and runs, but the result is not what you expected.
The client’s output is 0, not 95.
Explain what the problem is and how to fix it.
The constructor assigns the parameter numberGrade to itself, therefore not changing the value of the instance variable, which by default is 0. The constructor could be recoded as follows: public Grade( int newNumberGrade ) { numberGrade = newNumberGrade; } Or as : public Grade( int numberGrade ) { this.numberGrade = numberGrade; }
- You have defined the following enum constants:
enum Seasons { Winter, Spring, Summer, Fall }; In the main method of the class Test, you have coded:
Seasons s = Seasons.Spring;
if ( s.equals( Winter ) ) // line 10
System.out.println( “It is cold” );
else
System.out.println( “The weather is fine” );
When you compile, you get the following message:
Test.java:10: error: cannot find symbol if ( s.equals( Winter ) ) // line 10 ^ symbol : variable Winter location: class Test 1 error Explain what the problem is and how to fix it.
The compiler thinks that Winter is a variable, but it has not been declared. If we
meant to use the constant object Winter from the set of objects Seasons, then the
if statement should be if ( s.equals( Seasons.Winter ) )
- You have defined the following enum constants:
enum Seasons { Winter, Spring, Summer, Fall }; In the main method of the class Test, you have coded
Seasons.Fall = Autumn; // line 10
When you compile, you get the following message:
Test.java:10: error: cannot assign a value to final variable Fall Seasons.Fall = Autumn; // line 10 ^ Test.java:10: cannot find symbol Seasons.Fall = Autumn; // line 10 ^ symbol : variable Autumn location: class Test 2 errors
Explain what the problem is and how to fix it.
enum objects are constants and cannot be assigned a value. Furthermore, the
compiler thinks Autumn is a variable and it has not been declared. To fix the
problem, delete the statement Seasons.Fall = Autumn;