Chapter 7 - Understanding Code Flashcards
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?
• 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.
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?
- 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.
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?
foo1 = double foo2 = String
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 )
- Write a line or two of code to call method foo1 from a client class.
foo1 = class (since static used) foo2 = instance (since static not used)
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..
double d = Airplane.foo1( “Boeing” );
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.
String s = a1.foo2( ‘A’ );
- 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.
- 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);
- 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).
public int foo4( String s );
- 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( ) );
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.
- Declare two instance variables: grade, which is an integer, and letterGrade, which is a char.
// declare grade here // declare letterGrade here
int grade;
char letterGrade;
- Declare a class field for a federal tax rate, a constant, with value 0.07.
public static final double FEDERAL_TAX_RATE=0.07;
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.
- Code a default constructor for that class: initialize the fields to an empty string, 0, and false, respectively.
public TelevisionChannel()
{
//set empty string to name variable
name=””;
//set 0 to number variable
number=0;
//set false to cable
cable=false;
}
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.
- Code a constructor for that class that takes three parameters.
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;
}
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.
- 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.
public String isCableOrNetwork()
{
//using conditional operator, ?:
//checking cable boolean value is true, return cable
//otherwise return Network
return cable==true? “Cable”: “Network”;
}
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.
- 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.
public int numDigits()
{
int temp=number;
int count=0;
while(temp!=0)
{
count++;
temp=temp/10;
}
return count;
}