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

Get source files here: http://murach.com/servlet/murach.downloads.DownloadServle

ID: 3550458 • Letter: G

Question

Get source files here:

http://murach.com/servlet/murach.downloads.DownloadServlet?file=javp_allfiles.zip

Need help with Exercise 8-4:  Code an equals method

In this exercise, you'll add an equals method to the Product and LineItem classes that you can use to compare the instance variables of two objects.

1.  Open the porject named ch08_ex4_EqualsTest in the ex_starts directory. This application creates an compares two Product objects and two LineItem objects using the equals method. Review this code to see how it works.

2.  Run the project. Since the equals method isn't overridden in the Product or LineItem class, the output from this application should indicate that the comparisons are based on object references and not the data the objects contain.

3.  Open the Product class, and add an equals method like the one shown in figure 8-15. Then, run the project again. This time the output should indicate that the products are being compared based on their data and not their references.

4.  Repeat step 3 for the LineItem class. This time, the comparisons for both the products and line items should be based on their data.


Figure 8-15

@Override

public boolean equals(Object object)

{

     if (object instanceof Product)

     {

          Product product2 = (Product) object;

          if

          (

               code.equals(product2.getCode()) &&

               description.equals(product2.getDescription())) &&

               price == product2.getPrice()

          )

               return true;

      }

      return false;

}


Explanation / Answer

import java.text.NumberFormat;

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

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

    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()
    {
        double price = product.getPrice();
        total = quantity * price;
    }

    public String getFormattedTotal()
    {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(this.getTotal());
    }
    public boolean equals(LineItem L)
    {
        return (product.equals(L.product) && quantity == L.quantity);
    }
}



import java.text.NumberFormat;

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

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

    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()
    {
        String message =
            "Code:        " + code + " " +
            "Description: " + description + " " +
            "Price:       " + this.getFormattedPrice() + " ";
        return message;
    }
   public boolean equals(Product P)
    {
        return (code.equalsIgnoreCase(P.code) && description.equalsIgnoreCase(P.description) && price==P.price);
    }

}

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