Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee.
Click the Assignment Files tab to submit your assignment.

I have been trying to figure this out all week can someone help me with this? The assignment is due today.

package cars;
// Robert Sigmon
// 10/12/2014 11:00 AM

public class CarsInventory {


public static void main( String[] args ) {  

//instantiate cars object
     Cars[] products = new Cars[3];

      products[0] = new Cars(2479, "BMW M6", 45, 65000);
   products[1] = new Cars(2480, "Nissan Leaf", 23, 35000);
   products[2] = new Cars(2481, "Ford Focus", 17, 20000);

// get car information 
System.out.println("RGS New and Used Car Inventory Program");

    for (Cars product : products) {
        System.out.printf("%s %d\n", "Product Number:", product.getProdNumber());

    int model = 0;
System.out.printf( "%s %s\n", "Product Name:", product.getProdName() );
System.out.printf( "%s %d\n", "Total Units in Stock:", product.getUnitsTotal() );
System.out.printf( "%s $%.2f\n", "Price Per Unit:", product.getUnitPrice() );
System.out.printf( "%s $%.2f\n", "Inventory Value:", product.getValue() ); // printing of inventory value

    }
} // end main method
} // end class CarsInventory

Any Idea where I would put the modification? 

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example).

so; create a subclass of product with one additional variable

In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class.

override the method in the product class that calculates the value of the inventory, which (imho) makes no sense, since the calculation would be there for all instances of the article. this means, each instance 'knows' how many instances of it have been created. this looks like static members are in play, while this has nothing to do with the functionality of the class (at least not in decent OO design)

The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

about the same, it just describes how to alter previous method

Modify the output to display this additional feature you have chosen and the restocking fee.

this describes how to "alter the output" of that method, which makes even lesser sense. a calculation method should not return a formatted output, it should return a numeric value, which is the outcome of the actual calculation.

I have been trying to figure this out all week can someone help me with this? The assignment is due today.

if you have been trying all week, and have to hand it in today, well, I hate to say this, but if you had paid attention to either your course, or your textbook, you would have finished this in a matter of minutes, rather than days. if still it wouldn't have worked, you really shouldn't have waited days before asking for help.

First let me start off by saying that I am new to programing and I should have rephrased that statement. I have not been trying all week. I have been trying since thursday. I turned in part 2 on Wed. Thank you for the help.
I have this much done with the code but I am still getting errors.

package cars.inventory.restock;

/**
 *
 * @author Robbie
 */
public class CarsInventoryRestock {



    private String supplier;
    private float restockfee;
    CarsInventoryRestock()
    {
        super();
        supplier = "Unknown";
        restockfee = 0;
    }
    CarsInventoryRestock(String productName, int productID, float unitPrice, long unitsInStock, String supplier)
    {
        super(productName, productID, unitPrice, unitsInStock);
        this.supplier = supplier;
    }
    public String getSupplier()
    {
        return supplier;
    }
    public void setProducer(String supplier)
    {
        this.supplier = supplier;
    }
    public float getRestockFee()
    {
        return restockfee;
    }
    public float getTotalValue()
    {
        restockfee = (5*super.getTotalValue())/100;
        //added 5% restocking fee
        return (super.getTotalValue() + restockfee); 
    }
}  //ends class Cars Inventory Restock   

I get errors on lines 21,38, and 40.

Edited 2 Years Ago by rob.sigmon.1

it doesn't matter what errors you get. you either didn't read, or you didn't understand your assignment. read it again.

first issue, you exstend Object, and object has no constructor you can call with:

super(productName, productID, unitPrice, unitsInStock);

the same for the other issues:

 restockfee = (5*super.getTotalValue())/100;
//added 5% restocking fee
return (super.getTotalValue() + restockfee); 

the Object class has no method getTotalValue(), so (trying to) cal it, you'll get in trouble.

Again you are right I do not understand the assignment which is why I am asking for help. Am I just putting the mod in the wrong part of the application? This application has 3 other files.

In order to create a sub-class of an existing class, you need to use the extends keyword and the name of the class being extended in the declaration of the new class.

class UsedCar extends Car {

    // ...

}

Furthermore, the parent class would have to have the methods which are to be overridden. In this case, it would be getUnitPrice(), though as Stultuske points out, as an assignment goes this example doesn't make much sense.

Edited 2 Years Ago by Schol-R-LEA

Ok so in this part of the application?

package cars;
// Robert Sigmon
// 10/12/2014 11:00 AM
public class Cars {

    private final int prodNumber; // product number
    private final String prodName;// product name
    private final int unitsTotal; // total units in stock
    private final double unitPrice; // price per unit

    //initialize four-arguement constructor

    public Cars (int number, String name, int total, double price)
    {
        prodNumber = number;
        prodName = name;
        unitsTotal = total; //validate and store total of cars
        unitPrice = price; // validate and store price per car

    } //end four-arguement constructor

    public int getProdNumber() {
        return prodNumber;
    }   


    public String getProdName() {
        return prodName;
    }


