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.
Fish.java |
package UB2000345Ass1; import JAVA.io.*; /** public abstract class Fish { public Fish() { public abstract boolean acceptable(); public abstract void setWeight(double w); public double getWeight() { public abstract String getName(); private void writeObject(ObjectOutputStream out) { public String toString() { |
GoldenPerch.java |
package UB2000345Ass1; /** public class GoldenPerch public GoldenPerch(double w) { public boolean acceptable() { if ( (this.weight > 1) && (this.weight < 2)) { public void setWeight(double w) { public String getName() { } |
SilverPerch.java |
package UB2000345Ass1; /** public class SilverPerch public SilverPerch(double w) { public boolean acceptable() { public void setWeight(double w) { public String getName() { |
RiverBlackFish.java |
package UB2000345Ass1; /** public class RiverBlackFish public RiverBlackFish(double w) { public boolean acceptable() { public void setWeight(double w) { public String getName() { } |
MurrayCod.java |
package UB2000345Ass1; /** public class MurrayCod public MurrayCod(double w) { public boolean acceptable() { public void setWeight(double w) { public String getName() { } |
Angling.java |
package UB2000345Ass1; import JAVA.io.*; /** public class Angling { public Angling() { public static double getRandom() { public Fish getRandomFish() { switch (rFishing) { public String doFishing() { if (fish.acceptable()) { str += "\nTotal weight:" + totalWeight + public void writeFile(String str) { public static void main(String[] args) { |
PrintBasket.java |
package UB2000345Ass1; import JAVA.io.*; /** public class PrintBasket { public PrintBasket() { public void readFile() { FileReader fr = new FileReader(strFileName); br.close(); public static void main(String[] args) { } |
[ END ]