By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Is anyone there that can help with a couple simple Java program exersizes?

P: 59
Please-if anyone is online that can help me with a couple beginner Java programs... I have spent all day looking at them and I just can't figure out what I need to do...

Please help!!
Nov 30 '06 #1
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Please-if anyone is online that can help me with a couple beginner Java programs... I have spent all day looking at them and I just can't figure out what I need to do...

Please help!!
Ummmm.........42!

(You didn't ask any questions - post your code, any error messages you are getting, and your questions - preferably one at a time - about your code; otherwise we can't help!)
Nov 30 '06 #2

P: 59
Expand|Select|Wrap|Line Numbers
  1. //Inventory.java
  2. //Class created to store item information for Inventory purposes
  3. //Created November 15, 2006
  4. //Modified November 29, 2006
  5. //Audrey A. Paige
  6. //IT315
  7.  
  8. class Product
  9. {
  10.    private String name;     // class variable that stores the item name
  11.    private int number;      // class variable that stores the item number
  12.    private long stockQuantity;   // class variable that stores the quantity in stock
  13.    private double price;      // class variable that stores the item price
  14.    public Product() // Constructor for the Supplies class
  15.    {
  16.       name = "";
  17.       number = 0;
  18.       stockQuantity = 0L;
  19.       price = 0.0;
  20.    }
  21.    public Product(String name, int number, long stockQuantity, double price) // Constructor for the Supplies class
  22.       {
  23.          this.name = name;
  24.          this.number = number;
  25.          this.stockQuantity = stockQuantity;
  26.          this.price = price;
  27.    }
  28.    public void setItemName(String name)  // Method to set the item name
  29.    {
  30.       this.name = name;
  31.    }
  32.    public String getItemName()  // Method to get the item name
  33.    {
  34.       return name;
  35.    }
  36.    public void setItemNumber(int number)  // Method to set the item number
  37.    {
  38.       this.number = number;
  39.    }
  40.    public int getItemNumber()  // Method to get the item number
  41.    {
  42.       return number;
  43.    }
  44.    public void setStockQuantity(long quantity)  // Method to set the quantity in stock
  45.    {
  46.       stockQuantity = quantity;
  47.    }
  48.    public long getStockQuantity()  // Method to get the quantity in stock
  49.    {
  50.       return stockQuantity;
  51.    }
  52.    public void setItemPrice(double price)  // Method to set the item price
  53.    {
  54.       this.price = price;
  55.    }
  56.    public double getItemPrice()  // Method to get the item price
  57.    {
  58.       return price;
  59.    }
  60.    public double calculateInventoryValue()  // Method to calculate the value of the inventory
  61.    {
  62.       return price * stockQuantity;
  63.    }
  64. }//end class Product
  65.  
  66. public class Inventory
  67. {
  68.    // main methods begins execution of java application
  69.    public static void main( String args[])
  70.    {
  71.     Product p = new Product("Pencil", 5460, 125, 1.5);
  72.     System.out.printf("\n\nItem Name: %s\n",p.getItemName()); //display item name
  73.     System.out.printf("Item Number: %s\n",p.getItemNumber()); //display item number
  74.     System.out.printf("Quantity in Stock: %s\n",p.getStockQuantity()); //display quantity in stock
  75.     System.out.printf("Item Price: $%.2f\n",p.getItemPrice()); //display item price
  76.     System.out.printf("Value of Inventory: $%.2f\n",p.calculateInventoryValue()); //display total value of inventory for this item
  77.  
  78.  
  79.    } // end main method
  80. }//end class Inventory
This is my Inventory program that works... I am trying to add an array and variables to the main method, and have the items display. Then I will need to sort them, but I can't even get the array to work...

