Question

In: Computer Science

//Using Java language Write a copy instructor for the following class. Be as efficient as possible....

//Using Java language

Write a copy instructor for the following class. Be as efficient as possible.

import java.util.Random;

class Saw
{
   private int x;
   private Integer p;

//------------------------------------------
public Saw()

{

Random r = new Random();
       x = r.nextInt();
       p = new Integer(r.nextInt());

}

}

Solutions

Expert Solution

there is no concept called copy instructor java have only copy constructor.

A copy constructor is a constructor that creates a new object using an existing object of the same class and initializes each instance variable of newly created object with corresponding instance variables of the existing object passed as argument.

Saw.java

import java.util.Random;

public class Saw {
    private int x;
    private Integer p;
    //default (Normal) constructor
    public Saw(){
        Random r=new Random(); //Random is a class used for genrating random numbers
        x=r.nextInt();
        p=new Integer(r.nextInt());
    }

    //copy constructor
    //it will create a new obj by using  already existing obj
    public Saw(Saw saw){
        System.out.println("copy constructor called");
        x=saw.x;
        p=saw.p;
    }

    // Overriding the toString of Object class
    @Override
    public String toString() {
        return "Saw{" +
                "x=" + x +
                ", p=" + p +
                '}';
    }
}

Demo.java

import java.util.*;

public class SDemo {
    public static void main(String[] args) {
        // create a object1 for Saw class it will call a default constructor
        Saw object1=new Saw();
        //printing the values object1
        System.out.println(object1);
        //creating object2 by using object1
       // Following involves a copy constructor call
        Saw object2=new Saw(object1);
        //printing the values object2
        System.out.println(object2);

    }

}

"C:\Program Files Javaljdk1.8.0 121lbinljava.exe" Sawx-1358831207, p-1672367332) copy constructor called Sawx-1358831207, p-1672367332) Process finished with exit code O

"C:\Program Files Javaljdk1.8.0 121lbinljava.exe" Saw/XF-972383579, p= 11 7 3 6 4 4 7 51) copy constructor called Saw x-972383579, p-1173644751) Process finished with exit code O


Related Solutions

Please use Java language to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add new instance variables or modify existing ones. private LinkedNode<T> head; private LinkedNode<T> tail; private int size; Q1: Write a method called "public void addFirst(T data)" that does the following: * Adds the element to the front of the deque. * Must be O(1) * @param data the data to add to the front of the deque * @throws java.lang.IllegalArgumentException if data is null Q2:...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT