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

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...
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...
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...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Note: Strictly no copy paste, write in your language. Q. What are the steps when using...
Note: Strictly no copy paste, write in your language. Q. What are the steps when using lean thinking?
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use...
This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use it as a base class: /** * Simple representation of a bank account * * @author Jo Belle * @version 0.5 (10/12/2020) */ import java.text.NumberFormat; public class SimpleBankAccount{ // fields (instance variables) private double balance; private String accountId; /** * Constructor for objects of class SimpleBankAccount */ public SimpleBankAccount(){ balance = 0.0; accountId = ""; } /** * Constructor for objects of class SimpleBankAccount...
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT