分享
 
 
 

一道JAVA作业题

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

学JAVA的时候做的一道课后作业题,请打分!

Assignment:

This assignment involves writing a program, which simulates fishing. You must model the following situation: There are 4 fish species in the river, which you may catch:

Golden Perch - commonly less than 5 Kg; excellent eating, when between 1 and 2 Kg.

Silver Perch - commonly less than 7 Kg; excellent eating, when less then 1.5 Kg.

River Blackfish - Up to 5.5 kg; a superb eating fish with soft white flesh

Murray Cod - commonly less then 12 Kg; excellent eating when up to 7 Kg

For simplicity of the model, we assume that the chances to catch a fish of each of these four species are equal.

We also assume that within each species weights of fishes appear with equal chances, and range from 0 to the Maximal Weight. Maximal Weight for each of the species is specified in the species description given above.

You start fishing with an empty basket. If a caught fish¡¯s weight is within the recommended table range (specified above), you put the fish in the basket, otherwise release it. Fishes of weights less then 0.5 Kg also are released. Stop fishing as soon as the total weight of all the caught fishes exceeds 15 Kg.

To model the situation, you are supposed to use the random() method from the Math class. Fishes of different species must be implemented as objects of different classes, each extending an abstract class Fish. Whenever branching logic is required to handle fish objects of different species, you must do it polymorphically.

Task A

Create a design for your program from the following specifications:

The program requires you to create an abstract class called Fish. It can have the following class (minus constructor, which you will be required to supply).

Fish

protected double weight;

public abstract boolean acceptable()

public abstract void setWeight();

public double getWeight()

public abstract String getName()

public String toString()

The program also requires you to create four concrete classes ¨C GoldenPerch, SilverPerch, RiverBlackFish, MurrayCod, which inherit from the Fish class. These classes could have the following class diagrams.

GoldenPerch

SilverPerch

RiverBlackFish

MurrayCod

private static final String NAME = "Golden Perch";

private static final double MAX_WEIGHT = 5.0;

private static final String NAME = "Silver Perch";

private static final double MAX_WEIGHT = 7.0;

private static final String NAME = "River Black Fish";

private static final double MAX_WEIGHT = 5.5;

private static final String NAME = "Murray Cod";

private static final double MAX_WEIGHT = 12.0;

public boolean acceptable()

public void setWeight();

public String getName()

public boolean acceptable()

public void setWeight();

public String getName()

public boolean acceptable()

public void setWeight();

public String getName()

public boolean acceptable()

public void setWeight();

public String getName()

Task B

Create a class Angling,£¬ which randomly creates objects of GoldenPerch, SilverPerch, RiverBlackFish, MurrayCod types, and if suitable puts them into the basket, i.e.writes them to the basket.dat file. To write objects to the file you should use writeObject() method of ObjectOutputStream class (notice, that the Fish class must implement Serializable interface).

Current information about what is going on (i.e. what kind of fish was caught, what it¡¯s weight, was it put into the basket or released) must be printed to the BlueJ terminal window.

Task C

Create a class PrintBasket, which opens the file basket.dat, then reads objects from it one by one. It should then print that information to the BlueJ terminal window. Output should look like the following:

Golden Perch: 1.40 Kg

Golden Perch: 1.17 Kg

Golden Perch: 1.00 Kg

Golden Perch: 1.65 Kg

Golden Perch: 1.08 Kg

River Black Fish: 1.46 Kg

Murray Cod: 3.14 Kg

River Black Fish: 3.83 Kg

Murray Cod: 6.55 Kg

All created classes should be in a package called yourStudentId followed by Ass1. For example, UB2000345 would create a package called UB2000345Ass1.

My Answer Sheet:

Fish.java

package UB2000345Ass1;

import JAVA.io.*;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public abstract class Fish {

protected double weight;

public Fish() {

}

public abstract boolean acceptable();

public abstract void setWeight(double w);

public double getWeight() {

return this.weight;

}

public abstract String getName();

private void writeObject(ObjectOutputStream out) {

}

public String toString() {

return "This Fish's weight is:" + this.weight;

}

}

GoldenPerch.java

package UB2000345Ass1;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public class GoldenPerch

