Chapter 10 - Fill In the Code Flashcards

1
Q

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( );
 }
  1. The G class inherits from the F class. Code the class header of the G class.

// your code goes here

A

public class K extends F {}

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

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( );
 }
  1. 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

A
private char middle;
public G( String f, String n, char middle )
{
  super( f, n );
  this.middle = middle;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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( );
 }
  1. 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

A
public class G extends F
{

public String toString( )
{
return ( super.toString( ) + “\tmiddle: “ + middle );
}

}

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

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( );
 }
  1. 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

A

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 );
 }
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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( );
 }
  1. The K class inherits from the F class and the I interface; code the class header of the K class.

// your code goes here

A

public class K extends F implements I

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