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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaidalin79
    New Member
    • Nov 2006
    • 59

    #1

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

    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!!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by zaidalin79
    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.........4 2!

    (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!)

    Comment

    • zaidalin79
      New Member
      • Nov 2006
      • 59

      #3
      Code:
      //Inventory.java
      //Class created to store item information for Inventory purposes
      //Created November 15, 2006
      //Modified November 29, 2006
      //Audrey A. Paige
      //IT315
      
      class Product
      {
         private String name; 	// class variable that stores the item name
         private int number;	  // class variable that stores the item number
         private long stockQuantity;   // class variable that stores the quantity in stock
         private double price;	  // class variable that stores the item price
         public Product() // Constructor for the Supplies class
         {
      	  name = "";
      	  number = 0;
      	  stockQuantity = 0L;
      	  price = 0.0;
         }
         public Product(String name, int number, long stockQuantity, double price) // Constructor for the Supplies class
      	  {
      		 this.name = name;
      		 this.number = number;
      		 this.stockQuantity = stockQuantity;
      		 this.price = price;
         }
         public void setItemName(String name)  // Method to set the item name
         {
      	  this.name = name;
         }
         public String getItemName()  // Method to get the item name
         {
      	  return name;
         }
         public void setItemNumber(int number)  // Method to set the item number
         {
      	  this.number = number;
         }
         public int getItemNumber()  // Method to get the item number
         {
      	  return number;
         }
         public void setStockQuantity(long quantity)  // Method to set the quantity in stock
         {
      	  stockQuantity = quantity;
         }
         public long getStockQuantity()  // Method to get the quantity in stock
         {
      	  return stockQuantity;
         }
         public void setItemPrice(double price)  // Method to set the item price
         {
      	  this.price = price;
         }
         public double getItemPrice()  // Method to get the item price
         {
      	  return price;
         }
         public double calculateInventoryValue()  // Method to calculate the value of the inventory
         {
      	  return price * stockQuantity;
         }
      }//end class Product
      
      public class Inventory
      {
         // main methods begins execution of java application
         public static void main( String args[])
         {
          Product p = new Product("Pencil", 5460, 125, 1.5);
      	System.out.printf("\n\nItem Name: %s\n",p.getItemName()); //display item name
      	System.out.printf("Item Number: %s\n",p.getItemNumber()); //display item number
      	System.out.printf("Quantity in Stock: %s\n",p.getStockQuantity()); //display quantity in stock
      	System.out.printf("Item Price: $%.2f\n",p.getItemPrice()); //display item price
      	System.out.printf("Value of Inventory: $%.2f\n",p.calculateInventoryValue()); //display total value of inventory for this item
      
      
         } // end main method
      }//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...

      Code:
      //Inventory.java
      //Class created to store item information for Inventory purposes
      //Created November 15, 2006
      //Modified November 29, 2006
      //Audrey A. Paige
      //IT315
      
      class Product
      {
         private String name; 	// class variable that stores the item name
         private int number;	  // class variable that stores the item number
         private long stockQuantity;   // class variable that stores the quantity in stock
         private double price;	  // class variable that stores the item price
         public Product() // Constructor for the Supplies class
         {
      	  name = "";
      	  number = 0;
      	  stockQuantity = 0L;
      	  price = 0.0;
         }
         public Product(String name, int number, long stockQuantity, double price) // Constructor for the Product class
      	  {
      		 this.name = name;
      		 this.number = number;
      		 this.stockQuantity = stockQuantity;
      		 this.price = price;
         }
         public void setItemName(String name)  // Method to set the item name
         {
      	  this.name = name;
         }
         public String getItemName()  // Method to get the item name
         {
      	  return name;
         }
         public void setItemNumber(int number)  // Method to set the item number
         {
      	  this.number = number;
         }
         public int getItemNumber()  // Method to get the item number
         {
      	  return number;
         }
         public void setStockQuantity(long quantity)  // Method to set the quantity in stock
         {
      	  stockQuantity = quantity;
         }
         public long getStockQuantity()  // Method to get the quantity in stock
         {
      	  return stockQuantity;
         }
         public void setItemPrice(double price)  // Method to set the item price
         {
      	  this.price = price;
         }
         public double getItemPrice()  // Method to get the item price
         {
      	  return price;
         }
         public double calculateInventoryValue()  // Method to calculate the value of the inventory
         {
      	  return price * stockQuantity;
         }
         public String toString()
         {
         	  return "Name :"+name + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue"+calculateInventoryValue();
         }
      }//end class Product
      
      public class Inventory2
      {
         // main methods begins execution of java application
         public static void main( String args[])
         {
      	String Products[] = new Products[3];
      	//instantiate Products array
      
      	for (i=0; i<3; ++i)
      		pencil = new Products ("Pencil", 5460, 125, 1.5);
      
      
          //Product p = new Product("Pencil", 5460, 125, 1.5);
      	//System.out.printf("\n\nItem Name: %s\n",products[pencil] + Product.getItemName()); //display item name
      	//System.out.printf("Item Number: %s\n",products.pencil.getItemNumber()); //display item number
      	//System.out.printf("Quantity in Stock: %s\n",products.pencil.getStockQuantity()); //display quantity in stock
      	//System.out.printf("Item Price: $%.2f\n",products.pencil.getItemPrice()); //display item price
      	//System.out.printf("Value of Inventory: $%.2f\n",products.pencil.calculateInventoryValue()); //display total value of inventory for this item
      
      
         } // end main method
      }//end class Inventory2

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        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.

        Comment

        • zaidalin79
          New Member
          • Nov 2006
          • 59

          #5
          Originally posted by sicarie
          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...

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by zaidalin79
            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.

            Comment

            Working...