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

You need the source code to the Murach\'s Java Programming book found here: http

ID: 3550645 • Letter: Y

Question

You need the source code to the Murach's Java Programming book found here:

http://www.murach.com/downloads/javp.htm


In this exercise, you'll implement the Cloneable interface for the Product and Lineltem classes. Open the project named ch09_ex3_CloneableTest in the ex_starts directory. Display the ProductCloneApp class and review its code. Note that this class contains an error because the clone method has protected access in the Object class and isn't available from the Product class. Implement the Cloneable interface for the Product class. When you do, the ProductCloneApp class should no longer display an error. Run the ProductCloneApp class to make sure it works correctly. To do that, you can right - click on this class and select the Run File command. Repeat steps 1 through 3 for the LineltemCloneApp and Lineltem classes.

Explanation / Answer

/*
OUTPUT for ProductCloneApp
Welcome to the Product Clone Test
SUCCESS: The clone method of the Product class is cloning data.
*/

/* OUTPUT for LineItemCloneApp
Weclome to the Line Item Clone Test

Code: java
Description: Murach's Beginning Java
Price: $44.50
Quantity: 3
Total: $133.50

Code: java
Description: Murach's Beginning Java
Price: $44.50
Quantity: 2
Total: $89.00
*/

import java.text.NumberFormat;

public class LineItem implements Cloneable
{
    private Product product;
    private int quantity;
    private double total;

    public LineItem()
    {
        this.product = new Product();
        this.quantity = 0;
        this.total = 0;
    }

    public LineItem(Product product, int quantity)
    {
        this.product = product;
        this.quantity = quantity;
    }

    public void setProduct(Product product)
    {
        this.product = product;
    }

    public Product getProduct()
    {
        return product;
    }

    public void setQuantity(int quantity)
    {
        this.quantity = quantity;
    }

    public int getQuantity()
    {
        return quantity;
    }

    public double getTotal()
    {
        this.calculateTotal();
        return total;
    }

    private void calculateTotal()
    {
        total = quantity * product.getPrice();
    }

    public String getFormattedTotal()
    {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(this.getTotal());
    }

    @Override
    public String toString()
    {
        return
            "Code: " + product.getCode() + " " +
            "Description: " + product.getDescription() + " " +
            "Price: " + product.getFormattedPrice() + " " +
            "Quantity: " + quantity + " " +
            "Total: " + this.getFormattedTotal() + " ";
    }
    public Object clone() throws CloneNotSupportedException
    {
    return super.clone();
    }
}

import java.text.NumberFormat;

public class Product implements Cloneable
{
    private String code;
    private String description;
    private double price;

    public Product()
    {
        code = "";
        description = "";
        price = 0;
    }

    public Product(String code, String description, double price)
    {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public String getCode(){
        return code;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public String getDescription()
    {
        return description;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    public double getPrice()
    {
        return price;
    }

    public String getFormattedPrice()
    {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(price);
    }

    @Override
    public String toString()
    {
        return "Code:        " + code + " " +
               "Description: " + description + " " +
               "Price:       " + this.getFormattedPrice() + " ";
    }
    public Object clone() throws CloneNotSupportedException
    {
    return super.clone();
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote