Chapter 7 - Understanding Code Flashcards

1
Q

For Questions 17 and 18, consider that inside the class Sky, we have already coded the following:

public class Sky

{

private Color color;

public Sky( Color c )

{

color = c;

}

}

17. Consider the following method header:
public Color getColor( )
Is this method a constructor, mutator, or accessor?

A

• The method getColor doesn’t take any argument and returns the Color object
• So getColor method is an accessor method of Sky class.
• The getColor is not a constructor since constructor has same name as class name.
• The getColor is not mutator method since mutator methods takes arguments and sets value to
the corresponding field.
• Therefore, getColor is an accessor method of Sky class.

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

For Questions 17 and 18, consider that inside the class Sky, we have already coded the following:

public class Sky

{

private Color color;

public Sky( Color c )

{

color = c;

}

}

18. Consider the following method header:
public void setColor( Color c )​
Is this method a constructor, mutator, or accessor?

A
  • The method setColor takes Color argument and return void as return type.
  • So setColor method is a mutator method of Sky class.
  • The setColor is not a constructor since constructor has same name as class name.
  • The setColor is not accessor method since accessor doesn’t take argument.
  • Therefore, setColor is a mutator method of Sky class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

For Questions 19 through 24, consider that the class Airplane has two methods with the following method headers; we also have a default constructor already coded.

public static double foo1( String s )
public String foo2( char c )

19&20. What is the return type of method foo1 and foo2?

A
foo1 = double
foo2 = String
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

For Questions 19 through 24, consider that the class Airplane has two methods with the following method headers; we also have a default constructor already coded.

public static double foo1( String s )
public String foo2( char c )

  1. Write a line or two of code to call method foo1 from a client class.
A
foo1 = class (since static used)
foo2 = instance (since static not used)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For Questions 19 through 24, consider that the class Airplane has two methods with the following method headers; we also have a default constructor already coded.

public static double foo1( String s )
public String foo2( char c )

23. Write a line or two of code to call method foo1 from a client class..

A

double d = Airplane.foo1( “Boeing” );

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

For Questions 19 through 24, consider that the class Airplane has two methods with the following method headers; we also have a default constructor already coded.

public static double foo1( String s )
public String foo2( char c )

24. Write a line or two of code to call method foo2 from a client class. Assume we have instantiated an object named a1.

A

String s = a1.foo2( ‘A’ );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Inside method main, we see code like

Airplane.foo3( 34.6 );

From this, reconstruct the header of method foo3 (which belongs to the class Airplane); make appropriate assumptions if necessary.

A
  • From the statement, it is said that the Airplane is the name of the class.
  • The class name, Airplane itself calling the method foo3 with an argument of type 34.6.
  • Only static member methods can be called without using class object.
  • So, the method foo3 is static method and return type is void since it is not returning any value.

o static void foo3();

  • The argument to the static method is 34.6 is a double type value.
  • Therefore, argument type is double is passed to the static method foo3.

o static void foo3(double value);

  • Since it is called directly by the class name, it has public access specifier.
  • public static void foo3(double value);
  • Finally, the method header foo3 is as follows

o public void static foo3(double value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Inside method main, we see code like

Airplane a = new Airplane( );

int n = a.foo4( “Hello” );

From this, reconstruct the header of method foo4 (which belongs to class Airplane).

A

public int foo4( String s );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. If you have defined the following enum constants

enum Seasons { Winter, Spring, Summer, Fall };

what is the output of the following code sequence?

System.out.println( Seasons.Spring.ordinal( ) );

A

Output of the statement: 1

Explanation:

  • From the statement, Seasons is the name of enum constant.
  • The Enum class name allows accessing the constants declared.
  • By default , the enum class constant starts from 0 ,1,2 and continues

Until user specifically set value of constant in the enum class.

  • So that the constants in the Seasons enum class starts from Winter=0,Spring=1,Summer=2 and Fall=3.
  • The method ordinal that returns the integer constant value set to the Spring value.
  • Therefore, the Spring constant variable returns 1 when called ordinal method on the enum class Seasons.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Declare two instance variables: grade, which is an integer, and letterGrade, which is a char.
// declare grade here
// declare letterGrade here
A

int grade;
char letterGrade;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Declare a class field for a federal tax rate, a constant, with value 0.07.
A

public static final double FEDERAL_TAX_RATE=0.07;

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

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code a default constructor for that class: initialize the fields to an empty string, 0, and false, respectively.
A

public TelevisionChannel()

{

//set empty string to name variable

name=””;

//set 0 to number variable

number=0;

//set false to cable

cable=false;

}

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

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code a constructor for that class that takes three parameters.
A

Assume the following TelevisionChannel class declaration

//TelevisionChannel.java

public class TelevisionChannel

{

//name of type string

private String name;

//number of type integer

private int number;

//cable of type boolean

private boolean cable;

}

Parameterized TelevisionChannel constructor of TelevisionChannel class

/*Parameter constructor that set values

to class TelevisionChannel fields */

public TelevisionChannel(String n, int num, boolean c)

{

//set n to name field

name=n;

//set num to number variable

number=num;

//set c to cable boolean variable

cable=c;

}

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

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code a method returning the word cable if the current object represents a cable channel and returning the word network if the current object does not represent a cable channel.
A

public String isCableOrNetwork()

{

//using conditional operator, ?:

//checking cable boolean value is true, return cable

//otherwise return Network

return cable==true? “Cable”: “Network”;

}

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

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code a method returning the number of digits in the channel number. For instance, if the channel number is 21, the method returns 2; if the channel number is 412, the method returns 3.
A

public int numDigits()

{

int temp=number;

int count=0;

while(temp!=0)

{

count++;

temp=temp/10;

}

return count;

}

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

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code the equals method.
A

public boolean equals(Object object)

{

//cast object to TelevisionChannel

TelevisionChannel other=(TelevisionChannel)object;

//Returns true if other object is contains same values

//instance fields of this class.

return name.equals(other.getName()) &&
number==other.getNumber() &&
cable==other.getCable();

}

17
Q

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code the toString method.
A

public String toString()

{

return “Name : “+name

+” Number : “+number

+” Cable : “+cable;

}

18
Q

For Questions 30 through 37, we will assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel.

  1. Code the three mutators for that class.
A

public void setName(String n) { name=n;}

public void setNumber(int num) {number=num;}

public void setCable(boolean c) {cable=c;}