Implement a fishlake simulation similar to the previous

Assignment Help JAVA Programming
Reference no: EM13354432

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<numThings; 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<numThingsCaught; 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<numThingsCaught; 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<numThingsCaught; 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.

Reference no: EM13354432

Questions Cloud

Q1 a car carrying a 75 kg test dummy crashes into a wall at : q1. a car carrying a 75 kg test dummy crashes into a wall at 31 ms and is brought to rest in 0.10 s. find the average
Q1 a 67 kg man weighs 637 n on the earths surface how far : q1. a 67 kg man weighs 637 n on the earths surface. how far above the surface of the earth would he have to go to lose
Nbspd control design : nbspd control design using
Question 1 1000 words maximumfind a newspaper article or : question 1 1000 words maximumfind a newspaper article or web page report of an item of accounting news i.e. it refers
Implement a fishlake simulation similar to the previous : implement a fishlake simulation similar to the previous assignment. you will then make adjustments to accommodate class
Qestion 2 the fibonacci numbers are defined as follows f0 : question 2 the fibonacci numbers are defined as follows f0 0 f1 1 and fn fn-1 fn-2 for n gt2 prove each of the
Part a -analogue communication1write a brief explanation of : part a -analogue communication1.write a brief explanation of the principles of the super-heterodyne receiver. nbspit
Company manpower group incticker symbol man united : company manpower group incticker symbol man united statesmake an assessment of where your company stands right now what
One of the key decisions a company must make on : one of the key decisions a company must make on contemplating a new site or a new operation at an existing site is its

Reviews

Write a Review

JAVA Programming Questions & Answers

  What is overloading and what is overriding

What is overloading and what is overriding? Please use code to explain it.

  Algorithm analysis with advanced data structures

Algorithm Analysis with Advanced Data Structures, Your good friend, a Rock Star, asked you to create a Time Manager app for him, The Rock Star performs gigs at certain dates, all around the country - at most one gig per day

  Have to implement a search algorithm

Suppose you have to implement a search algorithm in a high programming language such as Java or C++. You are given a linked list that is not sorted and it's rather small, in the order of a few dozen elements.

  Cascading style sheet to a website

Compare and contrast the process of adding JavaScript and a Cascading Style Sheet to a Website. Determine if they can be used simultaneously in a page.

  Program (using java) that is suppose to find the largest

writing a program (using java) that is suppose to find the lowest integer that can be evenly divided by a range (ex. 1-25). I need help fixing/debugging it so it comes up with the proper output.

  Ask the user how many times you want to run

c the algorithm. 2. Then choose a word at random from the array of Strings as the target (Use the integer random number generator)

  Write a program to play a variation of the game

Roll two dice. Each die has six faces representing values 1, 1, ..., and 6. Check the sum of the two dice. If the sum is 2, 3, or 12 you lose; if the sum is 7 or 11, you win. If the sum is another value (4,5,6,8,9, or 10) a point is established.

  Find method of the class is passed each of the targets

How many calls to the recFind method of the ArraySortedList3 class are made when the find method of the class is passed each of the targets?

  Concept of web based information system

Design and implement a simple and small email server using the concept of web based information system

  Describe the steps to program development

What is the function of parseFloat and parseInt and what will be result if we send "abcd" through a prompt() input and pass it through parseInt()?

  Alter the prototype form page by javascript function

Alter the prototype form page so that when JavaScript function has verified that all the required fields have been filled, cookie is added to user's computer.

  Create bean jsp program which will compute simple interest

Create the bean which will compute Simple Interest. Use bean in JSP program. Accept details of saving like principal amount, rate of interest, period-in years from user and show amount

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd