For each of the following questions, remember that there should be no print stat
ID: 3613275 • Letter: F
Question
For each of the following
questions, remember that there should be no print statements inside the Fish class.
Instead, you will print results from the FishTester class, where you will use the Fish
class.
Exercise 3a
Write the accessor methods for the Fish class.
Exercise 3b
Write the incrementAge() method, which increases the age of the fish by 1 day.
Exercise 3c
A fish can only breed if it is 12 days old, or older. Write the canBreed() method, which
determines whether a fish can breed.
Exercise 3d
Write a FishTester class, which uses the Fish class. In this FishTester class, you should,
in the following order:
· Create a Fish object
· Repeatedly increment the age of the fish. Do this by using a loop.
· The loop should stop when the fish has reached the age that it can breed. You
should be able to write this without actually knowing what the age of a fish is for
it to be able to breed in the FishTester class.
· After the loop, when the fish has reached breeding age, print out the fish’s age.
/**
* This class represents a fish that can get older
* and can eventually breed
* @author:
*/
class Fish
{
// Instance Variables
// Age of the fish, in days
private int age;
// Constructor
/**
* Creates a new Fish
*/
public Account()
{
age = 0;
}
// Instance Methods
// EXERCISE 3a: WRITE YOUR CODE HERE
/**
* Increases the age of the fish by 1 day
*/
public void incrementAge()
{
// EXERCISE 3b: WRITE YOUR CODE HERE
}
/**
* Returns true if the fish can breed;
* a fish can breed if its age is at least 12 days old
*/
public boolean canBreed()
{
// EXERCISE 3c: WRITE YOUR CODE HERE
}
}
I really appreciate all this answer for this questions
thank you so much
Explanation / Answer
Exercise 3a Write the accessor methods for the Fish class. The fish class has only one instance variable- age - so only one accessor isneeded. public int getAge() { return age; } Exercise 3b Write the incrementAge() method, which increases the age of thefish by 1 day. Because the instance variable agestores the age of the fish in days, increment age by 1. public voidincrementAge() { age++; } Exercise 3c A fish can only breed if it is 12 days old, or older. Write thecanBreed() method, which determines whether a fish canbreed. If the fish is 12 days old, or older, returntrue. Otherwise, return false. publicboolean canBreed() { returnage>=12; } Exercise3d Write a FishTester class, which uses the Fish class. In thisFishTester class, you should, in the following order: · Create a Fish object · Repeatedly increment the age of the fish. Do this by usinga loop. · The loop should stop when the fish has reached the agethat it can breed. You should be able to write this withoutactually knowing what the age of a fish is for it to be able tobreed in the FishTester class. · After the loop, when the fish has reached breeding age,print out the fish’s age. publicclass FishTester { publicstatic void main(String[]args) { Fish fish=new Fish(); while(!fish.canBreed()) { fish.incrementAge(); } System.out.println(fish.getAge()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.