d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Clonable Problems > Java
Add Reply New Topic New Poll
Member
Posts: 3
Joined: Feb 26 2013
Gold: 0.00
Mar 2 2013 06:25pm
[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
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 2 2013 06:32pm
Sheep blackie= new Sheep();
blackie.setName("blackie");
Sheep dolly = blackie.clone();
dolly.setName("dolly");

now you have two sheep, blackie and dolly. do whatever you want with em
Member
Posts: 3
Joined: Feb 26 2013
Gold: 0.00
Mar 2 2013 06:48pm
ok I put in that code in my app and got this following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Object to Sheep

at CountTestApp.main(CountTestApp.java:45)

that line is: Sheep dolly = blackie.clone();

so why am I getting this error?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 2 2013 06:48pm
apparently clone() returns type Object. just cast it.

/edit: if you want the count to persist within blackie, you're gonna have to do some more work in your clone method. right now the default implementation inherited from Object doesn't copy over instance variables. personally, i use serializing and deserializing as long as performance isn't a problem. otherwise it has to be maintained as you add more instance variables. i dont care enough to read your assignment right now, so tldr make sure your clone method does everything you want it to do.

This post was edited by carteblanche on Mar 2 2013 06:52pm
Member
Posts: 3
Joined: Feb 26 2013
Gold: 0.00
Mar 2 2013 07:16pm
I appreciate your swift reply and your honest answer about not wanting to read the assignment. It helps me a lot if you remember laymen's terms or give a small example of what your trying to suggest I do to solve the error. I'm sorry I'm not a master in Java Programming. I have very little time in coding and no prior coding experience at all, so you know where I come from I'm really a newbie. What is tldr mean? Since this is the first time using clone method, I have no clue what I'm doing or if its doing everything I want it to do. This is why I came here to ask questions from all you pro's that deal with this on a regular basis... to learn and understand. Thanks in advance for your help.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll