Ask Java Expert


Home >> Java

Implement a Fish/Lake simulation similar to the previous assignment. You will then make adjustments to accommodate class hierarchies and make use of inheritance as well as a JAVA interface.

(1) The Basic Classes

Consider the following Fish, Lake and Fisher classes. Create a folder called Part1 and copy all the code into unique files into that folder and then compile them:

public class Fish {

// Any fish below this size must be thrown back into the lake

public static int THROW_BACK_SIZE = 18;

protected int size;

protected float weight;

public Fish(int aSize, float aWeight) {

size = aSize;

weight = aWeight;

}

public boolean isDesirableTo(Fisher f) {

// Replace the line below with your code

return false;

}

public boolean canKeep() {

// Replace the line below with your code

return false;

}

public int getSize() { return size; }

public float getWeight() { return weight; }

public String toString () {

return ("A " + size + "cm " + weight + "kg Fish");

}

}

______________________________________________________________________________

public class Lake {

private Fish[] catchableThings;

private int numThings;

public Lake(int capacity) {

catchableThings = new Fish[capacity];

numThings = 0;

}

public int getNumCatchableThings() { return numThings; }

public boolean isFull() { return numThings == catchableThings.length; }

public String toString() { return "Lake with " + numThings + " catchable things"; }

// Add the given thing to the lake

public void add(Fish aCatchableThing) {

if (numThings < catchableThings.length)

catchableThings[numThings++] = aCatchableThing;

}

// Choose a random thing to be caught in the lake and return it

public Fish catchSomething() {

if (numThings == 0) return null;

int index = (int)(Math.random() * numThings);

Fish f = catchableThings[index];

catchableThings[index] = catchableThings[numThings-1];

catchableThings[numThings-1] = null;

numThings--;

return f;

}

// List all things in the lake

public void listAllThings() {

System.out.println(" " + this + " as follows:");

for (int i=0; i

System.out.println(" " + catchableThings[i]);

System.out.println();

}

}

______________________________________________________________________________

public class Fisher {

public static int LIMIT = 10; // max # of fish that can be caught

private String name;

private Fish[] thingsCaught;

private int numThingsCaught;

private float weightLimit;

public Fisher(String aName, int wl) {

name = aName;

thingsCaught = new Fish[LIMIT];

numThingsCaught = 0;

weightLimit = wl;

}

public String getName() { return name; }

public int getNumThingsCaught() { return numThingsCaught; }

public float getWeightLimit() { return weightLimit; }

public String toString() {

return name + " with " + numThingsCaught + " things caught";

}

// List all the things caught and kept by this fisher

public void listThingsCaught() {

System.out.println(" " + this + " as follows:");

if (numThingsCaught > 0) {

for (int i=0; i

System.out.println(" " + thingsCaught[i]);

}

System.out.println(" -------------------------");

System.out.println(" Total fish weight: " + getWeightSoFar() + "kg");

System.out.println();

}

// Cause this fisher to keep the thing caught.

public void keep(Fish aThing) {

thingsCaught[numThingsCaught++] = aThing;

}

// Simulate the fisher catching something in the lake and keeping what is caught if

// it is desirable, otherwise throwing it back into the lake again.

public void goFishingIn(Lake aLake) {

Fish aThing = aLake.catchSomething();

if (aThing != null) {

if (aThing.isDesirableTo(this)) {

this.keep(aThing);

}

else {

aLake.add(aThing);

}

}

}

// Cause this fisher to give all fish to the given fisher, unless the given

// fisher exceeds the wight or catch limit, then throw the fish back into the lake.

public void giveAwayFish(Fisher f, Lake aLake) {

for (int i=0; i

if (thingsCaught[i].isDesirableTo(f))

f.keep(thingsCaught[i]);

else

aLake.add(thingsCaught[i]);

}

numThingsCaught = 0;

}

// Return the weight of all caught fish so far

public float getWeightSoFar() {

float total = 0;

for (int i=0; i

total += thingsCaught[i].getWeight();

return total;

}

}

 

Complete the canKeep() method in the Fish class. A fish can be "kept" if its size is greater than the "throw-back" size.

Complete the isDesirableTo() method in the Fish class. A fish is "desirable" to the given fisher if ALL of the following can be satisfied:a. it can be "kept" (i.e., all fish less than 18cm must be thrown back).

b. the Fisher has less than his LIMIT of fish that may be caught.

c. the weight of this fish combined with the "total weight of all fish caught by the Fisher so far"

has not exceeded the Fisher's weight limit.

3. Test your code with the following program. Run it a few times and make sure that it is working.

public class FishingTestProgram1 {

public static void main(String [] args) {

// Create a big pond with 15 fish

Lake weirdLake = new Lake(15);

weirdLake.add(new Fish(76, 6.1f));

weirdLake.add(new Fish(32, 0.4f));

weirdLake.add(new Fish(20, 0.9f));

weirdLake.add(new Fish(30, 0.4f));

weirdLake.add(new Fish(140, 7.4f));

weirdLake.add(new Fish(15, 0.3f));

weirdLake.add(new Fish(90, 5.9f));

weirdLake.add(new Fish(120, 6.8f));

weirdLake.add(new Fish(80, 4.8f));

weirdLake.add(new Fish(42, 3.2f));

weirdLake.add(new Fish(100, 5.6f));

weirdLake.add(new Fish(45, 2.0f));

weirdLake.add(new Fish(16, 0.2f));

weirdLake.add(new Fish(30, 1.2f));

weirdLake.add(new Fish(7, 0.1f));

System.out.println("Here is what is in the lake to begin with ...");

weirdLake.listAllThings();

// Create two people to fish in the lake

Fisher fred = new Fisher("Fred", 25);

Fisher suzy = new Fisher("Suzy", 15);

System.out.println("Fred casts his fishing line into the lake 10 times ...");

for (int i=0; i<10; i++)

fred.goFishingIn(weirdLake);

fred.listThingsCaught();

System.out.println("Suzy casts her fishing line into the lake 10 times ...");

for (int i=0; i<10; i++)

suzy.goFishingIn(weirdLake);

suzy.listThingsCaught();

System.out.println("Here is what remains in the lake ...");

weirdLake.listAllThings();

// Now simulate Suzy giving her fish to Fred

System.out.print("Suzy now gives all her fish to Fred (if he can keep ");

System.out.println("them), otherwise she returns them to the lake ...");

suzy.giveAwayFish(fred, weirdLake);

suzy.listThingsCaught();

fred.listThingsCaught();

System.out.println("Here is the lake when Fred and Suzy go home ...");

weirdLake.listAllThings();

}

}

 

(2) The Fish Subclasses

Create a folder called Part2 and copy all of the files from Part1 into it. Create all additional subclasses necessary to construct the following hierarchy of classes:

Note that Fish, NonEndangeredFish and EndangeredFish are all abstract classes, while the other 4 are concrete classes. With the exception of the Fish class, none of the other classes here have any attributes defined within them. Follow the steps on the next page.

1.    Change Fish to be an abstract class. Change the canKeep() method to be an abstract method.

2.    Adjust the toString() method in the Fish class so that it displays the proper type of fish. You cannot use instanceof nor use any IF statements.

(e.g., "A 16cm 0.2kg Perch" instead of "A 16cm 0.2kg Fish" )

3. Using maximum use of inheritance, create a toString() method in the EndangeredFish class so

that endangered fish display as follows:

"A 42cm 3.2kg AuroraTrout (ENDANGERED)"

"A 80cm 4.8kg AtlanticWhiteFish (ENDANGERED)"

4. Write the canKeep() method in the NonEndangeredFish and EndangeredFish classes so that endangered fish can never be kept and non-endangered fish can be kept only if their size is greater than the "throw-back" size as defined in the Fish class.

5. Test your code using the following program (copy the code into a file called FishingTestProgram2.java). You will need to create some constructors as well in order to get this to work.

public class FishingTestProgram2 {

public static void main(String [] args) {

// Create a big lake with 15 fish

Lake weirdLake = new Lake(15);

weirdLake.add(new AuroraTrout(76, 6.1f));

weirdLake.add(new Perch(32, 0.4f));

weirdLake.add(new Bass(20, 0.9f));

weirdLake.add(new Perch(30, 0.4f));

weirdLake.add(new AtlanticWhiteFish(140, 7.4f));

weirdLake.add(new Bass(15, 0.3f));

weirdLake.add(new Bass(90, 5.9f));

weirdLake.add(new Bass(120, 6.8f));

weirdLake.add(new AtlanticWhiteFish(80, 4.8f));

weirdLake.add(new AuroraTrout(42, 3.2f));

weirdLake.add(new Bass(100, 5.6f));

weirdLake.add(new Bass(45, 2.0f));

weirdLake.add(new Perch(16, 0.2f));

weirdLake.add(new Bass(30, 1.2f));

weirdLake.add(new Perch(7, 0.1f));

System.out.println("Here is what is in the lake to begin with ...");

weirdLake.listAllThings();

// Create two people to fish in the lake

Fisher fred = new Fisher("Fred", 25);

Fisher suzy = new Fisher("Suzy", 15);

System.out.println("Fred casts his fishing line into the lake 10 times ...");

for (int i=0; i<10; i++)

fred.goFishingIn(weirdLake);

fred.listThingsCaught();

System.out.println("Suzy casts her fishing line into the lake 10 times ...");

for (int i=0; i<10; i++)

suzy.goFishingIn(weirdLake);

suzy.listThingsCaught();

System.out.println("Here is what remains in the lake ...");

weirdLake.listAllThings();

// Now simulate Suzy giving her fish to Fred

System.out.print("Suzy now gives all her fish to Fred (if he can keep ");

System.out.println("them), otherwise she returns them to the lake ...");

suzy.giveAwayFish(fred, weirdLake);

fred.listThingsCaught();

suzy.listThingsCaught();

System.out.println("Here is the lake when Fred and Suzy go home ...");

weirdLake.listAllThings();

}

}

(3) The Other Subclasses

Create a folder called Part3 and copy all of the files from Part2 into it. Create all additional classes necessary to form this hierarchy:

Abstract class SunkenObject should have a weight (i.e., a float) attribute associated with it.

1. Create a constructor that takes an initial weight.

2. Create a get method for the weight.

3. The Tire, Treasure and RustyChain classes should each have a zero-parameter constructor that calls the SunkenObject constructor with initial weights of 10, 20 and 15, respectively.

4. Write a single toString() method in the SunkenObject class so that Tires, Treasures and RustyChains look as follows when printed:

A 10kg Tire

A 20kg Treasure

A 15kg RustyChain

5. Create and compile an interface called Catchable which defines the following two methods:

public float getWeight();

public boolean isDesirableTo(Fisher f);

6. Have SunkenObject implement the Catchable interface. By default,  unkenObjects are NOT desirable. However, Treasures are desirable if the number of things caught by the fisher is below the LIMIT of items that are allowed to be caught (see Fisher class).  lso, have Fish implement the Catchable interface.

7. Alter the Lake class so that they contain Catchable objects instead of just Fish objects.

8. Alter the Fisher class so that they keep Catchable objects as thingsCaught instead of just Fish objects.

9. Adjust the giveAwayFish() method in the Fisher class so that only Fish are given away, not Treasures.

10. Adjust the getWeightSoFar() method in the Fisher class so that it returns the weight of all Fish that have been caught and kept, ignoring the weight of any kept Treasures.

11. Test your code again by using the following test program. Run it a few times to be sure that everything is working. You will want to run until Suzy gets a treasure in order to make sure that she does not give it away to Fred, nor throw it back into the lake. Also, you will want to verify that Fred never exceeds his weight limit (especially when Suzy gives him her fish) and that no fish below 18cm are kept by anyone.

 

public class FishingTestProgram3 {

public static void main(String [] args) {

// Create a big lake with 15 fish, 2 tires, 2 treasures and 2 rusty chains

Lake weirdLake = new Lake(21);

weirdLake.add(new AuroraTrout(76, 6.1f));

weirdLake.add(new Tire());

weirdLake.add(new Perch(32, 0.4f));

weirdLake.add(new Bass(20, 0.9f));

weirdLake.add(new Treasure());

weirdLake.add(new Perch(30, 0.4f));

weirdLake.add(new AtlanticWhiteFish(140, 7.4f));

weirdLake.add(new RustyChain());

weirdLake.add(new Bass(15, 0.3f));

weirdLake.add(new Tire());

weirdLake.add(new Bass(90, 5.9f));

weirdLake.add(new Bass(120, 6.8f));

weirdLake.add(new AtlanticWhiteFish(80, 4.8f));

weirdLake.add(new AuroraTrout(42, 3.2f));

weirdLake.add(new Bass(100, 5.6f));

weirdLake.add(new Bass(45, 2.0f));

weirdLake.add(new RustyChain());

weirdLake.add(new Perch(16, 0.2f));

weirdLake.add(new Bass(30, 1.2f));

weirdLake.add(new Treasure());

weirdLake.add(new Perch(7, 0.1f));

System.out.println("Here is what is in the lake to begin with ...");

weirdLake.listAllThings();

// Create two people to fish in the lake

Fisher fred = new Fisher("Fred", 15);

Fisher suzy = new Fisher("Suzy", 15);

System.out.println("Fred casts his fishing line into the lake 10 times ...");

for (int i=0; i<10; i++)

fred.goFishingIn(weirdLake);

fred.listThingsCaught();

System.out.println("Suzy casts her fishing line into the lake 10 times ...");

for (int i=0; i<10; i++)

suzy.goFishingIn(weirdLake);

suzy.listThingsCaught();

System.out.println("Here is what remains in the lake ...");

weirdLake.listAllThings();

// Now simulate Suzy giving her fish to Fred

System.out.print("Suzy now gives all her fish to Fred (if he can keep ");

System.out.println("them), otherwise she returns them to the lake ...");

suzy.giveAwayFish(fred, weirdLake);

fred.listThingsCaught();

suzy.listThingsCaught();

System.out.println("Here is the lake when Fred and Suzy go home ...");

weirdLake.listAllThings();

}

}

NOTE: Submit all .java and .class files needed to run. You MUST NOT use packages in your code, nor projects. Submit

ALL of your files in one folder such that they can be opened and compiled individually in JCreator. Some IDEs may create

packages and/or projects automatically. You MUST export the .java files and remove the package code at the top if it is

there. Do NOT submit JCreator projects either. JUST SUBMIT the JAVA and CLASS FILES.

Java, Programming

  • Category:- Java
  • Reference No.:- M9158894
  • Price:- $60

Priced at Now at $60, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As