add a copy construction to the class on the program. it should create a deep cop
ID: 3656916 • Letter: A
Question
add a copy construction to the class on the program. it should create a deep copy. class C15e6 { public static void main(String[] args) { MyLinkedListL6 list = new MyLinkedListL6(); list.addFirst(1); list.addFirst(2); list.addFirst(3); System.out.println("Numbers on list"); list.traverse(); } } //============================================== class MyLinkedListL6 { private class Node { private Node link; private int x; } //---------------------------------- private Node first = null; //---------------------------------- public void addFirst(int d) { Node newNode = new Node(); newNode.x = d; newNode.link = first; first = newNode; } //---------------------------------- public void traverse() { Node p = first; while (p != null) { System.out.println(p.x); p = p.link; } } }Explanation / Answer
// : appendixa:CopyConstructor.java // A constructor for copying an object of the same // type, as an attempt to create a local copy. // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. import java.lang.reflect.Constructor; class FruitQualities { private int weight; private int color; private int firmness; private int ripeness; private int smell; // etc. public FruitQualities() { // Default constructor // Do something meaningful... } // Other constructors: // ... // Copy constructor: public FruitQualities(FruitQualities f) { weight = f.weight; color = f.color; firmness = f.firmness; ripeness = f.ripeness; smell = f.smell; // etc. } } class Seed { // Members... public Seed() { /* Default constructor */ } public Seed(Seed s) { /* Copy constructor */ } } class Fruit { private FruitQualities fq; private int seeds; private Seed[] s; public Fruit(FruitQualities q, int seedCount) { fq = q; seeds = seedCount; s = new Seed[seeds]; for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.