Question

java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

java questions:

1. Explain the difference between a deep and a shallow copy.

2. Given the below classes:

public class Date {

   private String month;

   private String day;

   private String year;

   public Date(String month, String day, String year) {

      this.month = month;

      this.day = day;

      this.year = year;

   }

   public String getMonth() {

      return month;

   }

   public String getDay() {

      return day;

   }

   public String getYear() {

      return year;

   }

  

   @Override public String toString() {

      return month + "/" + day + "/" + year;

   }

}

public class Dog {

    private String name;

    private double weight;

    private Date birthday;

    public Dog(String name, double weight, Date birthday) {

        this.name = name;

        this.weight = weight;

      this.birthday = birthday;

    }

    public String getName() {

        return name;

    }

    public double getWeight() {

        return weight;

    }

    public Date getBirthday() {

        return birthday;

    }

    public void printGreeting() {

        System.out.println("Woof!");

    }

    @Override public String toString() {

        return "Name: " + name

            + "\nWeight: " + weight

            + "\nDate: " + birthday.toString();

    }

}

  • Write a copy constructor for the above Dog class that creates a deep copy.

3. There is a problem with the below mutator method. Correct the problem in two different ways:

public void setAge(int age) {

    age = age;

}

4. Write a complete class named Car. It has three private member variables make (String), model (String) and mileage (double). It has 3 constructors - a default constructor, a 3-argument constructor and a copy constructor. It has accessor and mutator methods for all variables. It has a toString() method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans 1:

In shallow copy, only fields of primitive data type are copied while the objects references are not copied. Deep copy involves the copy of primitive data type as well as objet references


Ans 2:

public Dog(String name, double weight, Date birthday) {

        this.name = new String(name);

        this.weight = weight;

        this.birthday = new Date(birthday.getTime());;

    }

Ans 3:

public void setAge(int age) {

    this.age = age;

}

Ans 4:

public class Car {
       private String make;
       private String model;
       private double mileage;
      
       //default constructor
       public Car() {
           super();
           this.make = "";
           this.model = "";
           this.mileage = 0.00;
       }
      
       //Parameterized constructor
       public Car(String make, String model, double mileage) {
           this.make = make;
           this.model = model;
           this.mileage = mileage;
       }
      
      
       //Copy Constructor
       public Car(Car c) {
           this.make = c.make;
           this.model = c.model;
           this.mileage = c.mileage;
       }
      
      
      
       public String getMake() {
           return make;
       }
       public void setMake(String make) {
           this.make = make;
       }
       public String getModel() {
           return model;
       }
       public void setModel(String model) {
           this.model = model;
       }
       public double getMileage() {
           return mileage;
       }
       public void setMileage(double mileage) {
           this.mileage = mileage;
       }
       @Override
       public String toString() {
           return "Car [make=" + make + ", model=" + model + ", mileage=" + mileage + "]";
       }
      
      
}

Add a comment
Know the answer?
Add Answer to:
java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • My values are not storing into my object and I keep returning zero. please explain my...

    My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0;    public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); }          public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...

  • this needs to be in Java: use a comparator class which is passed into the sort...

    this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...

  • Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding...

    Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding one to all dates. Run your code using the attached input files outputlong.txtoutputlab3.txt. Please note that your the dates that are sorted will be the ones after 1 day has been added to them. Sample Output: (green is user input) Run #2 using sample file outputlab3.txt Enter name of file to import or the word null to bypass: outputlab3.txt How many assessments in this...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Amend the Date.java file on p. 77 (also available on the lab drive) so that the...

    Amend the Date.java file on p. 77 (also available on the lab drive) so that the Date class: a. Has a month field that is a String instead of an int. b. Has a third constructor that will take in a String parameter (only.) This constructor will parse the String into the month, day, and year fields. The String is provided in the following format: “Month Day, Year”. If the tokenizing and/or parsing generates an exception, the date defaults to...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • solve it in c++ 10 note: please do not give me same answer like this 1....

    solve it in c++ 10 note: please do not give me same answer like this 1. Define a Pet class that stores the pet's name, age, and weight. Add appropriate constructors, accessor functions, and mutator functions. Also define a function named getLifespan that returns a string with the value "unknown lifespan." Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT