家庭作业不会……60分求解

suxing1982 2003-04-24 01:09:25
两个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;
}



}


...全文
80 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuhandong125 2003-04-27
  • 打赏
  • 举报
回复
不好意思 我不会 来给你捧个场吧
cowboy1114 2003-04-25
  • 打赏
  • 举报
回复
不明白还可以发短消息给我
cowboy1114 2003-04-25
  • 打赏
  • 举报
回复
//---------------------interface Backpacker-------------------------

/**
* This is a family backpacker accomodation.
*
* @author (Uncle Dave)
* @version (March 27, 2003)
*/
import java.util.*;

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
*/
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;
}

//我添加了这个函数判断两个Family对象是否相等
//是不是只要判断lastName就可以了?
//不合要求的话可以自行修改
public boolean equals(Object o)
{
Family temp=(Family)o;
if(o.lastName.equals(lastName))
return true;
else
return false;
}

}

-------------------------class MyBackpacker------------------------

/**
* must implement the Backpacker interface.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyBackpacker implements Backpacker
{
//分配100个Family对象空间给familyCapactiy
private Family familyCapacity=new Family[100];

//familyCapactiy的上限,初始值为50
private int familyMaxCount=50;

//当前familyCapacity中的Family对象个数
private int familyCount=0;



public MyBackpacker ()
{

}

public String setMaxOccupants (int max)
{
if (( max > 0 ) && ( max < 100 ))
{
familyMaxCount=max;
return "OK";
}
else
{
return "ERROR";
}
}

public int getMaxOccupants()
{
return familyMaxCount;
}

public int getNumOccupants()
{
reurn familyCount;
}

String increaseNumOccupants(Family family)
{
if(familyCount<familyMaxCount)
{
familyCapacity[familyCount]=family;
familyCount++;
return "OK";
}
else
{
return "ERROR";
}


}

String decreaseNumOccupants (Family family)
{
Iterator itr=familyCapacity.iterator();
String modified="ERROR";
while(itr.hasNext())
{
Family temp=(Family)ite.next();
if(temp.equals(family))
{
modified="OK";
itr.remove()
}
}

return modified;

}



}
suxing1982 2003-04-25
  • 打赏
  • 举报
回复
………………………………
suxing1982 2003-04-24
  • 打赏
  • 举报
回复
没有哪个大侠出来给点idea吗?

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