Chapter 7 - Identifying Errors Flashcards

1
Q

41. We are coding the following inside the class Gift; where is the error?

public boolean equals( Object g ) { return ( this == g ); }

A

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 );

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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;

A

The setTaxable method is not a void method; it takes a boolean argument. The
second statement should be g.setTaxable( true );​

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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 );
A
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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

43. We are coding the following inside the class Gift; where is the error?

public double calcTax( TAX_RATE )
{
return ( TAX_RATE * price );
}

A

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( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

42. We are coding the following inside the class Gift; where is the error?

public void setTaxRate( double newTaxRate )
{
TAX_RATE = newTaxRate;
}

A

TAX_RATE is a constant (final keyword); we cannot modify its value:

public void setTaxRate(double newTaxRate)

{

TAX_RATE=newTaxRate;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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 );
}

A

The toString method needs to return a String, not output data:

public String toString()

{

return“description = “+description+”price = “+price

+“occasion = “+occasion+”taxable = “+taxable;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

39. We are coding the following inside the class Gift; where is the error?

public void setOccasion( String occasion ) { occasion = occasion; }

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

38. We are coding the following inside the class Gift; where is the error?

public void getPrice( ) { return price; }

A

Accessor getPrice should have return type double, not void:

public double getprice() { return price; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

46. Where are the errors in the following statement?

enum Months = { “January”, “February”, “March” };

A

Correction:
enum Months{January,February,March};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly