Results 1 to 6 of 6
  1. #1
    RRasco is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default [Solved] Classes with object attributes

    Okay, I have no idea what to call this problem, I am pretty new to Java and need some assistance.

    I have an inventory program I am building for a school project and am running into an issue trying to provide data to objects. This program is supposed to use static data and output each item in a stock/inventory kind manner. I have three files, Main, Product, and CD. Essentially, the Product class is supposed to be movies, and the CD class is for CDs. At first I was supplying class variables in each class file and just using that data, but I am now trying to provide attributes to the objects in the Main file. Like this:

    Java Code:
        // create our inventory objects
        private static Product items[] = {
            new CD("Staind - The Illusion of Progress", 102, 6, 12.99, "Rock", "CD"), 
            new Product("Boondock Saints", 101, 4, 14.99, "DVD") 
        };
    The problem is I am receiving a "cannot find symbol" error. Symbol: constructor Product(). This is occuring on the line indicated by the comment. I have done some debugging and found that if I remove the public Product() portion of code from my Product.java class then this error goes away. Why is this occuring?

    Product.java
    Java Code:
    public class Product {
       
        private String prodName;
        private int itemNumber;
        private double stock;
        private double price;
        private String prodType;
       
        public Product(String name, int number, double inStock, double itemPrice, String type)
        {
            prodName = name;
            itemNumber = number;
            stock = inStock;
            price = itemPrice;
            prodType = type;
        }
    
       // methods here
    
    }
    CD.java
    Java Code:
    public class CD extends Product {
       
        private String prodName;
        private int itemNumber;
        private double stock;
        private double price;
        private String genre;
        private String prodType;
       
        public CD(String name, int number, double inStock, double itemPrice, String itemGenre, String type)
        { // error is occurring on this line
            prodName = name;
            itemNumber = number;
            stock = inStock;
            price = itemPrice;
            genre = itemGenre;
            prodType = type;
        }
    
       // methods here
    
    }
    Last edited by RRasco; 11-24-2008 at 10:22 PM. Reason: Solved!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If CD inherits from Product, I think that you shouldn't give CD the same fields that Product already has. You should only give CD fields that it has that are not duplicated in Product.

    Your error is due to your CD constructor's not explicitly calling the product constructor -- super(....) on the first line of the CD constructor. If you don't do this, the compiler will try to call a default Product constructor, one with no parameters, and since this doesn't exist an error is produced.

    The solution: 1) get rid of the redundant fields in CD and only keep fields that are new to CD and not in product.
    2) The first line of your CD constructor should be a call to super(...) the Product's constructor. And make sure to pass all the necessary parameters to this constructor.
    Last edited by Fubarable; 11-24-2008 at 08:55 PM.

  3. #3
    RRasco is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Awesome! I was able to solve this based off of your suggestions. The subclass did in fact need the parameters, but I needed to use super() on the inherited ones.

    Thanks!

    Java Code:
    public class CD extends Product {
        
        private String prodName;
        private int itemNumber;
        private double stock;
        private double price;
        private String genre;
        private String prodType;
        
        public CD(String name, int number, double inStock, double itemPrice, String type, String itemGenre) 
        {    
            super(name, number, inStock, itemPrice, type);
            
            prodName = name;
            itemNumber = number;
            stock = inStock;
            price = itemPrice;
            prodType = type;
            genre = itemGenre;
        }
    
       // methods here
    }
    Last edited by RRasco; 11-24-2008 at 10:24 PM. Reason: Syntax correction

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The subclass did in fact need the parameters
    I'm still thinking that you don't need these parameters.

    Usually you're able to get a handle on these guys through the super class's public accessor and mutator methods.

    Please come back after this has been graded and let us know what your instructor thinks.
    Last edited by Fubarable; 11-25-2008 at 12:34 AM.

  5. #5
    RRasco is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    I agree that theoretically through inheritance, no I should not need them. However, when I remove them it throws an exception. I was able to remove the declarations and initializations, but I had to keep them supplied as attributes.

    Java Code:
    public class CD extends Product {
        
        // setup our class attributes
        private double inStock;
        private double itemPrice;
        
        public CD(String name, int number, double stock, double price, String type, String genre) 
        {    
            // call the super constructor
            super(name, number, stock, price, type, genre);
            
            // assign attributes
            inStock = stock;
            itemPrice = price;
        }
    
        // methods here
     
    }
    I would come back and share what my instructor says, but he is less than competent when it comes to providing feedback and answering questions, hence why I am here to begin with.

    Thanks anyways mate! This works and I'm sticking with it. However, if you would like to assist me further I am supposed to create a GUI to display the output as opposed to using System.out.println(). If you could recommend a GUI component I would appreciate it much!
    Last edited by RRasco; 11-25-2008 at 12:48 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I agree that theoretically through inheritance, no I should not need them. However, when I remove them it throws an exception. I was able to remove the declarations and initializations, but I had to keep them supplied as attributes.
    As a general rule, it's never good to fix a bug with a kludge but rather to find out why the bug occurs in the first place, but I'm sure that I'm not telling you anything that you don't already know.

    Regarding GUI's I'd recommend you search this and other forums for ideas and sample code. There's a lot of code out there, some of it even is good.

Similar Threads

  1. JTextPane (reading char and its attributes)
    By Defero in forum New To Java
    Replies: 0
    Last Post: 07-19-2008, 09:09 PM
  2. How to read AIX attributes & values.
    By John_28 in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 09:07 AM
  3. Replies: 2
    Last Post: 12-04-2007, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •