5 Replies - 738 Views - Last Post: 30 March 2008 - 09:26 PM Rate Topic: -----

#1 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

Need help with the following errors

Post icon  Posted 30 March 2008 - 05:52 PM

I have been wroking on this forever it seems, and I can't seem to fix these last five error to make my program work and it's time to modify it some more. Please help!!
import java.text.DecimalFormat;

public class Inventory {

int inventorySize = 30;
private Product items[] = new Product [inventorySize];

DecimalFormat formatter = new DecimalFormat ("$##,###.00");


// Adds a product to the array of products.

public void addProduct(Product item) {
	for (int i =0; i < inventorySize; i++) {
		if (items[i] == null) {
			items[i] = item;
			return;
		}
	}
}

public double getTotalInvValue() {
	double sumOfInventory = 0.0;
	
	for (Product item : items) {
		if (item != null) {
			sumOfInventory += item.getItemValue();
		}
	}
	return sumOfInventory;
}

public void printInventory() {
	System.out.println("Printing items in inventory...\n");
	
	boolean hasItems = false;
	
	for (Product item : items) {
		if(item !=null) {
			hasItems = true;
			System.out.println(item.toString() + "Quantity:" + item.getQuantityOnHand() + "Value of Stock: " + formatter.format(item.getItemValue()));
			
		}
	}
	// If no items were found, print a message saying the inventory is empty
	if (!hasItems) {System.out.println("Inventory is empty at the moment. \n"); }
	
}
}

public class Product{
	//Private variables
	private String name;
	private int quantity;
	private double price;
	private int productid = 0;
	
	public Product() {
		this(0,"American Gangster",4,19.99);
		
	}
	public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
		productid = productId;
		setName(itemname);
		setQuantityOnHand(quantityOnHand);
		setPrice(itemprice);
	}
	
	public void setName(String itemname) {
		name = itemname;
	}
	
	public void setPrice(double itemPrice) {
		if (itemPrice > 0.00) {
			price =itemPrice;
		}
		else { price = 0.00;}
	}
	
	public String getName() {
		return name;
	}
	
	public int getQuantityOnHand() {
		return quantity;
	}
	
	public double getPrice() {
		return price;
	}
	
	public double getItemValue() {
		return (price * (double)quantity);
	}
	
	public String toString() {
		return name + "-" + price;
	}
}

// Inherited class DVD from the base class Product

public class DVD  {
	// Holds the year of movie
	private int movieyear;
	
	public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) {
		super(productId, itemname,quantityOnHand, itemprice);
		movieyear = year;
	}
	
	public void setYear(int year) {
		movieyear = year;
	}
	
	//Get the year of this DVD
	public int getYear() {
		return movieyear;
	}
	
	// adding a 5% restocking fee
	public double getItemValue() {
		return super.getItemValue() *1.05;
	}
	
	public double getRestockinkingFee() {
		return super.getItemValue() * .05;
	}
}



here is a list of the error that I have.

.\Product.java:13: cannot find symbol
symbol :variable productId
location class Product
productid=productId;
^
Product.java:15: cannot find symbol
symbol : method setQuantityOnHand(int)
location: class Product
setQuantityOnHand(quantityOnHand);
^

DVD.java:8:Object() in java.lang.Object cannot be applied to (int.java.lang.String,int,double)
super(productId,itemname,quantityOnHand,itemprice);

DVD.java:23: cannot find symbol
symbol : method getItemValue()
location: class java.lang.Object
return super.getItemValue() * 1.05;
^

DVD.java:27: cannot find symbol
symbol : method getItemValue()
location :class java.lang.Object
return super.getItemValue() * .05;
^

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with the following errors

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Need help with the following errors

Posted 30 March 2008 - 06:56 PM

Ok, lets hammer out these errors shall we!

Your first two errors are related to this function...

public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
        // Remember java is case sensitive so you put productID in your
        // function header so you must use that here
        // Notice the capital I and the capital D
        productid = productID;
        setName(itemname);

        // You forgot to create a function called setQuantityOnHand
        // you have the getQuantityOnHand, but not the set.
        setQuantityOnHand(quantityOnHand);
        setPrice(itemprice);
}



Ok, that hammers out the first two, the third one is related to your inhertance. Remember to inherit from a base class your class must use the "extends" keyword when defining the class name...

public class DVD extends Product {



Here we are saying that DVD inherits from Product because it EXTENDS it. Once you make this change, you should solve the problem about using super() because now super() will refer to the base class Product as it should. It should also solve your other two errors (4 and 5) because when you call super.getItemValue you are again referring to the base class "Product".

Without extending from product, it is assumed that your class is extending the base class of all classes called Object. Object doesn't have a method called getItemValue(). That is why you are getting those errors.

So here is your assignment back with all the fixes...

Inventory.java

import java.text.DecimalFormat;

public class Inventory {

int inventorySize = 30;
private Product items[] = new Product [inventorySize];

DecimalFormat formatter = new DecimalFormat ("$##,###.00");


// Adds a product to the array of products.

public void addProduct(Product item) {
    for (int i =0; i < inventorySize; i++) {
        if (items[i] == null) {
            items[i] = item;
            return;
        }
    }
}

public double getTotalInvValue() {
    double sumOfInventory = 0.0;
    
    for (Product item : items) {
        if (item != null) {
            sumOfInventory += item.getItemValue();
        }
    }
    return sumOfInventory;
}

public void printInventory() {
    System.out.println("Printing items in inventory...\n");
    
    boolean hasItems = false;
    
    for (Product item : items) {
        if(item !=null) {
            hasItems = true;
            System.out.println(item.toString() + "Quantity:" + item.getQuantityOnHand() + "Value of Stock: " + formatter.format(item.getItemValue()));
            
        }
    }
    // If no items were found, print a message saying the inventory is empty
    if (!hasItems) {System.out.println("Inventory is empty at the moment. \n"); }
    
}
}



Product.java...

public class Product{
    //Private variables
    private String name;
    private int quantity;
    private double price;
    private int productid = 0;
    
    public Product() {
        this(0,"American Gangster",4,19.99);
        
    }
    public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
	// Notice here the capital I and D (Case sensitive)
        productid = productID;
        setName(itemname);

	// I defined this function later down the class.
        setQuantityOnHand(quantityOnHand);
        setPrice(itemprice);
    }
    
    public void setName(String itemname) {
        name = itemname;
    }
    
    public void setPrice(double itemPrice) {
        if (itemPrice > 0.00) {
            price =itemPrice;
        }
        else { price = 0.00;}
    }

    // New defined function!!!
    public void setQuantityOnHand(int theQuantity) {
        quantity = theQuantity;
    }
    
    public String getName() {
        return name;
    }
    
    public int getQuantityOnHand() {
        return quantity;
    }

    
    public double getPrice() {
        return price;
    }
    
    public double getItemValue() {
        return (price * (double)quantity);
    }
    
    public String toString() {
        return name + "-" + price;
    }
}



Notice the new function and the change in the constructor to match the case sensitivity.

DVD.java

// Inherited class DVD from the base class Product

// Notice the word "extends" and it is extending Product class
public class DVD extends Product  {
    // Holds the year of movie
    private int movieyear;
    
    public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) {
        super(productId, itemname,quantityOnHand, itemprice);
        movieyear = year;
    }
    
    public void setYear(int year) {
        movieyear = year;
    }
    
    //Get the year of this DVD
    public int getYear() {
        return movieyear;
    }
    
    // adding a 5% restocking fee
    public double getItemValue() {
        return super.getItemValue() *1.05;
    }
    
    public double getRestockinkingFee() {
        return super.getItemValue() * .05;
    }
}



In the code you will see that I use the "extends" keyword to show the extension of the base class.

