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?
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 )
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’ );
Airplane.foo3( 34.6 );
From this, reconstruct the header of method foo3 (which belongs to the class Airplane); make appropriate assumptions if necessary.
o static void foo3();
o static void foo3(double value);
o public void static foo3(double value);
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 );
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:
Until user specifically set value of constant in the enum class.
// declare grade here // declare letterGrade here
int grade;
char letterGrade;
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.
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.
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.
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.
public int numDigits()
{
int temp=number;
int count=0;
while(temp!=0)
{
count++;
temp=temp/10;
}
return count;
}
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.
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();
}
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.
public String toString()
{
return “Name : “+name
+” Number : “+number
+” Cable : “+cable;
}
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.
public void setName(String n) { name=n;}
public void setNumber(int num) {number=num;}
public void setCable(boolean c) {cable=c;}