extends Fish {

private static final String NAME = "Golden Perch";

private static final double MAX_WEIGHT = 5.0;

public GoldenPerch(double w) {

setWeight(w);

}

public boolean acceptable() {

if ( (this.weight > 1) && (this.weight < 2)) {

return true;

}

else {

return false;

}

}

public void setWeight(double w) {

this.weight = w;

}

public String getName() {

return NAME;

}

}

SilverPerch.java

package UB2000345Ass1;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public class SilverPerch

extends Fish {

private static final String NAME = "Silver Perch";

private static final double MAX_WEIGHT = 7.0;

public SilverPerch(double w) {

setWeight(w);

}

public boolean acceptable() {

if ( (this.weight >= 0.5) && (this.weight < 1.5)) {

return true;

}

else {

return false;

}

}

public void setWeight(double w) {

this.weight = w;

}

public String getName() {

return NAME;

}

}

RiverBlackFish.java

package UB2000345Ass1;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public class RiverBlackFish

extends Fish {

private static final String NAME = "River Black Fish";

private static final double MAX_WEIGHT = 5.5;

public RiverBlackFish(double w) {

setWeight(w);

}

public boolean acceptable() {

if (this.weight >= MAX_WEIGHT) {

return true;

}

else {

return false;

}

}

public void setWeight(double w) {

this.weight = w;

}

public String getName() {

return NAME;

}

}

MurrayCod.java

package UB2000345Ass1;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public class MurrayCod

extends Fish {

private static final String NAME = "Murray Cod";

private static final double MAX_WEIGHT = 12.0;

public MurrayCod(double w) {

setWeight(w);

}

public boolean acceptable() {

if ( (this.weight >= 7) && (this.weight < MAX_WEIGHT)) {

return true;

}

else {

return false;

}

}

public void setWeight(double w) {

this.weight = w;

}

public String getName() {

return NAME;

}

}

Angling.java

package UB2000345Ass1;

import JAVA.io.*;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public class Angling {

public float totalWeight = 0;

public double rWeight;

public int rFishing;

public Angling() {

}

public static double getRandom() {

return JAVA.lang.Math.random();

}

public Fish getRandomFish() {

rFishing = (int) JAVA.lang.Math.ceil(getRandom() * 4);

Fish fish = null;

switch (rFishing) {

case 1:

fish = new GoldenPerch(rWeight);

break;

case 2:

fish = new SilverPerch(rWeight);

break;

case 3:

fish = new RiverBlackFish(rWeight);

break;

case 4:

fish = new MurrayCod(rWeight);

break;

}

return fish;

}

public String doFishing() {

int i = 0;

String str = "Begin Fishing:\n";

do {

i++;

rWeight = JAVA.lang.Math.ceil(getRandom() * 120) / 10;

Fish fish = getRandomFish();

if (fish.acceptable()) {

totalWeight += rWeight;

str += i + "." + fish.getName() + ":" + rWeight + "Kg,put in basket.\n";

}

else {

str += i + "." + fish.getName() + ":" + rWeight + "Kg,release it.\n";

}

}

while (totalWeight < 15);

str += "\nTotal weight:" + totalWeight +

"Kg,bigger than 15Kg,Stop Fishing.\n";

return str;

}

public void writeFile(String str) {

try {

FileOutputStream f = new FileOutputStream("basket.dat");

ObjectOutputStream s = new ObjectOutputStream(f);

s.writeObject(str);

s.close();

}

catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

Angling angling = new Angling();

angling.writeFile(angling.doFishing());

}

}

PrintBasket.java

package UB2000345Ass1;

import JAVA.io.*;

/**

* <p>Title: Fish</p>

* <p>Description: Fishing Class</p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: Longware Studio</p>

* @author Longware

* @version 1.0

*/

public class PrintBasket {

public String strFileName = "basket.dat";

public PrintBasket() {

}

public void readFile() {

try {

String text = null;

FileReader fr = new FileReader(strFileName);

BufferedReader br = new BufferedReader(fr);

text = new String();

while ( (text = br.readLine()) != null) {

System.out.println(text);

}

br.close();

fr.close();

}

catch (IOException e) {

System.out.println("File Read Error!");

}

}

public static void main(String[] args) {

PrintBasket printBasket1 = new PrintBasket();

printBasket1.readFile();

}

}

[ END ]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有