当前位置:
文档之家› JAVA练习题含答案-answers to pratice 4
JAVA练习题含答案-answers to pratice 4
if(checkDayOfWeek(dow))
dayOfWeek = dow;
else
System.out.println("Fatal error....invalid day of week value!");
}
public void setMonth(String m)
{
/**Valid values are strings January–December*/
It is considered good programming practice to make all instance variables private, but there are times when the values of the instance variables need to be accessed or updated. Accessor methods allow you to obtain the data, while mutator methods allow you to change the data in a class object. A mutator method can verify the validity of a value before updating the instance variable.
public class Appointment
{
privateint startTime;
privateint endTime;
privateString dayOfWeek;
privateString month;
privateint day;
privateint year;
/**Mutator methods*/
A
15)
int
char
boolean
double
C
16)
prescript
postscript
precondition
postcondition
A
17)
parameter
argument
return
primitive
A
2
1)
A
2)
A
3)
A
4)
A
5)
A
6)
A
7)
A
8)
A
9)
A
10)
11)
A
3
Writea methodcalled power the computesxnwhere x and n and positive integers. The method has two integer parameters and returns a value of type long.
}
private boolean checkMonth(String month)
{
if(month.equalsIgnoreCase("January") || month.equalsIgnoreCase("February") ||
month.equalsIgnoreCase("March") || month.equalsIgnoreCase("April") ||
{
return year;
}
/**Facilitator methods*/
private boolean checkDayOfWeek(String d)
{
if(d.equalsIgnoreCase("Sunday") || d.equalsIgnoreCase("Monday") ||
d.equalsIgnoreCase("Tuesday") || d.equalsIgnoreCase("Wednesday") ||
this
A
10)
getName and setName
toString and equals
compareTo and charAt
toLowerCase and toUpperCase
A
11)
driver program
stub
bottom-up test
recursive method
A
12)
public
month.equalsIgnoreCase("October") ||
month.equalsIgnoreCase("November") || month.equalsIgnoreCase("December"))
return true;
else
return false;
}
}
Discuss the importance of accessor and mutator methods and how they apply to the abstraction concept.
public void setStartTime(int st)
{
/**valid range for military time is 0-2400*/
if((st >= 0) && (st <= 2400))
startTime = st;
}
public void setEndTime(int et)
if(checkMonth(m))
month = m;
else
System.out.println("Fatal error...invalid month value!");
}
public void setDay(int d)
{
/**Valid days in a date are the integers 1–31*/
The modifiers public and private can both be used with methods and instance variables. Any instance variable can be labeled either public or private. The modifier public means that there are no restrictions on where the instance variable can be used. The modifier private means that the instance variable cannot be accessed by name outside of the class definition.
Chapter
1
1)
allocates memory
is used to create an object of a class
associates an object with a variable that names it.
All of the above.
A
2)
null
void
public
private
A
6)
( )
/* */
“ “
{ }
A
7)
objects
primitive types
this
all of the above
A
8)
instance variables
local variables
global variables
the calling object
A
9)
String
hidden
default
{
return endTime;
}
public String getDayOfWeek()
{
return dayOfWeek;
}
public String getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
A
3)
void
invocation
thows
return
A
4)
instance variable
local variable
global variable
none of the above
A
5)
instance variable
local variable
global variable
none of the above
for(int i = n; i > 0; i--)
result *= x;
}
else
{
result = 0;
System.out.println("Fatal error.......positive integers required!");
}
return result;
}
Write a method called isEqual that returns a Boolean value. The method compares two integers for equality.
protected
private
all of the above
A
13)
an accessor method
a mutator method
a recursive method