    public int getUnitsTotal() {
        return unitsTotal;
    }


    public double getUnitPrice() {
        return unitPrice;
    }


    public double getValue() {
        return unitsTotal * unitPrice;
    }

} // end class cars

Since that's the class you're sub-classing, yes. You would have this class in one file, and your sub-class in another; both need to be declared as public classes.

Edited 2 Years Ago by Schol-R-LEA

Ok Should it look like this...

package cars.inventory.restock;


public class CarsImpl extends CarsInventoryRestock {

/**
 *
 * @author Robbie
 */

    private String supplier;
    private float restockfee;
    CarsImpl()
    {
        super();
        supplier = "Unknown";
        restockfee = 0;
    }
    CarsImpl(String productName, int productID, float unitPrice, long unitsInStock, String supplier)
    {
        super(ProdName, ProdNumber, UnitPrice, UnitsTotal);
        this.supplier = supplier;
    }
    public String getSupplier()
    {
        return supplier;
    }
    public void setProducer(String supplier)
    {
        this.supplier = supplier;
    }
    public float getRestockFee()
    {
        return restockfee;
    }
    public float getTotalValue()
    {
        restockfee = (5*super.getTotalValue())/100;
        //added 5% restocking fee
        return (super.getTotalValue() + restockfee); 
    }
}  //ends class Cars Inventory Restock   

Now I get errors on line 26.

without telling us what exactly line 26 is, and what code exists in class
CarsInventoryRestock, it'll be pretty hard for us to tell what's wrong.

This is the code with the errors

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cars.inventory.restock;


public class CarsImpl extends CarsInventoryRestock {

/**
 *
 * @author Robbie
 */

    private String supplier;
    private float restockfee;
    CarsImpl()
    {
        super();
        supplier = "Unknown";
        restockfee = 0;
    }
    CarsImpl(String productName, int productID, float unitPrice, long unitsInStock, String supplier)
    {
        super(ProdName, ProdNumber, UnitPrice, UnitsTotal);
        this.supplier = supplier;
    }
    public String getSupplier()
    {
        return supplier;
    }
    public void setProducer(String supplier)
    {
        this.supplier = supplier;
    }
    public float getRestockFee()
    {
        return restockfee;
    }
    public float getTotalValue()
    {
        restockfee = (5*super.getTotalValue())/100;
        //added 5% restocking fee
        return (super.getTotalValue() + restockfee); 
    }
}  //ends class Cars Inventory Restock   

yyyeeeeeaaaahhh ...
there's a reason why I said

without telling us what exactly line 26 is, and what code exists in class
CarsInventoryRestock, it'll be pretty hard for us to tell what's wrong.

yes, I understand that that is the code in which you have an error, but the lines we see here are not always the same as the one your compiler sees as line 26.

secondly: it looks like a super call is what is causing the problem. in which case, we need to see what the code of the parent class is. because of that is the problem, it's that you are calling a constructor in the parent class that doesn't exist. so, that would be your problem. but, since we don't know what your line 26 is, which your compiler is telling you, or how can we check whether that constructor in the parent class does or does not exist, if you assume showing that code is not relevant?

Actually the real problem is that you're getting the parent-child relationship backwards: you want CarsInventoryRestock to be a sub-class of Cars, but right now you have it the other way around. It should look like:

public class CarsInventoryRestock extends Cars {

with the parent class being the one extended.

Edited 2 Years Ago by Schol-R-LEA

Ok Iam going to post all of the files here...

package cars;
// Robert Sigmon
// 10/12/2014 11:00 AM
public class Cars {

    private final int prodNumber; // product number
    private final String prodName;// product name
    private final int unitsTotal; // total units in stock
    private final double unitPrice; // price per unit

    //initialize four-arguement constructor

    public Cars (int number, String name, int total, double price)
    {
        prodNumber = number;
        prodName = name;
        unitsTotal = total; //validate and store total of cars
        unitPrice = price; // validate and store price per car

    } //end four-arguement constructor

    public int getProdNumber() {
        return prodNumber;
    }   


    public String getProdName() {
        return prodName;
    }


    public int getUnitsTotal() {
        return unitsTotal;
    }


    public double getUnitPrice() {
        return unitPrice;
    }


    public double getValue() {
        return unitsTotal * unitPrice;
    }

} // end class cars

package cars.list;

/**
 *
 * @author Robbie
 */
//Stores a Car List
public class CarsList {
    private int item;
    private String name;
    private int units;
    private double price;

//  constructor
    public CarsList(int item, String name, int units, double price) {
        this.item = item;
        this.name = name;
        this.units = units;
        this.price = price;
    }

//  total value
    public double value() {
        return units*price;
    }

//  getters and setters

    public int getItem() {
        return item;
    }

