- 1、本文档共4页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第四章习题2答案
1.下列application应用右边的类定义代码。借助application的代码完成右边的类定义框架:
2.创建一个Order类并设计一个application进行测试. Order类要完成以下的工作:
add a certain number of coffees to the order (cost of a coffee $3)
add a certain number of cakes to the order (cost of a cake $4)
set the order to paid
obtain a description of each order
obtain the total cost of the order
以下的例子是当创建2个Order对象时,测试时的输出结果:
3.为MyPoint类补写 equals() 方法:
完成测试equals() method的程序:
4.写出下列程序执行的结果:
public class GG {
private int age;
private String name;
private int wins, losses;
public GG(String s,int a){…}
public boolean isReadyToRace(){
… }
public String toString(){
… }
public void addToAge(int n){
… }
public void addToWins(int n){
… }
public void addToLosses(int n){
… }
public int getTotalRaces(){
… }
}
public class UsesOrder{
public static void main(String[] args){
Order o1, o2;
o1 = new Order(2, 2);
o2 = new Order(2, 1);
o1.addCoffees(1);
o2.addCakes(3);
o1.addCoffees(1);
o1.setIsPaid(true);
o2.setIsPaid(false);
System.out.println(o1.toString());
System.out.println(o2.toString());
}
}
}
}
public class Order{
numCoffees = nCoff;
numCakes = nCakes;
}
public void addCoffees(int numToAdd){
numCoffees += numToAdd;
}
public void addCakes(int numToAdd){
numCakes += numToAdd;
}
public void setIsPaid(boolean paid){
isPaid = paid;
}
public int getTotal(){
return numCoffees*3 + numCakes*4;
}
public String toString(){
String orderInfo = “New Order: \n”;
orderInfo += numCoffees+“ Coffees $”+ numCoffees*3+“\n”;
orderInfo += numCakes + “ Cakes $”+ numCakes*4+“\n”;
orderInfo += “ Total $”+ getTotal() +“ ”;
if (isPaid)
orderInfo += “ has been paid \n”;
else
orderInfo += “ not paid \n”;
return orderInfo;
}
}
public class MyPoint{
private int x;
private int y;
public MyPoint(int xValue, int yValue){
x = xValue;
y = yValue;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public String toString(){
return “(” + x + “,” + y + “)”;
}
public boolean equals(MyPoint otherPt){
if (x == otherPt.x y== oth
文档评论(0)