Operation This application uses an Alligator class that implements a Countable i
ID: 3799227 • Letter: O
Question
Operation
This application uses an Alligator class that implements a Countable interface to display Alligator objects as shown above.
This application uses a Sheep class that implements a Countable interface and the Cloneable interface to display and clone Sheep objects as shown above.
Specifications
Create an interface named Countable that can be used to count an object. This interface should include these methods:
void incrementCount()void resetCount()int getCount()String getCountString()
Create a class named Alligator that implements the Countable interface. This class should include an instance variable that stores the count and a method that returns the formatted count.
Create a class named CountUtil. This class should include a static method that lets you count any Countableobjects a specified number of times. For example:
public static void count(Countable c, int maxCount)
Create a class named CountTestApp that uses the CountUtil class to count an Alligator object 3 times as shown above.
Create a class named Sheep that implements the Countable and Cloneable interfaces. This class should include an instance variable that stores the count and the name of the sheep, and it should provide methods that can set and get the name of the sheep.
Modify the CountTestApp class so it (a) counts the first sheep 2 times, (b) clones the first sheep, changes the name, and counts it 3 times, and (c) counts the first sheep again 1 time.
JAVA
Console Counting alligators 1 alligator 2 alligator 3 alligator Counting sheep. 1 Blackie 2 Blackie 1 Dolly 2 Dolly 3 Dolly 1 BlackieExplanation / Answer
public class Alligator implements Countable
{
private int count;
public void countInc()
{
count++;
}
public void countRes()
{
count = 0;
}
public int getCount()
{
return count;
}
public String getCountStr()
{
return count + "" + "Alligator";
}
}
public class Sheep implements Countable, Cloneable {
private int sheepCount = 0;
private String sheepName;
public void setSheepName(String sheep)
{
sheepName = sheep;
}
public String getSheepNames()
{
return sheepName;
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
public void countInc()
{
sheepCount = sheepCount + 1;
}
public void countRes()
{
sheepCount = 0;
}
public int getCount()
{
return sheepCount;
}
public String getCountStr()
{
return getCount() + "" + getSheepNames();
}
}
public interface Countable {
void countInc();
void countRes();
public int getCount();
String getCountStr();
}
public class CountUtil {
public static void count (Countable c, int maxCount)
{
for (int i=0; i<maxCount;i++)
{
c.countInc();
c.getCount();
System.out.println(c.getCountStr());
}
}
}
public class CountTestApp extends CountUtil {
public static void main(String args[]) throws CloneNotSupportedException
{
System.out.println("Counting alligators..."+" ");
Countable a = new Alligator();
count(a,3);
System.out.println();
System.out.println("Counting sheep..."+" ");
Countable s = new Sheep();
Sheep s1 = new Sheep();
s1.countRes();
s1.setSheepName("Blackie");
count(s1,2);
System.out.println("");
Sheep s2 = (Sheep) s1.clone();
s2.countRes();
s2.setSheepName("Dolly");
count(s2,3);
System.out.println("");
s1.countRes();
s1.setSheepName("Blackie");
count(s1,1);
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.