家庭作业不会……60分求解
两个class,分别叫Family和MyBackpacker,一个interface,叫Backpacker。
class MyBackpacker和interface Backpacker两者,与class Family之间的
关系均为uses。class MyBackpacker与interface Backpacker之间的关系是
inheritance。
两个class都是未完成,interface不能作改动。老师的要求是,完成class
Family,使其正常工作,另外完成class MyBackpacker,使其implements
interface Backpacker。我已经写完了class Family,但不知道对不对,
而class Mybackpacker,写了点,实在是写不下去了。
所有的代码如下,大侠们要有时间,就帮我改改、完成吧,先写了。
Code:
---------------------interface Backpacker-------------------------
/**
* This is a family backpacker accomodation.
*
* @author (Uncle Dave)
* @version (March 27, 2003)
*/
public interface Backpacker
{
//setters
String setMaxOccupants(int max); //set number of maximum occupants
// must be > 0 and < 100
// return "OK" or "ERROR"
//getters
int getMaxOccupants(); //get maximum number of occupants
int getNumOccupants(); //get number of current occupants
//other methods
String increaseNumOccupants (Family family); //Increase the number of occupants
//The backpacker must have sufficient capacity to
// add the enrire family
//This method updates the details of the current occupants
// if any error occurs, return "ERROR" or "OK"
String decreaseNumOccupants (Family family); //Decrease the number of occupants
//The entire family must be deleted for any change to occur
//This method updates the details of the current occupants
// if any error occurs, return "ERROR" or "OK"
}
----------------------------class Family--------------------------------
/**
* Room accomodation
*
* @author
* @version
*/
public class Family
{
private String lastName;
private int numPeople;
//null constructor
public Family()
{
lastName = "";
numPeople = 0;
}
//full constructor
public Family(String lastName, int numPeople)
{
setLastName(lastName);
setNumPeople(numPeople);
}
//setters
//length of lastName must be >0 and <10
//lastName must not contain a space
// return "OK" or "ERROR"
public String setLastName(String lastName)
{
int spaceLoc = lastName.indexOf(" ");
int len;
len = lastName.length();
if (( len <= 0 ) || ( len >= 10 ))
{
return "ERROR";
}
else
{
if ( spaceLoc == -1 )
{
return "ERROR";
}
else
{
return "OK";
}
}
}
//number of people must be >0 and <75
// return "OK" or "ERROR"
public String setNumPeople(int numPeople)
{
this.numPeople = numPeople;
if (( numPeople <= 0) || ( numPeople >= 75 ))
{
return "ERROR";
}
else
{
return "OK";
}
}
//getters
public String getLastName()
{
return lastName;
}
public int getNumPeople()
{
return numPeople;
}
}
-------------------------class MyBackpacker------------------------
/**
* must implement the Backpacker interface.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyBackpacker implements Backpacker
{
public MyBackpacker ()
{
}
public String setMaxOccupants (int max)
{
if (( max > 0 ) && ( max < 100 ))
{
return "OK";
}
else
{
return "ERROR";
}
}
public int getMaxOccupants()
{
return max;
}
public int getNumOccupants()
{
reurn numOccupants;
}
}