inheritance

//class ProductApp work as the driver
public class ProductApp {
    public static void main(String[] args) {
        Product[] list = new Product[6];
        list[0] = new ImperialUnit("I9001", 1, 2, 610.09);
        list[1] = new ImperialUnit("I9002", 2, 4, 551.37);
        list[2] = new ImperialUnit("I9003", 1, 3, 392.28);
        list[3] = new MetricUnit("M8001", 0.62013, 443.51);
        list[4] = new MetricUnit("M8002", 1.10025, 601.15);
        list[5] = new MetricUnit("M8003", 0.53038, 440.27);

        System.out.println(
                "---------------------------------------------Report---------------------------------------------");
        for (Product items : list) {
            if (items != null) {
                System.out.println(items.toString());
            }
        }
    }
}

// class Product
abstract class Product {
    private String productID;
    private double volume;

    public Product() {
        this.productID = "";
        this.volume = 0;
    }

    public Product(String productID, double volume) {
        this.productID = productID;
        this.volume = volume;
    }

    public String getProductID() {
        return productID;
    }

    public void setProductID(String productID) {
        this.productID = productID;
    }

    public double getVolume() {
        return volume;
    }

    public void setVolume(double volume) {
        this.volume = volume;
    }

    public abstract double calculateMassInGrams();

    public double calculateDensity() {
        if (volume == 0) {
            return 0;
        } else {
            return calculateMassInGrams() / volume;
        }
    }

    public String analysis() {
        double density = calculateDensity();

        if (density < 1.25) {
            return "Too thin.";
        } else if (density > 1.55) {
            return "Too thick.";
        } else {
            return "Acceptable.";
        }
    }

    @Override
    public String toString() {
        String result;
        result = String.format("ID = %-10s Mass = %-10.2f Volume = %-10.2f Density = %-10.2f Analysis = %-15s",
                getProductID(), calculateMassInGrams(), getVolume(), calculateDensity(), analysis());
        return result;
    }

}

// class MetricUnit
class MetricUnit extends Product {
    private double kilograms;

    public MetricUnit() {
        super();
        this.kilograms = 0;
    }

    public MetricUnit(String productID, double kilograms, double volume) {
        super(productID, volume);
        this.kilograms = kilograms;
    }

    @Override
    public double calculateMassInGrams() {
        return kilograms * 1000;
    }

}

// class ImperialUnit
class ImperialUnit extends Product {
    private double pounds;
    private double ounces;

    public ImperialUnit() {
        super();
        this.pounds = 0;
        this.ounces = 0;
    }

    public ImperialUnit(String productID, double pounds, double ounces, double volume) {
        super(productID, volume);
        this.pounds = pounds;
        this.ounces = ounces;
    }

    @Override
    public double calculateMassInGrams() {
        return (pounds * 16 + ounces) * 28.35;
    }

}

 

Scroll to Top