Simulate some people catching fish in a lake in java program

Assignment Help JAVA Programming
Reference no: EM1365116

In this project, you will simulate some people catching fish in a lake. The purpose of the assignment is to get used to using Arrays as well as getting more experience in having objects interact together. 

(1) The Fish class  

Define a class called Fish that has the following private attributes:  

• species - a string indicating the type of fish (e.g., "Pike", "Bass", "Sunfish") 

• size - an int indicating the size of the fish in cm.  

Write these public methods: 

• get methods for each attribute 

• a constructor that takes the size and species of the fish 

• a toString() method that returns a string in the following format: 

 "A 10cm Pike" 

Make sure that your code works before continuing by running the following code: 

public class FishTestProgram { 

 public static void main(String args[]) { 

 Fish f = new Fish(4, "Sunfish"); 

 System.out.println(f); // should display A 4cm Sunfish 

 System.out.println(f.getSize()); // should display 4 

 System.out.println(f.getSpecies()); // should display Sunfish

 

(2) The Pond class  

Define a class called Pond that defines the following private attributes:  

• fish - an array which will contain all Fish objects that are in the pond 

• numFish - an int indicating the # of fish that are currently in the pond  

Write the following: 

• a constructor that takes the capacity of the pond. The capacity is the maximum number of fish that can be stored in the pond at any time. 

• a get method for the numFish attribute. 

• a method called isFull() which returns a boolean indicating whether or not the pond has reached its capacity of fish. 

• a toString() method that returns a string in the following format: "Pond with 8 fish"

 

Also write the following more interesting methods:  

• a method called add() which takes a single Fish parameter and adds that fish to the pond, provided that the pond is not full. If the pond is full to capacity, simply do nothing. You should assume that the fish is always added to the first available position in the array.  

• a method called listFish() which lists (using System.out.println) all fish in the pond as follows:  Pond with 3 fish as follows: A 35 cm Pike A 6 cm Sunfish A 10 cm Bass  

• a method called catchAFish() which returns a random fish from the pond. The fish should be removed from the pond and returned from the method. If there are no fish, then null should be returned. Note however, that we cannot remove from the array, so simply fill in that position in 

the array with null. You should assume that the fish are all kept consecutively in the array from indices 0 through numFish-1 (i.e., no empty spaces in between). Use Math.random() to compute a random position in the array. Once you take the fish out, move the last fish in the array to fill in the gap so that the fish are stored consecutively again.

Here is a test program that creates 10 fish in a pond and then randomly catches 7. Run the program 

many times to make sure that it works properly without crashing: 

public class PondTestProgram { public static void main(String args[]) { // Create a pond with 10 fish Pond pond = new Pond(15); pond.add(new Fish(4, "Sunfish")); pond.add(new Fish(25, "Pike")); pond.add(new Fish(20, "Bass")); pond.add(new Fish(30, "Perch")); pond.add(new Fish(4, "Sunfish")); pond.add(new Fish(15, "Pike")); pond.add(new Fish(9, "Pike")); pond.add(new Fish(12, "Bass")); pond.add(new Fish(5, "Sunfish")); pond.add(new Fish(12, "Sunfish")); pond.listFish(); 

// Now catch 7 random fish System.out.println("Catching 7 random fish as follows ..."); for (int i=0; i<7; i++) System.out.println(pond.catchAFish()); System.out.println(); // Now show what is left in the pond pond.listFish();  

 // Now try to catch 5 random fish ... but only 3 actually left System.out.println("Catching 5 random fish as follows ..."); for (int i=0; i<5; i++) System.out.println(pond.catchAFish()); System.out.println(); // Now show what is left in the pond pond.listFish(); } } 

(3) The Fisher class  

Define a class called Fisher with the following private attributes: 

• name - a String representing the name of the person who is fishing 

• fishCaught - an array containing all Fish objects that were kept by this person 

• numFishCaught - an int indicating the number of fish that were caught and are currently being kept by this person 

• keepSize - an int indicating the minimum size of fish that this person is willing to keep. Anything less will be thrown back. 

Make the following publicly accessible static variable:  

• LIMIT = 3 (which is the maximum number of fish that all fishers are allowed to keep by "law").  

Write the following:  

• any get methods that you would like to have 

• a constructor that takes the name of the person and the minimum size of fish that can be kept 

• a toString() method that returns a string in the following format: "Bob with 2 fish"

Also write the following more interesting methods:  

• a method called keep() which takes a single Fish parameter and causes the fisher to keep this fish. If the fisher has already reached the LIMIT with regards to how many fish can be kept, this method does nothing.  

• a method called likes() which takes a single Fish parameter and returns a boolean indicating whether or not this fisher would like to keep this fish. A fisher likes a fish only if the fish is at least the desired keepSize for this person and the fish is NOT a Sunfish. 

• a method called listFish() which lists (using System.out.println) all fish caught and kept by this fisher as follows: 

Bob with 2 fish as follows: A 4 cm Pike A 15 cm Bass 

At this point, test your keep(), likes() and listFish() methods using the following test program. Do not 

continue until your code works: 

public class FisherTestProgram1 { 

 public static void main(String [] args) { 

 // Create two people to fish in the pond 

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

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

 

 Fish f1 = new Fish(4, "Sunfish"); 

 Fish f2 = new Fish(25, "Sunfish"); 

 Fish f3 = new Fish(10, "Pike"); 

 Fish f4 = new Fish(15, "Pike"); 

 Fish f5 = new Fish(20, "Pike"); 

 

 System.out.println(fred.likes(f1)); // false 

 System.out.println(fred.likes(f2)); // false 

 System.out.println(fred.likes(f3)); // false 

 System.out.println(fred.likes(f4)); // true 

 System.out.println(fred.likes(f5)); // true 

 

 System.out.println(suzy.likes(f1)); // false 

 System.out.println(suzy.likes(f2)); // false 

 System.out.println(suzy.likes(f3)); // true 

 System.out.println(suzy.likes(f4)); // true 

 System.out.println(suzy.likes(f5)); // true 

 System.out.println(); 

 

 // FORCE the fishers to keep some fish (for testing purposes only) 

 fred.keep(f1); 

 fred.keep(f2); 

 fred.keep(f3); 

 fred.keep(f4); 

 fred.keep(f5); 

 suzy.keep(f2); 

 suzy.keep(f3); 

System.out.println(); 
 
 // Now display the fish that were kept 
 fred.listFish(); // should show 3 fish 
 suzy.listFish(); // should show 2 fish 
 } 
 
Now create a method called goFishingIn() which takes a single Pond object parameter and returns no specific value. The method should catch a random fish from the pond and then decide whether or not the fisher "likes" the fish. If he/she "likes" the fish, the fish is removed from the pond and kept by the fisher. If unable to keep the fish for any reason, the fish must be returned to the pond. You MUST make use of your keep() , likes(), catchAFish() and add() methods that you wrote previously.
Change the LIMIT of fish that the fisher is able to catch from 3 to 10 and run the following test program: 
public class FishingTestProgram2 { 
 public static void main(String [] args) { 
 // Create a big pond with 15 fish 
 Pond bigPond = new Pond(15); 
 bigPond.add(new Fish(4, "Sunfish")); 
 bigPond.add(new Fish(25, "Pike")); 
 bigPond.add(new Fish(20, "Bass")); 
 bigPond.add(new Fish(30, "Perch")); 
 bigPond.add(new Fish(14, "Sunfish")); 
 bigPond.add(new Fish(15, "Pike")); 
 bigPond.add(new Fish(9, "Pike")); 
 bigPond.add(new Fish(12, "Bass")); 
 bigPond.add(new Fish(5, "Sunfish")); 
 bigPond.add(new Fish(12, "Sunfish")); 
 bigPond.add(new Fish(10, "Bass")); 
 bigPond.add(new Fish(2, "Bass")); 
 bigPond.add(new Fish(16, "Perch")); 
 bigPond.add(new Fish(30, "Sunfish")); 
 bigPond.add(new Fish(7, "Perch")); 
 bigPond.listFish(); 
 
 // Create two people to fish in the pond 
 Fisher fred = new Fisher("Fred", 15); 
 Fisher suzy = new Fisher("Suzy", 10); 
 
 System.out.println("First Fred catches 20 fish in the big pond ..."); 
 for (int i=0; i<20; i++) 
 fred.goFishingIn(bigPond); 
 fred.listFish(); 
 
 System.out.println("Suzy now catches 20 fish in the big pond ..."); 
 for (int i=0; i<20; i++) 
 suzy.goFishingIn(bigPond); 
 suzy.listFish(); 
System.out.println("Here is what is left of the pond ..."); 
 bigPond.listFish(); 
 } 
 
Run the program a few times. You should notice that Fred keeps an average of between 3 to 5 fish each time while Suzy keeps an average of between 2 and 4 ... depending on how many Fred keeps (because he fishes first). However, if you ran it many times and Fred kept catching sunfish and small fish, likely Suzy can get up to 6 or 7 fish. Notice as well that the fish remaining after the fishing are almost all Sunfish or small fish. Occasionally, some other fish are in there because they avoided being caught ;). 
 
Finally, write a method called giveAwayFish() which represents a person quitting altogether and returning his/her fish to the pond and/or giving them away to another fisher. The method should take two parameters. The first parameter should be another Fisher representing the person that this fisher wants to give the fish to. The second parameter should be a Pond, representing the pond to return any 
unwanted fish to. The method works as follows. Go through all of this person's fish (i.e., the one giving the fish away) and see if the other fisher (i.e., the one who will be receiving the fish) is willing to keep any. If the other fisher wants any, they are to be given to that fisher. You should make use of the likes() and keep() methods. Otherwise, if the fisher is unwilling to keep the fish, then these fish must be returned to the pond. By the end of the method, the fisher giving away the fish should have no fish left and no fish may be lost (i.e., must be with the other fisher or in the pond). 
Test your code by adding the following to the bottom of the FishingTestProgram2: 
 
 // Now simulate Suzy giving her fish to Fred 
 suzy.giveAwayFish(fred, bigPond); 
 fred.listFish(); 
 suzy.listFish(); 
 bigPond.listFish(); 
 
You should notice that Fred will keep some of Suzy's fish, but not the smaller ones...which will be added back to the pond. Make sure that the total fish in the pond and with Fred add up to 15 ;). 

Reference no: EM1365116

Questions Cloud

Health care consumers and health care providers : There are a variety of communication modalities available to health care consumers and health care providers. These modalities and venues of communication may entail benefits and challenges to both consumers and providers.
Orginazaitonal behavior questions : Show the advantages and disadvantages of the use of personality assessment tools, such as the MBTI, in organizations for internal personnel assessments and recruitment.
Governmental accounting standards : What is Governmental Accounting Standards and what are Financial Accounting Standards Board. What are the objectives of the GASB and the FASB. What are their similarities and what are their differences.
What is the value of a and b : He also says he wouldn't mind moving if when he moved he got a raise of $B. What is the value of A and B.
Simulate some people catching fish in a lake in java program : In this project, you will simulate some people catching fish in a lake. The purpose of the assignment is to get used to using Arrays as well as getting more experience in having objects interact together.
Health and health policy : Research the Internet for key words such as "Health" and "Health Policy" and select a policy topic that is of interest to you.
Cluster multivariate technique : Describe the Cluster analysis, how it is different factor analysis and multi-dimensional scaling? Name a real-life company has used the Cluster analysis technique
Calculating deprciation expense : O'Bria Service Company purchased a copier on January 1, 2012, for $17,000 and paid an additional $200 for delivery charges. The copier was estimated to have a life of four years or 800,000 copies.
What was its velocity during the time : A stationary radar operator determines that a ship is 17 km south of him. An hour later the same ship is 21 km southeast. If the ship moved at constant speed and always in the similar direction, what was its velocity during this time.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Socket programming in java: tcp

In this project we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web server that is capable of processing multiple simultaneous service requests in parallel.

  Executing stringed musical instrument class

Create and execute a stringed musical instrument class.

  Distributed systems

Build robust, secure distributed systems using advanced programming techniques

  Java application to permit user to enter ten numbers

Write down Java application which permits a user to enter 10 numbers (double precision) into the array and then sorts and displays numbers from lowest to highest.

  Prepare executable program and a dictionary program

Prepare executable program and a dictionary Program.

  Design an abstract data type in java

Design an abstract data type in Java that represents a musical pitch

  Implement the application using a singly linked list

Implement the following application using a singly linked list. This application accepts from console and stores a list of 10 names of your friends in the singly linked list

  World data app

Prepare WorldDataApp project. It implements the NameIndex portion, including creating it in SetupProgram, and searching, viewing and updating it in UserApp program.

  Create a driver class in java

Your project is to create a driver class that uses SuperJavaIceCreamClass.

  Write java program to compute how much federal need to pay

Write a java application to calculate how much federal and state tax you need to pay. The program should accomplish the following task.

  Classes and pointers experience using dynamic memory

To practice defining classes using separate compilation using classes, vectors, and pointers experience using dynamic memory.

  Uml exercise

UML Exercise: Automated Teller Machine (ATM),   1. To allow authorized card holders to make transactions,   Brief Summary of Requirements:

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