Results 1 to 19 of 19
Thread: Cloning problem in Java
- 10-30-2008, 01:42 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Cloning problem in Java
I'm having trouble understanding the whole process of cloning in Java. I've read everything I can find and I still don't get it. I need to build a "Defensive" class using cloning. I'm not exactly sure how to do this I get the general idea that I need to make the variables immutable so that once they are edited once the user will not be able to mess with them. Here is my code if you need any other information let me know. Thanks in advance.
that is one of four classes I can post the others if needed here is my Main...Java Code:/* * Memory.java */ package defensive; public final class Memory implements Cloneable{ //Attributes private String partNumber; private String model; private int capacity; // Constructors public Memory() { this("00000000", "Dummy", 0); } public Memory(String partNumber, String model, int capacity) { this.partNumber = partNumber; this.model = model; this.capacity = capacity; } //Accessors public String getPartNumber() { return partNumber; } public String getModel() { return model; } public int getCapacity() { return capacity; } //Mutators public final void setPartNumber(String partNumber) { this.partNumber = partNumber; } public final void setModel(String model) { this.model = model; } public void setCapacity(int capacity) { this.capacity = capacity; } @Override public Object clone() throws CloneNotSupportedException { Memory s2 = (Memory)super.clone(); //s2.capacity = (Capacity)this.capacity.clone(); return s2; } // Behaviors public String toString() { return "Model: " + model + " (" + capacity + " GB)" + " - P/N: " + partNumber; } }
If any other info is needed let me know thanks.Java Code:/* * Main.java */ package defensive; import java.lang.String; public class Main { public static void main(String[] args) { boolean TOGGLE = false; //true = Demo, false = Test if (TOGGLE) { //Brittle Demo System.out.println("===== Begin Brittle Demo =====\n"); //Build a brittle computer object Cpu brittleCpu = new Cpu("11111", "22222", 3.3); Memory brittleMemory = new Memory("44444", "55555", 6); MotherBoard brittleMotherBoard = new MotherBoard("77777", brittleCpu, brittleMemory); Computer brittleComputer = new Computer("88888", brittleMotherBoard); //Show original configuration System.out.println("Original Configuration\n" + brittleComputer + "\n"); //Break it brittleMemory.setCapacity(100000000); //Show broken configuration System.out.println("Broken Configuration\n" + brittleComputer + "\n"); //Create a replacement CPU and install in brittleMotherBoard Cpu replacementCpu = new Cpu("AAAAA", "BBBBB", 999.0); brittleComputer.getMotherBoard().setCpu(replacementCpu); //Show new configuration System.out.println("New Configuration\n" + brittleComputer + "\n"); //Modify replacementCpu replacementCpu.setModel("ZZZZZ"); //Show new broken configuration System.out.println("New Broken Configuration\n" + brittleComputer + "\n"); //Create String representing memory model data * use to modify memory String memoryModelChange = "WWWWW"; brittleComputer.getMotherBoard().getMemory().setModel(memoryModelChange); //Show current configuration System.out.println("Current Configuration\n" + brittleComputer + "\n"); //Modify memoryModelChange memoryModelChange += "*****"; //Show current configuration System.out.println("Unchanged Configuration\n" + brittleComputer + "\n"); } else { //Begin Test System.out.println("===== Begin Bullet-Proof Testing =====\n"); //Create test Cpu object and print state Cpu testCpu = new Cpu("GX9764CPU", "Intel 3200G", 3.2); System.out.println(testCpu.toString() + "\n"); //Create test Memory object and print state Memory testMemory = new Memory("MD400SD", "Micron SDRAM", 4); System.out.println(testMemory.toString() + "\n"); //Create test MotherBoard object and print state MotherBoard testMotherBoard = new MotherBoard("Intel XP3300", testCpu, testMemory); System.out.println(testMotherBoard.toString() + "\n"); //Create test Computer object and print state Computer testComputer = new Computer("Dell PowerEdge 2100", testMotherBoard); System.out.println(testComputer.toString() + "\n"); //Test #1 System.out.println("*** Test #1 ***\n"); //Change state of testCpu and testMemory testCpu.setModel("Broken"); testMemory.setCapacity(0); //Show changes have no impact on testComputer object System.out.println(testCpu + "\n"); System.out.println(testMemory + "\n"); System.out.println(testComputer + "\n"); //Test #2 System.out.println("*** Test #2 ***\n"); //Change state of testMotherBoard Cpu newCpu = new Cpu("Broken", "Broken", 0.0); testMotherBoard.setCpu(newCpu); //Show changes have no impact on testComputer object System.out.println(testMotherBoard + "\n"); System.out.println(testComputer + "\n"); //Test #3 System.out.println("*** Test #3 ***\n"); //Get aggregate object of testComputer and modify it MotherBoard newMotherBoard = testComputer.getMotherBoard(); //Show current state of testComputer's motherboard' System.out.println(newMotherBoard + "\n"); //Change the CPU in the acquired motherboard object newMotherBoard.setCpu(newCpu); //Show current state of acquired motherboard object System.out.println(newMotherBoard + "\n"); //Show changes have no impact on testComputer object System.out.println(testComputer + "\n"); //Test #4 System.out.println("*** Test #4 ***\n"); //Swap out the CPU in the computer Cpu swapCpu = new Cpu("AZ4500CPU", "Intel 5000G", 5.0); MotherBoard tempMotherBoard = testComputer.getMotherBoard(); tempMotherBoard.setCpu(swapCpu); testComputer.setMotherBoard(tempMotherBoard); //Show CPU swapped out System.out.println(testComputer + "\n"); //Modify swapCpu and show no impact on testComputer swapCpu.setPartNumber("None"); System.out.println(testComputer + "\n"); } } }
- 10-30-2008, 02:31 AM #2
The only other info we need is what do you want from us?
Do you have any questions? Are you getting errors?
Is the output not what you want?
Can you describe what you want to change?
Would that be like a final variable.make the variables immutable
Or does it mean that all the values in the object are passed in via a constructor and that there are no set.... methods that can change the variables once the object is created?
- 10-30-2008, 02:42 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
I need to keep the variables from being changed once they are initially defined. I was told to do this by cloning the data that was submitted (given in Main). In doing so if the user tries to edit the data again to "break" it. I have attached the remainding classes in which I need to clone the data in as well. Oh, when I say data I mean the objects created. They should not be able to edit the object once it is set the first time. I have attached my output thus far and the output that it needs to match.
These are the remainding classes if needed.
Java Code:/* * MotherBoard.java */ package defensive; public class MotherBoard implements Cloneable{ //Attributes => immutable & mutable private String model; private Cpu cpu; private Memory memory; // Constructors public MotherBoard() { this("00000000", new Cpu(), new Memory()); } public MotherBoard(String model, Cpu cpu, Memory memory) { this.model = model; setCpu(cpu); setMemory(memory); } // Accessors public Cpu getCpu() { return cpu; } public Memory getMemory() { return memory; } // Mutators public void setCpu(Cpu cpu) { this.cpu = cpu; } public void setMemory(Memory memory) { this.memory = memory; } // Behaviors public String toString() { return " - Motherboard Configuration:\n * Processor - " + cpu.toString() + "\n * Memory - " + memory.toString(); } }Java Code:/* * Cpu.java */ package defensive; public class Cpu implements Cloneable { //Attributes private String partNumber; private String model; private double speed; // Constructors public Cpu() { this("00000000", "Dummy", 0.0); } public Cpu(String partNumber, String model, double speed) { setPartNumber(partNumber); setModel(model); setSpeed(speed); } //Accessors public String getPartNumber() { return partNumber; } public String getModel() { return model; } public double getSpeed() { return speed; } //Mutators public void setPartNumber(String partNumber) { this.partNumber = partNumber; } public void setModel(String model) { this.model = model; } public void setSpeed(double speed) { this.speed = speed; } // Behaviors @Override public String toString() { return "Model: " + model + " (" + speed + " GHz)" + " - P/N: " + partNumber; } }This is my current output.Java Code:/* * Computer.java */ package defensive; public class Computer { //Attributes private String model; private MotherBoard motherBoard; //Constructors public Computer() { this("00000000", new MotherBoard()); } public Computer(String model, MotherBoard motherBoard) { this.model = model; setMotherBoard(motherBoard); } //Accessors public MotherBoard getMotherBoard() { return motherBoard; } public void setMotherBoard(MotherBoard motherBoard) { this.motherBoard = motherBoard; } //Behaviors public String toString() { return "Model: " + model + "\n" + motherBoard.toString(); } }
And this is the output that I need to match.Java Code:===== Begin Bullet-Proof Testing ===== Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU Model: Micron SDRAM (4 GB) - P/N: MD400SD - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Test #1 *** Model: Broken (3.2 GHz) - P/N: GX9764CPU Model: Micron SDRAM (0 GB) - P/N: MD400SD Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Broken (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD *** Test #2 *** - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD *** Test #3 *** - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD *** Test #4 *** Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 5000G (5.0 GHz) - P/N: AZ4500CPU * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 5000G (5.0 GHz) - P/N: None * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
Thanks for the help.Java Code:===== Begin Bullet-Proof Testing ===== Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU Model: Micron SDRAM (4 GB) - P/N: MD400SD - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Computer Configuration *** - Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Test #1 *** Model: Broken (3.2 GHz) - P/N: GX9764CPU Model: Micron SDRAM (0 GB) - P/N: MD400SD *** Computer Configuration *** - Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Test #2 *** - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Computer Configuration *** - Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Test #3 *** - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD - Motherboard Configuration: * Processor - Model: Broken (0.0 GHz) - P/N: Broken * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Computer Configuration *** - Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Test #4 *** *** Computer Configuration *** - Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 5000G (5.0 GHz) - P/N: AZ4500CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD *** Computer Configuration *** - Model: Dell PowerEdge 2100 - Motherboard Configuration: * Processor - Model: Intel 5000G (5.0 GHz) - P/N: AZ4500CPU * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
- 10-30-2008, 02:51 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
I am guessing I am going to have to Deep clone the objects of each of the provided classes how would I do that?
- 10-30-2008, 06:54 AM #5
Cloning Objects
Making Deep Copies of Objects
How to avoid traps and correctly override methods from java.lang.Object
Attack of the clones
Java Code:import java.awt.Point; public class CloneTest { public static void main(String[] args) throws CloneNotSupportedException { CloneMe cm1 = new CloneMe(10, 25, 12); CloneMe cm2 = (CloneMe)cm1.clone(); System.out.printf("cm1 = %s%ncm2 = %s%n", cm1, cm2); cm1.p.x = 35; System.out.printf("cm1 = %s%ncm2 = %s%n", cm1, cm2); } } class CloneMe implements Cloneable { Point p = new Point(); int z = 12; public CloneMe(int x, int y, int z) { p.setLocation(x, y); this.z = z; } public Object clone() throws CloneNotSupportedException { CloneMe newObj = (CloneMe)super.clone(); // Comment-out this next line for shallow copy. newObj.p = (Point)this.p.clone(); return newObj; } public String toString() { return getClass().getName() + "[p=" + p.getClass().getSimpleName() + "[" + p.x + ", " + p.y + "]; z=" + z + "]"; } }
- 10-30-2008, 07:11 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Thanks for the example but now I am getting this error clone() has protected access in java.lang.Object. I've tried everything I can think of to fix it but no good. here is my code if you could tell me what Im doing wrong.
This is where it says the error is. newMemory.model = (String)this.model.clone();
Java Code:/* * Memory.java */ package defensive; public class Memory implements Cloneable{ //Attributes private String partNumber; private String model; private int capacity; // Constructors public Memory() { this("00000000", "Dummy", 0); } public Memory(String partNumber, String model, int capacity) { this.partNumber = partNumber; this.model = model; this.capacity = capacity; } //Accessors public String getPartNumber() { return partNumber; } public String getModel() { return model; } public int getCapacity() { return capacity; } //Mutators public void setPartNumber(String partNumber) { this.partNumber = partNumber; } public final void setModel(String model) { this.model = model; } public void setCapacity(int capacity) { this.capacity = capacity; } @Override public Object clone() throws CloneNotSupportedException{ Memory newMemory = (Memory)super.clone(); newMemory.model = (String)this.model.clone(); return newMemory; } // Behaviors public String toString() { return "Model: " + model + " (" + capacity + " GB)" + " - P/N: " + partNumber; } }
- 10-30-2008, 07:29 AM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Have you tried to make your clone() method protected instead of public?
- 10-30-2008, 08:25 AM #8
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
still gives same error
- 10-30-2008, 11:04 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
- 10-30-2008, 02:01 PM #10
When you get an error, please post the full text of the message without editing or changing it.
- 10-30-2008, 04:58 PM #11
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Here is the full error
Compiling 2 source files to C:\Documents and Settings\Jake\My Documents\NetBeansProjects\Defensive\build\classes
C:\Documents and Settings\Jake\My Documents\NetBeansProjects\Defensive\src\defensive \Memory.java:19: clone() has protected access in java.lang.Object
clonedMemory.partNumber = (String)partNumber.clone();
1 error
BUILD FAILED (total time: 0 seconds)
- 10-30-2008, 05:45 PM #12
Why would you need to clone a String object. Since its contents can't be changed, copying a reference to it would be enough.
- 10-30-2008, 05:56 PM #13
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Right okay. Well what about when I try to clone capacity? I get this error.
C:\Documents and Settings\Jake\My Documents\NetBeansProjects\Defensive\src\defensive \Memory.java:34: int cannot be dereferenced
clonedMemory.capacity = (Integer)this.capacity.clone();
1 error
BUILD FAILED (total time: 0 seconds)
- 10-30-2008, 05:57 PM #14
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Shouldn't when I cloned Memory all of the variables be cloned as well?
- 10-30-2008, 06:02 PM #15
Integer is like String, its contents can't be changed, so a reference to it gives you its value without worrying about some one changing it.
Can you show the definitions of the variables referenced in the error messages?
- 10-30-2008, 06:10 PM #16
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Not sure what you mean by definition. I have only been messing around with one class and here is what I have.
No matter what I do main still manages to edit the object. How would I pass the cloned object back to Main assuming I have implemented cloning correctly?Java Code:package defensive; public class Memory implements Cloneable{ //Attributes private String partNumber; private String model; private int capacity; //Creation of Deep Clone method. @Override public Object clone() { String clonedModel; String clonedPartNumber; int clonedCapacity; try { super.clone(); //model = clonedModel; //capacity = clonedCapacity; //partNumber = clonedPartNumber; //Memory clonedMemory = (Memory)super.clone(); //clonedMemory.capacity = (Integer)capacity.clone(); //clonedMemory.capacity = (Integer)this.capacity.clone(); return super.clone();//clonedModel, clonedCapacity, clonedPartNumber; } catch (CloneNotSupportedException e) { throw new InternalError(e.toString()); } } // Constructors public Memory() throws CloneNotSupportedException { this("00000000", "Dummy", 0); } public Memory(String partNumber, String model, int capacity) throws CloneNotSupportedException { Memory clonedMemory = (Memory)super.clone(); this.partNumber = partNumber; this.model = model; this.capacity = capacity; } //Accessors public String getPartNumber() { return partNumber; } public String getModel() { return model; } public int getCapacity() { return capacity; } //Mutators public void setPartNumber(String partNumber) { this.partNumber = partNumber; } public final void setModel(String model) { this.model = model; } public void setCapacity(int capacity) { this.capacity = capacity; } //@Override //protected Object clone() throws CloneNotSupportedException{ //Memory clonedMemory = (Memory)super.clone(); //newMemory.model = (String)this.model.clone(); //return clonedMemory; //} // Behaviors public String toString() { return "Model: " + model + " (" + capacity + " GB)" + " - P/N: " + partNumber; } }
- 10-30-2008, 07:18 PM #17
int capacity; // define the variable capacityNot sure what you mean by definition
Which object? If an object is editable, it will always be editable.main still manages to edit the object
Do you have any println() output to show what you mean?
The question is: Can you have one copy of an object in class A that you clone and pass the clone to class B and be able to prevent class B from changing anything in the original class object saved in class A. If you deep copy, there won't be any way for class B to change the original object that is saved in class A.
- 10-30-2008, 07:45 PM #18
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Okay well am I implementing Deep Copy correctly?
- 10-30-2008, 10:52 PM #19
Similar Threads
-
Java problem
By grend in forum New To JavaReplies: 5Last Post: 08-18-2008, 11:44 PM -
folder cloning
By jad in forum Advanced JavaReplies: 1Last Post: 07-01-2008, 12:28 AM -
Cloning objects
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:33 AM -
Cloning in Java
By gchalpatirao in forum Advanced JavaReplies: 0Last Post: 08-14-2007, 03:29 PM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks