Chapter 7 - Identifying Errors Flashcards
41. We are coding the following inside the class Gift; where is the error?
public boolean equals( Object g ) { return ( this == g ); }
The expression return( this == g ) is incorrect because we are
comparing the object references, not the object data. The equals method should be written as follows:
public boolean equals( Object o )
{
if ( ! ( o instanceof Gift ) )
return false;
else
{
Gift g = ( Gift ) o;
return ( description.equals( g.description )
&& price == g.price && occasion.equals( g.occasion )
&& taxable == g.taxable );
}
}
45. We are coding the following in the main method inside the class GiftClient; where is the error?
Gift g = new Gift( “radio”, 59.99, “Birthday”, false );
g.setTaxable( ) = true;
The setTaxable method is not a void method; it takes a boolean argument. The
second statement should be g.setTaxable( true );
44. We are coding the following in the main method inside the class GiftClient; where is the error?
Gift g = new Gift( "radio", 59.99, "Birthday", false ); Gift.setPrice( 99.99 );
The setPrice method is an instance method, not a static method. It should be called by a Gift object reference such as g, not by the class Gift:
Gift g=new Gift(“radio”, 59.99, “Birthday”, false);
g.setPrice(99.99);
43. We are coding the following inside the class Gift; where is the error?
public double calcTax( TAX_RATE )
{
return ( TAX_RATE * price );
}
The method header should be
public double calcTax( double TAX_RATE )
However, because TAX\_RATE is an instance variable, the Gift class can access TAX\_RATE directly, so there is no need to send the value as a parameter.
The method header could simply be
public double calcTax( )
42. We are coding the following inside the class Gift; where is the error?
public void setTaxRate( double newTaxRate )
{
TAX_RATE = newTaxRate;
}
TAX_RATE is a constant (final keyword); we cannot modify its value:
public void setTaxRate(double newTaxRate)
{
TAX_RATE=newTaxRate;
}
40. We are coding the following inside the class Gift; where is the error?
public String toString( )
{
System.out.println( “description = “ + description );
System.out.println( “price = “ + price );
System.out.println( “occasion = “ + occasion );
System.out.println( “taxable = “ + taxable );
}
The toString method needs to return a String, not output data:
public String toString()
{
return“description = “+description+”price = “+price
+“occasion = “+occasion+”taxable = “+taxable;
}
39. We are coding the following inside the class Gift; where is the error?
public void setOccasion( String occasion ) { occasion = occasion; }
The code compiles but does not do anything. The statement inside this mutator
method should be modified to say this.occasion = occasion;
Alternatively, the mutator could be recoded as follows:
public void setOccasion( String newOccasion )
{
occasion = newOccasion;
}
38. We are coding the following inside the class Gift; where is the error?
public void getPrice( ) { return price; }
Accessor getPrice should have return type double, not void:
public double getprice() { return price; }
46. Where are the errors in the following statement?
enum Months = { “January”, “February”, “March” };
Correction:
enum Months{January,February,March};