Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code for the Customer class so that it can be cloned. You don\'t need

ID: 3696292 • Letter: W

Question

Write the code for the Customer class so that it can be cloned. You don't need to code a constructor or get and set methods. (Refer to class diagram 8-3.) Write code that creates a Customer object named cl and then clones it and stores the clone in an object named c2. (Refer to class diagram 8-3.) Write the code for the Account class so that it can be cloned. You don't need to code a constructor or get and set methods, but you should assume that they are coded for each instance variable. You can also assume that the Customer class implements the Cloneable interface.

Explanation / Answer

/**
* @author srinu
*
*/
public class Customer implements Cloneable {

   String name;
   String address;
   String city;
   String state;
   String zip;

   /**
   * @param name
   * @param address
   * @param city
   * @param state
   * @param zip
   */
   public Customer(String name, String address, String city, String state,
           String zip) {
       this.name = name;
       this.address = address;
       this.city = city;
       this.state = state;
       this.zip = zip;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the address
   */
   public String getAddress() {
       return address;
   }

   /**
   * @param address
   * the address to set
   */
   public void setAddress(String address) {
       this.address = address;
   }

   /**
   * @return the city
   */
   public String getCity() {
       return city;
   }

   /**
   * @param city
   * the city to set
   */
   public void setCity(String city) {
       this.city = city;
   }

   /**
   * @return the state
   */
   public String getState() {
       return state;
   }

   /**
   * @param state
   * the state to set
   */
   public void setState(String state) {
       this.state = state;
   }

   /**
   * @return the zip
   */
   public String getZip() {
       return zip;
   }

   /**
   * @param zip
   * the zip to set
   */
   public void setZip(String zip) {
       this.zip = zip;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#clone()
   */
   public Object clone() {
       try {
           return (Customer) super.clone();
       } catch (CloneNotSupportedException e) {
           // This should never happen
       }
       return null;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Customer [name=" + name + ", address=" + address + ", city="
               + city + ", state=" + state + ", zip=" + zip + "]";
   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       Customer c1 = new Customer("Srinivas", "address", "VSKP", "AP",
               "530018");
       System.out.println("C1 :" + c1);
       Customer c2 = (Customer) c1.clone();
       System.out.println("C2 :" + c2);
   }
}

/**
* @author srinu
*
*/
public class Account {

   int number;
   Customer customer;
   String type;
   double balance;

   /**
   * @return the number
   */
   public int getNumber() {
       return number;
   }

   /**
   * @param number
   * the number to set
   */
   public void setNumber(int number) {
       this.number = number;
   }

   /**
   * @return the customer
   */
   public Customer getCustomer() {
       return customer;
   }

   /**
   * @param customer
   * the customer to set
   */
   public void setCustomer(Customer customer) {
       this.customer = customer;
   }

   /**
   * @return the type
   */
   public String getType() {
       return type;
   }

   /**
   * @param type
   * the type to set
   */
   public void setType(String type) {
       this.type = type;
   }

   /**
   * @return the balance
   */
   public double getBalance() {
       return balance;
   }

   /**
   * @param balance
   * the balance to set
   */
   public void setBalance(double balance) {
       this.balance = balance;
   }

}

OUTPUT:

C1 :Customer [name=Srinivas, address=address, city=VSKP, state=AP, zip=530018]
C2 :Customer [name=Srinivas, address=address, city=VSKP, state=AP, zip=530018]

Note : this output is for Customer classs ,and nothing mentioned for Account class