Question: public class Item { //variables private String name; private String vendor; private double salePrice; private double costPrice; private double weight; private boolean taxable; //Constructor public
public class Item { //variables private String name; private String vendor; private double salePrice; private double costPrice; private double weight; private boolean taxable; //Constructor public Item(String n,double cp, double sp) { name=n; costPrice=cp; salePrice=sp; weight=1; taxable=true; } //Accessors public String getName() { return name; } public String getVendor() { return vendor; } public double getSalePrice() { return salePrice; } public double getCostPrice() { return costPrice; } public double getWeight() { return weight; } public boolean getTaxable() { return taxable; } //mutuators public void setVendor(String v) { vendor=v; } public void setWeight(double w) { weight=w; } public void setTaxable(boolean tax) { taxable=tax; } //increaseCost public void increaseCost() { costPrice=costPrice+1; } //profit public double profit() { return salePrice-costPrice; } public static void main(String[] args) { //ItemTester? Item chair = new Item("Desk Chair", 30, 55); //increase cost price by $3. chair.increaseCost(); chair.increaseCost(); chair.increaseCost(); //display the profit System.out.println("The chair's profit is now $" + chair.profit()); //set the chairs weight to 7 lb chair.setWeight(7); Item table = new Item("Picnic Table", 70,88); System.out.println("The table's profit is now $" + table.profit()); } }
Assignment 6 Part I This is a continuation of the Item class we designed in the last assignment Add a String method to your Item class. This method does not accept any parameters and returns a String reflecting the current state of the object. Once you have written the toString method, the following statement will display the contents of all member variables in an Item object called chair: System out.println (chair); Part II Create an array of Item objects. Then write a loop that calculates the total weight of the Items in the array. Since the weight is a private data member of the Item class, the loop will need to call an accessor method to query the weight of each Item in the arrayStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
