• Post Reply Bookmark Topic Watch Topic
  • New Topic

not able to implement Clone  RSS feed

 
amit cha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 4 classes....given below
[Added code tags - see UseCodeTags for details]

how can i apply clone to this...
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is difficult to understand because you have not indented it consistently. Please post it correctly indented and spaced, so we can all see where one method ends and the next begins. It looks as if you have two clone() methods in the same class the way you have written it.
You also need to consider what sort of fields your classes have:
  • 1: All primitives or immutable reference types, or
  • 2: Some fields are mutable reference types
  • In case 1 you can use a shallow clone, but in case 2 you need a deep clone.
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Amit. Welcome to the Ranch!

    I've added code tags to your code to make it easier to read (see UseCodeTags for instructions on how to do that) - but it still really needs formatting properly.

    Anyway, can you give more detail about what your problem is? The first thing you need to decide is how you want cloning to behave. Do you want a shallow clone (meaning you keep the same Node/Vehicle objects)? If so then super.clone() does just what you want, and all you need to do is implement Cloneable and override clone() to make it public. Or do you want it to be a deep clone? In that case, the first thing you need to do is make each of those Cloneable (or provide some other way of making a copy).

    Once you've worked out how you want it to behave, the general approach is:

    - Implement Cloneable
    - Override clone() to make it public
    - In clone(), call super.clone(), then manually perform any additional copying you've decided you need.

    (Or ignore Cloneable completely and provide another way of copying, like a copy constructor. A lot of people prefer to do it that way.)


    You've also got two clone() methods in your Linked class for some reason.
     
    amit cha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    public class Linked implements Cloneable
    {
    Node head;
    Node tail;
    public boolean isEmpty()
    {
    return head==null;
    }
    public void insert(Vehicle veh)
    {
    Node newnode=new Node(veh);
    if(isEmpty())
    {
    head=newnode;
    tail=newnode;
    }
    else
    {
    tail.next=newnode;
    tail=newnode;
    }
    }
    public Object clone()
    {
    Vehicle v=new Vehicle();
    Linked obj=new Linked();
    while(head!=null)
    {
    v=(Vehicle) head.data;
    Node node=new Node(v);
    }
    }



    public Object clone()
    try
    {

    Linked nObj= (Linked) super.clone();
    nObj.data =()head.clone();
    return nobj;
    } catch (CloneNotSupportedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    }

    public Node delete()
    {

    if(head==null)
    {
    tail=null;
    }
    else
    head=head.next;
    return head;
    }

    public void display()
    {
    Node currentNode=head;
    while(currentNode != null)
    {
    currentNode.display();
    currentNode=currentNode.next;

    }
    }

    }







    public class Node
    {
    Object data;
    Node next;
    Node accept;
    Node(Vehicle veh)
    {
    data=veh;
    next=null;
    }
    public void display() {

    System.out.println(data);
    }
    }







    public class Vehicle
    {
    String owner;
    double changeInSpeed;
    double directionOfSpeed;
    Vehicle(String owner,double changeInSpeed,double directionOfSpeed)
    {
    this.owner=owner;
    this.changeInSpeed=changeInSpeed;
    this.directionOfSpeed=directionOfSpeed;
    }
    @Override
    public String toString() {
    return "Vehicle [owner=" + owner + ", changeInSpeed=" + changeInSpeed
    + ", directionOfSpeed=" + directionOfSpeed + "]";
    }

    }



    public class Test
    {
    public static void main(String[] args)
    {
    Vehicle car1=new Vehicle("nano",123.132,143.5);
    Vehicle car2=new Vehicle("tata",3434.23,4342.34);
    Vehicle car3=new Vehicle("maruti",342.45,67.67);
    Linked list= new Linked();
    list.insert(car1);
    list.insert(car2);
    list.insert(car3);
    list.display();

    System.out.println(list);
    Linked list1=(Linked)list.clone();
    System.out.println("copied list is=");
    list1.display();
    list.display();
    list.delete();
    list.display();

    }

    }


    I tried my object list in Test class but it is giving error..
     
    amit cha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The class Node , Linked and Test are used for linked list...
    Vehicle class object reference is passed to the list.
    now all i want is to clone the object..which i have tried...but it is not working ..giving error.
    I want shallow clone on the object...


     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amit cha wrote:The class Node , Linked and Test are used for linked list...
    Vehicle class object reference is passed to the list.
    now all i want is to clone the object..which i have tried...but it is not working ..giving error.
    I want shallow clone on the object...

    What...Vehicle? Why?

    You said above that you wanted a shallow clone; and in most cases, you achieve that by copying references, not whole objects.
    However, I'm still not exactly sure what you want or why you want it, so perhaps you could elaborate.

    Winston
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If all you want is a shallow clone, all you need is one clone() method that looks like this:
     
    amit cha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Winston Gutkowski -- vehicle class is just for creating objects ... and those objects are passes to the list..
    yes i want shallow clone.. but the problem is when i am trying to clone it

    public Object clone()
    try
    {

    Linked nObj= (Linked) super.clone();
    nObj.data =()head.clone();
    return nobj;
    } catch (CloneNotSupportedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    }


    is is giving me error...
    i know there is something wrong with the above code..i just want to know"What is wrong in the code"

     
    amit cha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry Vehicle class initializes the value of the object ... passed from the test class
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amit cha wrote:is is giving me error...
    i know there is something wrong with the above code..i just want to know"What is wrong in the code"


    You still haven't told us what error you are getting. The error message gives you (and us) a lot of information.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amit cha wrote:Sorry Vehicle class initializes the value of the object ... passed from the test class
    What on earth does that mean?
     
    drac yang
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amit cha wrote:@Winston Gutkowski -- vehicle class is just for creating objects ... and those objects are passes to the list..
    yes i want shallow clone.. but the problem is when i am trying to clone it

    public Object clone()
    try
    {

    Linked nObj= (Linked) super.clone();
    nObj.data =()head.clone();
    return nobj;
    } catch (CloneNotSupportedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    }



    is is giving me error...
    i know there is something wrong with the above code..i just want to know"What is wrong in the code"



    it's hard to see something from this given snippet especially without exception stack trace.

    maybe the following small demo would help a little

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!