Here is a test driver I made for you to show you that everything is working.

inventorytest.java

public class inventorytest {
	public static void main(String args[]) {

		// Create a product, create a DVD which is a product
		Product clothes = new Product(1, "Shirt", 23, 44.50);
		DVD metallica = new DVD(2, "Metallica", 2, 200.00, 1992);

		// Create an inventory class
		Inventory inv = new Inventory();

		// Add the two products, one Product and DVD extended from Product
		inv.addProduct(clothes);
		inv.addProduct(metallica);

		// Print inventory value
		System.out.println("Inventory value: " + inv.getTotalInvValue());

	}
}



As you can see we created a product, we then created a DVD which is a type of product and extends it, add both to the inventory class, and print out the total value of inventory.

Edit: and yes I made metallica 200.00 on purpose... they rock. Plain and simple.

Enjoy!

"At DIC we be masters of the inventory class project... and code ninjas!" :snap:

This post has been edited by Martyr2: 30 March 2008 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Need help with the following errors

Posted 30 March 2008 - 06:59 PM

Martyr beat me to it...but not by much...

:P

and yes...Martyr is right, Metallica DOES rock.

One question I have though, how do you make your boxes with code specify the language on top, and show line numbers?

This post has been edited by Locke37: 30 March 2008 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need help with the following errors

Posted 30 March 2008 - 07:04 PM

If the class DVD inherits, as your comment say, Product you must define it as:

class DVD extends Product


In class product

public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
productid = productId;

should be uppercase d

productid = productID;


the method setQuantityOnHand(int quantity)

is not defined

Happy coding... good start

PBL


Sorry cross-posted while Martyr2 was answering everything

PBL
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Need help with the following errors

Posted 30 March 2008 - 07:15 PM

View PostLocke37, on 30 Mar, 2008 - 06:59 PM, said:

Martyr beat me to it...but not by much...

:P

and yes...Martyr is right, Metallica DOES rock.

One question I have though, how do you make your boxes with code specify the language on top, and show line numbers?


For various languages you can post the language type in the code tag... so instead of just [.code.] (minus the periods of course) you would write [.code=java.] for java.

The ones I have discovered so far are... java, cpp (for c++), csharp, vb, php, jscript and a few others

Since I don't post in the other languages I can't really say if there are more of them. I am sure there are.

Edit: Here is the official thread that mentions them all and how to do the highlighting...

DIC Syntax Highlighting

I hope that answers your question. :)

This post has been edited by Martyr2: 30 March 2008 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

Re: Need help with the following errors

Posted 30 March 2008 - 09:26 PM

View PostMartyr2, on 30 Mar, 2008 - 06:56 PM, said:

Ok, lets hammer out these errors shall we!

Your first two errors are related to this function...

public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
        // Remember java is case sensitive so you put productID in your
        // function header so you must use that here
        // Notice the capital I and the capital D
        productid = productID;
        setName(itemname);

        // You forgot to create a function called setQuantityOnHand
        // you have the getQuantityOnHand, but not the set.
        setQuantityOnHand(quantityOnHand);
        setPrice(itemprice);
}



Ok, that hammers out the first two, the third one is related to your inhertance. Remember to inherit from a base class your class must use the "extends" keyword when defining the class name...