Expand|Select|Wrap|Line Numbers
  1. //Inventory.java
  2. //Class created to store item information for Inventory purposes
  3. //Created November 15, 2006
  4. //Modified November 29, 2006
  5. //Audrey A. Paige
  6. //IT315
  7.  
  8. class Product
  9. {
  10.    private String name;     // class variable that stores the item name
  11.    private int number;      // class variable that stores the item number
  12.    private long stockQuantity;   // class variable that stores the quantity in stock
  13.    private double price;      // class variable that stores the item price
  14.    public Product() // Constructor for the Supplies class
  15.    {
  16.       name = "";
  17.       number = 0;
  18.       stockQuantity = 0L;
  19.       price = 0.0;
  20.    }
  21.    public Product(String name, int number, long stockQuantity, double price) // Constructor for the Product class
  22.       {
  23.          this.name = name;
  24.          this.number = number;
  25.          this.stockQuantity = stockQuantity;
  26.          this.price = price;
  27.    }
  28.    public void setItemName(String name)  // Method to set the item name
  29.    {
  30.       this.name = name;
  31.    }
  32.    public String getItemName()  // Method to get the item name
  33.    {
  34.       return name;
  35.    }
  36.    public void setItemNumber(int number)  // Method to set the item number
  37.    {
  38.       this.number = number;
  39.    }
  40.    public int getItemNumber()  // Method to get the item number
  41.    {
  42.       return number;
  43.    }
  44.    public void setStockQuantity(long quantity)  // Method to set the quantity in stock
  45.    {
  46.       stockQuantity = quantity;
  47.    }
  48.    public long getStockQuantity()  // Method to get the quantity in stock
  49.    {
  50.       return stockQuantity;
  51.    }
  52.    public void setItemPrice(double price)  // Method to set the item price
  53.    {
  54.       this.price = price;
  55.    }
  56.    public double getItemPrice()  // Method to get the item price
  57.    {
  58.       return price;
  59.    }
  60.    public double calculateInventoryValue()  // Method to calculate the value of the inventory
  61.    {
  62.       return price * stockQuantity;
  63.    }
  64.    public String toString()
  65.    {
  66.          return "Name :"+name + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue"+calculateInventoryValue();
  67.    }
  68. }//end class Product
  69.  
  70. public class Inventory2
  71. {
  72.    // main methods begins execution of java application
  73.    public static void main( String args[])
  74.    {
  75.     String Products[] = new Products[3];
  76.     //instantiate Products array
  77.  
  78.     for (i=0; i<3; ++i)
  79.         pencil = new Products ("Pencil", 5460, 125, 1.5);
  80.  
  81.  
  82.     //Product p = new Product("Pencil", 5460, 125, 1.5);
  83.     //System.out.printf("\n\nItem Name: %s\n",products[pencil] + Product.getItemName()); //display item name
  84.     //System.out.printf("Item Number: %s\n",products.pencil.getItemNumber()); //display item number
  85.     //System.out.printf("Quantity in Stock: %s\n",products.pencil.getStockQuantity()); //display quantity in stock
  86.     //System.out.printf("Item Price: $%.2f\n",products.pencil.getItemPrice()); //display item price
  87.     //System.out.printf("Value of Inventory: $%.2f\n",products.pencil.calculateInventoryValue()); //display total value of inventory for this item
  88.  
  89.  
  90.    } // end main method
  91. }//end class Inventory2
  92.  
  93.  
Nov 30 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Do you need to use an array? It looks like a Data Structure such as a Linked List would be more suited to your needs.

If you are set on arrays, here are some links that should help:

This will help correct your 'for' loop

And then this and this will help correct your array declaration / instantiation.
Nov 30 '06 #4

P: 59
Do you need to use an array? It looks like a Data Structure such as a Linked List would be more suited to your needs.

If you are set on arrays, here are some links that should help:

This will help correct your 'for' loop

And then this and this will help correct your array declaration / instantiation.
It is not that I am set, but that is the assignment...
Dec 1 '06 #5

10K+
P: 13,264
It is not that I am set, but that is the assignment...
Last time I checked I had give you program that works with arrays like you are requesting right now.

http://www.thescripts.com/forum/thread570714.html
Dec 1 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.