Results 1 to 17 of 17
  1. #1
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default Help with program

    I am trying to write a program that uses a four argument constructor. It is an inventory program that contains item number, item name, unit price, and units in stock. I created a class Product with get and set methods for each. There is also a method to calculate the extended price. The Inventory class must pass three sets of data through the constructor and display the results for each product on one line. Appropriate column headings must also be displayed. I am having trouble with the inventory class displaying the variables and column headings. I cannot use arrays. Can anyone help me?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Column heading should display only one time, right? So before doing all other data displaying print the headings. You can easily do that, because I think you have to use a loop to find all inventory results, and so before entering the loop you can do it.

    Without looking what you have done, it's difficult to suggest a best approach at all.

  3. #3
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    I can't use a loop to do it. I will copy and paste what I have:

    public class Product
    {
    private int itemNumber; // declare variable for item number
    private string productName; // declare variable for product name
    private int stockUnits; // declare variable for number of units in stock
    private double unitPrice; // declare variable for unit price of each product

    // Four argument constructor
    public Product( int item, String name, int units, double price )
    {
    itemNumber = item;
    productName = name;
    stockUnits = units;
    unitPrice = price;
    } // End four argument constructor

    public void setItemNumber( int item ) // Set item number
    {
    itemNumber = item;
    } // end set item number

    public int getItemNumber() // return item number
    {
    return itemNumber;
    } // end return item number

    public void setProductName( String name ) // set product name
    {
    productName = name;
    } // end set product name

    public String getProductName() // return product name
    {
    return productName;
    } // end return product name

    public void setStockUnits( int units ) // set units in stock
    {
    stockUnits = units;
    } // end set units in stock

    public int getStockUnits() // return units in stock
    {
    return stockUnits;
    } // end return units in stock

    public void setUnitPrice( double price ) // set unit price
    {
    unitPrice = price;
    } // end set unit price

    public double getUnitPrice() // return unit price
    {
    return unitPrice;
    } // end return unit price

    public double getExtendedPrice() // calculate extended price
    {
    return stockUnits * unitPrice;
    } // end method to calculate extended price

    } // end class product


    public class InventoryPart1
    {
    public static void main( String args[] )
    {
    // print column headings
    system.out.println( "Item # Item Name Units in Stock Unit Price Extended Price" );

    // instantiate first item
    Product dreamCatcher = new Product( "001", "Dream Catcher", "25", "7.99" );

    system.out.printf( dreamCatcher.getItemNumber(), dreamCatcher.getProductName(),
    dreamCatcher.getStockUnits(), dreamCatcher.getUnitPrice(), dreamCatcher.getExtendedPrice() );
    }
    }

    I just kind of guessed at the Inventory class. I am not sure how to return the results and make the table look good. Thanks for the help!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you have copy-paste this code, it should never compile. I found an error at the very beginning of the code when I'm reading it.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Firs fix those errors in two classes. You problem is about data types. In the constructor and the object instantiation, you have data type conflict.

  6. #6
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    I'm not sure I undertand what you mean. The data types are specified as what I need them to be. Can you explain further?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Check that four argument constructor you have define in the code. There data types are int, String, int and double respectively.

    Then find the Product class instantiation in the inventory class. You have declared all the argument types as String. So it wrong. Either you have to declare another constructor which can invoke with four String arguments or, change the instantiation according to your application requirement.

  8. #8
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    Okay, I fixed the variables and changed the inventory class to set the values in the construcor. The program compiles but won't display anything. Here is the new code:

    public class Product
    {
    private int itemNumber; // declare variable for item number
    private String productName; // declare variable for product name
    private int stockUnits; // declare variable for number of units in stock
    private double unitPrice; // declare variable for unit price of each product

    // Four argument constructor
    public Product( int item, String name, int units, double price )
    {
    itemNumber = 0;
    productName = name;
    stockUnits = 0;
    unitPrice = 0;
    } // End four argument constructor

    public void setItemNumber( int item ) // Set item number
    {
    itemNumber = item;
    } // end set item number

    public int getItemNumber() // return item number
    {
    return itemNumber;
    } // end return item number

    public void setProductName( String name ) // set product name
    {
    productName = name;
    } // end set product name

    public String getProductName() // return product name
    {
    return productName;
    } // end return product name

    public void setStockUnits( int units ) // set units in stock
    {
    stockUnits = units;
    } // end set units in stock

    public int getStockUnits() // return units in stock
    {
    return stockUnits;
    } // end return units in stock

    public void setUnitPrice( double price ) // set unit price
    {
    unitPrice = price;
    } // end set unit price

    public double getUnitPrice() // return unit price
    {
    return unitPrice;
    } // end return unit price

    public double getTotalValue() // calculate total value
    {
    return stockUnits * unitPrice;
    } // end method to calculate total value

    } // end class product

    public class InventoryPart1
    {
    public int item;
    public String name;
    public int units;
    public double price;

    public static void main( String args[] )
    {
    Product product1 = new Product( 001, DreamCatcher, 25, 7.99 );

    product1.setItemNumber( 001 );
    product1.setItemName( DreamCatcher );
    product1.setStockUnits( 25 );
    product1.setUnitPrice( 7.99 );

    system.out.println( "", product1.getItemNuber(), product1.getItemName(), product1.getStockUnits(),
    product1.getUnitPrice(), product1.getTotalValue() );
    }
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    If the program compiles with no errors, what shows on the console when you execute it? Add some more println() statements to show what is happening in the various methods.

    Does the program shown above compile without errors?
    system.out.println( "", product1.getItemNuber(), product1.getItemName(), product1.getStockUnits(),
    product1.getUnitPrice(), product1.getTotalValue() );
    The above should give you errors!!!

  10. #10
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    The program compiled fine with what I had but when I ran it it didn't display anything. I changed the print statement to printf with format specifiers and it would not compile. I keep getting weird errors like "system package does not exist." I am sooooo confused!

  11. #11
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    For some reason, when I try to print just the headings using println the compiler says that the system package does not exist. I thought I had a fairly decent grasp on this language, but I have become very confused. Can anyone help?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    system.out.println
    This would give a compile error because of the lowercase s in system. Which makes me think that the code you posted is not what you are exectuing.
    Try System.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes Norm, as I said pointed very beginning on one of my replies, our thread starter found lots of case issues. Those are the basic things he should know, isn't it? Amazing thing is he doesn't found such compile errors. ;)

  14. #14
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    I have been playing with this code for two days and can't get it to what I want. I have made changes and got it to compile, but when I run it now it tells me I have a no such method error. Here is what I have now.

    public class Product
    {
    private int itemNumber; // declare variable for item number
    private String productName; // declare variable for product name
    private int stockUnits; // declare variable for number of units in stock
    private double unitPrice; // declare variable for unit price of each product
    public double totalValue;

    // Four argument constructor
    public Product( int item, String name, int units, double price )
    {
    itemNumber = 0;
    productName = name;
    stockUnits = 0;
    unitPrice = 0;
    } // End four argument constructor

    public void setItemNumber( int item ) // Set item number
    {
    itemNumber = item;
    } // end set item number

    public int getItemNumber() // return item number
    {
    return itemNumber;
    } // end return item number

    public void setProductName( String name ) // set product name
    {
    productName = name;
    } // end set product name

    public String getProductName() // return product name
    {
    return productName;
    } // end return product name

    public void setStockUnits( int units ) // set units in stock
    {
    stockUnits = units;
    } // end set units in stock

    public int getStockUnits() // return units in stock
    {
    return stockUnits;
    } // end return units in stock

    public void setUnitPrice( double price ) // set unit price
    {
    unitPrice = price;
    } // end set unit price

    public double getUnitPrice() // return unit price
    {
    return unitPrice;
    } // end return unit price

    public double totalValue() // calculate total value
    {
    totalValue = stockUnits * unitPrice;
    return totalValue;
    } // end method to calculate total value

    public void displayInventory()
    {
    System.out.println( "Item # Name Stock Unit Price Total Value" );
    System.out.printf( "%d", itemNumber, "%S", productName, "%d", stockUnits);
    System.out.printf( "%.2f", unitPrice, "%.2f", totalValue );
    }

    } // end class product

    public class InventoryPart1
    {
    public int item;
    public String name;
    public int units;
    public double price;

    public void main( String args[] )
    {
    Product product1 = new Product( item, name, units, price );

    item = 001;
    name = "Dream Catcher";
    units = 25;
    price = 7.99;

    product1.setItemNumber( item );
    product1.setProductName( name );
    product1.setStockUnits( units );
    product1.setUnitPrice( price );
    }
    }

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  16. #16
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    There is a run-time error, but no compile-time erors. I realize I should know some things and have done well to this point, but I am basiclly teaching myself how to do this. I ordered a book, but it has not arrived yet. I really, realy appreciate everyone's help. I am not looking for anyone to give me answers, just point me in the right direction. Again, thanks!

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Cool, I give the first point. In any class main method is the stating point. If any of your class doesn't have a main method, that me you can't run that class, because you don't have a starting point to directed to the Java Runtime Environment.

    Since to run a class each one of it have a main method, which should be a static one. So that JVM initialize only one main method for a class, until it reaches to the end.

    So your main method should like this.

    Java Code:
    public static void main(String[] args) {
        // Your implementation goes here
    }

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  3. cannot run the program
    By amiey in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 05:13 AM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  5. Why does this program not end?
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:22 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
  •