public class DVD extends Product {



Here we are saying that DVD inherits from Product because it EXTENDS it. Once you make this change, you should solve the problem about using super() because now super() will refer to the base class Product as it should. It should also solve your other two errors (4 and 5) because when you call super.getItemValue you are again referring to the base class "Product".

Without extending from product, it is assumed that your class is extending the base class of all classes called Object. Object doesn't have a method called getItemValue(). That is why you are getting those errors.

So here is your assignment back with all the fixes...

Inventory.java

import java.text.DecimalFormat;

public class Inventory {

int inventorySize = 30;
private Product items[] = new Product [inventorySize];

DecimalFormat formatter = new DecimalFormat ("$##,###.00");


// Adds a product to the array of products.

public void addProduct(Product item) {
    for (int i =0; i < inventorySize; i++) {
        if (items[i] == null) {
            items[i] = item;
            return;
        }
    }
}

public double getTotalInvValue() {
    double sumOfInventory = 0.0;
    
    for (Product item : items) {
        if (item != null) {
            sumOfInventory += item.getItemValue();
        }
    }
    return sumOfInventory;
}

public void printInventory() {
    System.out.println("Printing items in inventory...\n");
    
    boolean hasItems = false;
    
    for (Product item : items) {
        if(item !=null) {
            hasItems = true;
            System.out.println(item.toString() + "Quantity:" + item.getQuantityOnHand() + "Value of Stock: " + formatter.format(item.getItemValue()));
            
        }
    }
    // If no items were found, print a message saying the inventory is empty
    if (!hasItems) {System.out.println("Inventory is empty at the moment. \n"); }
    
}
}



Product.java...

public class Product{
    //Private variables
    private String name;
    private int quantity;
    private double price;
    private int productid = 0;
    
    public Product() {
        this(0,"American Gangster",4,19.99);
        
    }
    public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
	// Notice here the capital I and D (Case sensitive)
        productid = productID;
        setName(itemname);

	// I defined this function later down the class.
        setQuantityOnHand(quantityOnHand);
        setPrice(itemprice);
    }
    
    public void setName(String itemname) {
        name = itemname;
    }
    
    public void setPrice(double itemPrice) {
        if (itemPrice > 0.00) {
            price =itemPrice;
        }
        else { price = 0.00;}
    }

    // New defined function!!!
    public void setQuantityOnHand(int theQuantity) {
        quantity = theQuantity;
    }
    
    public String getName() {
        return name;
    }
    
    public int getQuantityOnHand() {
        return quantity;
    }

    
    public double getPrice() {
        return price;
    }
    
    public double getItemValue() {
        return (price * (double)quantity);
    }
    
    public String toString() {
        return name + "-" + price;
    }
}



Notice the new function and the change in the constructor to match the case sensitivity.

DVD.java

// Inherited class DVD from the base class Product

// Notice the word "extends" and it is extending Product class
public class DVD extends Product  {
    // Holds the year of movie
    private int movieyear;
    
    public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) {
        super(productId, itemname,quantityOnHand, itemprice);
        movieyear = year;
    }
    
    public void setYear(int year) {
        movieyear = year;
    }
    
    //Get the year of this DVD
    public int getYear() {
        return movieyear;
    }
    
    // adding a 5% restocking fee
    public double getItemValue() {
        return super.getItemValue() *1.05;
    }
    
    public double getRestockinkingFee() {
        return super.getItemValue() * .05;
    }
}



In the code you will see that I use the "extends" keyword to show the extension of the base class.

Here is a test driver I made for you to show you that everything is working.

inventorytest.java

public class inventorytest {
	public static void main(String args[]) {

		// Create a product, create a DVD which is a product
		Product clothes = new Product(1, "Shirt", 23, 44.50);
		DVD metallica = new DVD(2, "Metallica", 2, 200.00, 1992);

		// Create an inventory class
		Inventory inv = new Inventory();

		// Add the two products, one Product and DVD extended from Product
		inv.addProduct(clothes);
		inv.addProduct(metallica);

		// Print inventory value
		System.out.println("Inventory value: " + inv.getTotalInvValue());

	}
}



As you can see we created a product, we then created a DVD which is a type of product and extends it, add both to the inventory class, and print out the total value of inventory.

Edit: and yes I made metallica 200.00 on purpose... they rock. Plain and simple.

Enjoy!

"At DIC we be masters of the inventory class project... and code ninjas!" :snap:

you are a life saver, I still have a few parts to go so I'll be back I'm sure. Thanks again

Edit:and yes mettallica rocks, but I prefer their older stuff like Master of Puppets and Ride the Lightning.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1