Question
I need help with this assignment, I did some of it and it stuck on it, is there anyone can help me out with explain
I need help with this assignment, I did some of it and it stuck on it, is there anyone can help me out with explain please.
I don't know how to write the test case because I don't even know did I did right in this part ....
a. Create a class called Battery containing a battery number (as a unique identifier) of type int, a manufacturer of type String, and expiration date of type Date. Make it cloneable and implement its toString( ) method.
b. Create a class called Toy that contains a name of type String, and an array of Battery and a toString( ) method that prints out info about all its batteries. Write a clone( ) for Toy that automatically clones its name and all of its Battery objects.
c. Make sure that the clone( ) methods catch the CloneNotSupportedException rather than passing it to the caller, as shown in the Textbook.
d. Write test methods only for the clone( ) methods that will do the cloning and print out the input parameter(s), expected results, and actual results. Choose 2-3 appropriate inputs to validate that they perform deep cloning (and not do shallow cloning.)
Here is what I got right now.
import java.util.Date;
import java.util.GregorianCalendar;
public class Battery implements Cloneable {
private Date ExpireDate;
private String name;
private int battery;
public Battery(String name, int battery) {
this.name = name;
this.battery = battery;
ExpireDate = new Date();
}
public Battery clone() throws CloneNotSupportedException {
Battery cloned = (Battery) super.clone();
cloned.ExpireDate = (Date) ExpireDate.clone();
return cloned;
}
public void setExpireDate(int year, int month, int Day) {
Date newExpireDate = new GregorianCalendar(year, month - 1, Day).getTime();
ExpireDate.setTime(newExpireDate.getTime());
}
public String toString() {
return name;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started