Chapter 10 - Fill In the Code Flashcards
For Questions 21 to 25, consider the following class F and the interface I:
public class F
{
private String first;
protected String name;
public F( ) { }
public F( String first, String name ) { this.first = first; this.name = name; } public String getFirst( ) { return first; } public String getName( ) { return name; } public String toString( ) { return "first: " + first + "\tname: " + name ; } public boolean equals( Object f ) { if ( ! ( f instanceof F ) ) return false; else { F objF = ( F ) f; return( first.equals( objF.first ) && name.equals( objF.name ) ); } } } public interface I { String TYPE = "human"; int age( ); }
- The G class inherits from the F class. Code the class header of the G class.
// your code goes here
public class K extends F {}
For Questions 21 to 25, consider the following class F and the interface I:
public class F
{
private String first;
protected String name;
public F( ) { }
public F( String first, String name ) { this.first = first; this.name = name; } public String getFirst( ) { return first; } public String getName( ) { return name; } public String toString( ) { return "first: " + first + "\tname: " + name ; } public boolean equals( Object f ) { if ( ! ( f instanceof F ) ) return false; else { F objF = ( F ) f; return( first.equals( objF.first ) && name.equals( objF.name ) ); } } } public interface I { String TYPE = "human"; int age( ); }
- Inside the G class, which inherits from the F class, declare a private instance variable for the middle initial and code a constructor with three parameters, calling the constructor of the F class and assigning the third parameter, a char, to the new instance variable.
// your code goes here
private char middle; public G( String f, String n, char middle ) { super( f, n ); this.middle = middle; }
For Questions 21 to 25, consider the following class F and the interface I:
public class F
{
private String first;
protected String name;
public F( ) { }
public F( String first, String name ) { this.first = first; this.name = name; } public String getFirst( ) { return first; } public String getName( ) { return name; } public String toString( ) { return "first: " + first + "\tname: " + name ; } public boolean equals( Object f ) { if ( ! ( f instanceof F ) ) return false; else { F objF = ( F ) f; return( first.equals( objF.first ) && name.equals( objF.name ) ); } } } public interface I { String TYPE = "human"; int age( ); }
- Inside the G class, which inherits from the F class, code the toString method, which returns a printable representation of a G object reference.
// your code goes here
public class G extends F {
public String toString( )
{
return ( super.toString( ) + “\tmiddle: “ + middle );
}
}
For Questions 21 to 25, consider the following class F and the interface I:
public class F
{
private String first;
protected String name;
public F( ) { }
public F( String first, String name ) { this.first = first; this.name = name; } public String getFirst( ) { return first; } public String getName( ) { return name; } public String toString( ) { return "first: " + first + "\tname: " + name ; } public boolean equals( Object f ) { if ( ! ( f instanceof F ) ) return false; else { F objF = ( F ) f; return( first.equals( objF.first ) && name.equals( objF.name ) ); } } } public interface I { String TYPE = "human"; int age( ); }
- Inside the G class, which inherits from the F class, code the equals method, which compares two G objects and returns true if they have identical instance variables; false otherwise.
// your code goes here
public class G extends F
{
public boolean equals( Object obj ) { if ( ! ( obj instanceof G ) ) return false; else { G g = (G) obj; return ( super.equals( g ) && middle == g.middle ); } } }
For Questions 21 to 25, consider the following class F and the interface I:
public class F
{
private String first;
protected String name;
public F( ) { }
public F( String first, String name ) { this.first = first; this.name = name; } public String getFirst( ) { return first; } public String getName( ) { return name; } public String toString( ) { return "first: " + first + "\tname: " + name ; } public boolean equals( Object f ) { if ( ! ( f instanceof F ) ) return false; else { F objF = ( F ) f; return( first.equals( objF.first ) && name.equals( objF.name ) ); } } } public interface I { String TYPE = "human"; int age( ); }
- The K class inherits from the F class and the I interface; code the class header of the K class.
// your code goes here
public class K extends F implements I