Question: Class Name Fish Access public Data Instance variables id (type int with private access) weight (type int with private access) maxWeight (type int with private

Class Name

Fish

Access

public

Data

Instance variables

id (type int with private access)

weight (type int with private access)

maxWeight (type int with private access)

alive (type boolean with private access)

Class variables

idCount (type int with private access)

numFishAlive (type int with private access)

Methods

Instance methods

1-argument constructor (accepts argument of appropriate type to initialize the weight - public access)

initialize the weight using the passed-in argument

set maxWeight to twice weight's value

set alive to true

increment idCount using the pre-increment operator

set id to the value of idCount

increment numFishAlive using the post-increment operator

getWeight (private access)

return the weight

kill(public access)

print a message stating that the fish is dead, include the fish id in the message (see picture below for a sample message)

kill the fish (do it quick, there is no need to torture it!)

eat(accepts 1 parameter of type Fish- public access)

print a message indicating that a fish is eating another fish. Include both fish ids in the message (see picture below for a sample message)

kill the fish being eaten

add the weight of the fish being eaten to this fish's weight

if the fish's weight now exceeds the maxWeight, kill the fish. Here is some code you can use for this: if(weight > maxWeight) { kill(); }

toString (public access)

return a well-formatted string indicating the id, weight, and life status of the fish

Class methods

getNumFishAlive (public access)

returns numFishAlive

Class Name

Driver

Access

public

Data

Instance variables

none

Class variable

none

Methods

Instance methods

none

Class method

main(see image below for a sample run)

Ask the user for the weights of four fish.

using the information provided by the user, instantiate four Fish.

Print the number of fish that are alive.

Feed the second fish to the first fish

Feed the fourth fish to the third fish

Feed the first fish to the third fish

Print each of the four fish

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!