JAVA练习题含答案-answers to pratice 4

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
return endTime;
}
public String getDayOfWeek()
{
return dayOfWeek;
}
public String getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
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
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
/**method to compare to integers for equality*/
public static boolean isEqual(int x, int y)
{
return x == y;
}
Discuss the public and private modifiers in context of methods and instance variables.
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
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.
month.equalsIgnoreCase("May") || month.equalsIgnoreCase("June") ||
month.equalsIgnoreCase("July") || month.equalsIgnoreCase("August") ||
month.equalsIgnoreCase("September") ||
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.
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.
d.equalsIgnoreCase("Thursday") || d.equalsIgnoreCase("Friday") ||
d.equalsIgnoreCase("Saturday") || d.equalsIgnoreCase("Sunday"))
return true;
else
return false;
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.
Normal good programming practices require that all instance variables be private and typically most methods be public.
Create a class named Appointment that contains instance variables startTime, endTime, dayOfWeek (valid values are Sunday through Saturday), and a date which consists of a month, day and year.All times should be in military time, therefore it is appropriate to use integers to represent the time.Create the appropriate accessor and mutator methods.
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*/
/**x and n arenonnegativeintegers*/
public long power(int x, int n)
{
long result = 1;
/**check for positive numbers*/
if((x >= 0) && (n >= 0))
{
/**raise x to the nth power*/
{
/**valid range for military time is 0-2400*/
if((et >= 0) && (et <= 2400))
endTime = et;
}
public void setDayOfWeek(String dow)
{
/**Valid values are the strings Sunday–Saturday*/
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*/
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
The modifiers public and private before a method definition have a similar meaning. If the method is labeled public, there are no restrictions on its usage. If the method is labeled private, the method can only be used in the definition of another method of the same class.
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)
protected
private
all of the above
A
13)
an accessor method
a mutator method
a recursive method
none of the above
A
14)
return
promotes abstraction
both A and B
none of the above
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.
if((d >= 1) && (d <= 31))
day = d;
}
public void setYear(int y)
{
if(y >= 0)
year = y;
}
/**Accessor methods*/
public int getStartTime()
{
return startTime;
}
public int getEndTime()
public class Appointment
{
privateint startTime;
privateint endTime;
privateString dayOfWeek;
privateString month;
privateint day;
privateint year;
/**Mutator methods*/
}
private boolean checkMonth(String month)
{
if(month.equalsIgnoreCase("January") || month.equalsIgnoreCase("February") ||
month.equalsIgnoreCase("March") || month.equalsIgnoreCase("April") ||
相关文档
最新文档