[URL]http://forums.d2jsp.org/topic.php?t=28116162&f=124&o=40[/URL]
I also had a similar assignment project and I really learned a lot so far from this discussion but it just ended.
I got my alligators to count 1 alligator 2 alligator 3 alligator and I got my counting sheep... 1 blackie 2 blackie after that I can not get my app to b) clones the first sheep, changes the name, and counts it 3 times and c) counts the first sheep again 1 time. This is my first post so I have forgotten what to surround the coding with that I saw when I first signed up this week, so please be patient. I'm new with java programming and I really appreciate patience with my lack of understanding. I hope its suppose to be surrounded with code.Code
/**
* @author Lori bb implements the Countable interface. Should include
* an instance variable that stores the count and a method that returns
* the formatted count.
*
*/
public class Alligator implements Countable
{
private int count;
public Alligator()
{
this.count = 1;
}
@Override
public void incrementCount()
{
count++;
}
@Override
public void resetCount()
{
count = 1;
}
@Override
public int getCount()
{
return count;
}
@Override
public String getCountString()
{
return (count + " alligator");
}
}
/**
* @author Lori bb interface tht can be used to count an object
* this interface should include these methods: void incrementCount
* void resetCount, int getCount, and String getCountString
*
*/
public interface Countable
{
void incrementCount();
void resetCount();
int getCount();
String getCountString();
}
/**
* @author Loribb Class should include a static method that lets
* you count any Countable objects a specified number of times.
*
*/
public class CountUtil
{
public void count(Countable c, int maxCount)
{
//call the resetCount method
c.resetCount();
while (c.getCount() <= maxCount)
{
System.out.println(c.getCountString());
c.incrementCount();
}
System.out.println("\n");
}
}
/**
* @author Lori bb to display and clone sheep objects
*
*/
public interface Cloneable
{
}
/**@author Loribb implements a Countable interface and the Cloneable
* interface. 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.
*/
public class Sheep implements Countable, Cloneable
{
private int count;
private String name;
public Sheep()
{
this.count = 1;
}
@Override
public void incrementCount()
{
count++;
}
@Override
public void resetCount()
{
count = 1;
}
@Override
public int getCount()
{
return count;
}
@Override
public String getCountString()
{
return (count + " Blackie");
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
@Override
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
* @author Loribb Project 8-1 wk.7 Count alligators and clone sheep
* Operation: This application uses and 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
* object 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 Countable objects 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.
*/
public class CountTestApp
{
public static void main(String[] args)
{
System.out.println("Counting alligators...\n");
int count = 3;
CountUtil countUtil = new CountUtil();
Alligator alligator = new Alligator();
countUtil.count(alligator, count);
System.out.println("Counting sheep...\n");
int sheepCount = 2;
Sheep sheep = new Sheep();
countUtil.count(sheep, sheepCount);
}
}
This is what I have so far which does run without errors as described. Now how do I clone the first sheep Blackie, change the name to Dolly, print Dolly 3 times to console and count the first sheep Blackie again? <<This is where I'm stuck at>>
Any explanation or help I'd appreciate. I already handed in what I had, but I want to know how to do it correctly and understand it not just hand it in what I have