    public void setItem(int item) {
        this.item = item;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUnits() {
        return units;
    }

    public void setUnits(int units) {
        this.units = units;
    }

    public double getPrice() {
        return price;
    }

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

}

package cars;
// Robert Sigmon
// 10/12/2014 11:00 AM

public class CarsInventory {


public static void main( String[] args ) {  

//instantiate cars object
     Cars[] products = new Cars[3];

      products[0] = new Cars(2479, "BMW M6", 45, 65000);
   products[1] = new Cars(2480, "Nissan Leaf", 23, 35000);
   products[2] = new Cars(2481, "Ford Focus", 17, 20000);

// get car information 
System.out.println("RGS New and Used Car Inventory Program");

    for (Cars product : products) {
        System.out.printf("%s %d\n", "Product Number:", product.getProdNumber());

    int model = 0;
System.out.printf( "%s %s\n", "Product Name:", product.getProdName() );
System.out.printf( "%s %d\n", "Total Units in Stock:", product.getUnitsTotal() );
System.out.printf( "%s $%.2f\n", "Price Per Unit:", product.getUnitPrice() );
System.out.printf( "%s $%.2f\n", "Inventory Value:", product.getValue() ); // printing of inventory value

    }
} // end main method
} // end class CarsInventory

OK, then. There's no harm in it, I just was wondering.

At this point, you should have changed the class declaration for CarsInventoryRestock as I recommended, right? What was the result of that?

(And don't forget that the file name has to match the public class.)

Edited 2 Years Ago by Schol-R-LEA

Are you getting any errors on the last code now? If so which if not, what questions do you have about it?

package cars;


public class CarsImpl extends Cars {

/**
 *
 * @author Robbie
 */

    private String supplier;
    private float restockfee;
    CarsImpl()
    {
        super();
        supplier = "Unknown";
        restockfee = 0;
    }
    CarsImpl(String productName, int productID, float unitPrice, long unitsInStock, String supplier)
    {
        super(ProdName, ProdNumber, UnitPrice, UnitsTotal);
        this.supplier = supplier;
    }
    public String getSupplier()
    {
        return supplier;
    }
    public void setProducer(String supplier)
    {
        this.supplier = supplier;
    }
    public float getRestockFee()
    {
        return restockfee;
    }
    public float getTotalValue()
    {
        restockfee = (5*super.getTotalValue())/100;
        //added 5% restocking fee
        return (super.getTotalValue() + restockfee); 
    }
}  //ends class Cars Inventory Restock   

Errors on lines 15,21, and 36

Edited 2 Years Ago by rob.sigmon.1

Cannot find symbol
symbol: variable ProdName
location: class CarsImpl

Cannot find symbol
symbol: variable ProdNumber
location: class CarsImpl

Cannot find symbol
symbol: variable UnitPrice
location: class CarsImpl

Cannot find symbol
symbol: variable UnitTotal
location: class CarsImpl
That is on line 26 on my program but line 25 on here

Edited 2 Years Ago by rob.sigmon.1

You are using different variable names in the super() call than you used in the parameter list for CarsImpl. Try the following:

super(productName, productID, unitPrice, unitsInStock);

OK, looking at the Cars class again, I can see you have the first two parameters reversed (and the last two as well);

public Cars (int number, String name, int total, double price)

so the call should in fact be

    super(productID, productName, unitsInStock, unitPrice);

I would recommend changing the CarsImpl c'tor to match as well:

CarsImpl(int productID, String productName, long unitsInStock, float unitPrice, String supplier)

Edited 2 Years Ago by Schol-R-LEA

ok in the CarsImpl Where it sayssuper(); I get the error Constructor Cars in class Cars cannot be applied to given types;
required: int,string,int,double
found: no arguments
reason: actual and formal arguement list differs in length.

And where it says super(productID, productName, unitsInStock, unitPrice);
I get the error Incompatible types: possible lossy conversation from long to int

The first problem is because you are attempting to call a default constructor, but you didn't define one for the Cars class. Normally, if you don't define a c'tor, Java will define a default one for you; but if you define a non-default c'tor, then it doesn't generate the default c'tor, so there isn't one unless you explicitly implement it.

The second problem is because the type of the unitsInStock parameter for CarsImpl is long, while the Cars parameter total is an int, which is smaller. By trying to go from a long to an int, you are potentially losing data, hence the error. You have ther reverse problem with unitPrice and price. The solution is to change both so that they are both long and double, respectively:

public Cars (long number, String name, long total, double price)

and

CarsImpl(long productID, String productName, long unitsInStock, double unitPrice, String supplier)

Note that I made the ID numebr long as well.

Edited 2 Years Ago by Schol-R-LEA

I still have the error on the lines that say super

Edited 2 Years Ago by rob.sigmon.1

ok in the CarsImpl Where it says super(); I get the error Constructor Cars in class Cars cannot be applied to given types;
required: long,string,long,double
found: no arguments
reason: actual and formal arguement list differs in length.

Edited 2 Years Ago by rob.sigmon.1

Ah, this is what I explained earlier about default constructors. If you want a default c'tor for CarsImpl that calls a default c'tor for Cars, you need to add a default c'tor to Cars.

This article has been dead for over six months. Start a new discussion instead.