Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-02-2007, 07:23 PM   #1 (permalink)
Negative6
Recruit
 
Join Date: May 2007
Posts: 1
Negative6 is on a distinguished road
Unhappy Please help me pass my class

I am really having trouble in my java class. This is my last class i have to pass before getting my degree in information tech. I truly need some help with my assignments so i can at least pass with a C. After starting the degree i quickly realized information tech was not where i thought i wanted to be, and really have a great admiration for the people who can do it. I would much rather be in buisness management.

Here is my assignment:

Week 5

CheckPoint: Inventory Program Part 1

• Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).

• Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.

• Create a Java application that displays the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock multiplied by the price of each unit). Pay attention to the good programming practices in the text to ensure your source code is readable and well documented.

If you don’t know how to do the Payroll part 3, they have a difficult time with this inventory part 1.

For this inventory part 1, you need to create 2 classes: inventory and inventoryTest (you can give any names you want).

The inventory class contains the item number, the name of the product, the number of units in stock, and the price of each unit. Create the set and get methods. You can also create a method to calculate the value of the inventory.

The inventoryTest instantiates the inventory class to do the display.

So there are 2 classes: inventory that contains the information about the product, and the inventoryTest class that initializes the product class and does the display with its main program.

Please see example below.




For this inventory part 1, you need to create 2 classes: inventory and inventoryTest (you can give any names you want).

The inventory class contains the item number, the name of the product, the number of units in stock, and the price of each unit. Create the set and get methods. You can also create a method to calculate the value of the inventory.

The inventoryTest instantiates the inventory class to do the display.

So there are 2 classes: inventory that contains the information about the product, and the inventoryTest class that initializes the product class and does the display with its main program..


Here is my first attempt at the program:
inventory.java
Code:
public class inventory
{
  private String dvdName;
  public void setDvdName (String name)
  {
    dvdName = name;
  }

  public String getDvdName()
  {
    return dvdName;
  }

  private String itemNumber;

  public void setItemNumber (String number)
  {
    itemNumber = number;
  }

  public String getItemNumber()
  {
    return itemNumber;
  }

  private String stockNumber;

  public void setStockNumber (String stock)
  {
    stockNumber = stock;
  }

  public String getStockNumber()
  {
    return stockNumber;
  }

  private String price;

  public void setPrice (String price)
  {
    price = price;
  }

  public String getPrice()
  {
    return price;
  }

  private String inventoryValue;

  public void setInventoryValue (String value)
  {
    inventoryValue = value;
  }

  public String getInventoryValue()
  {
    return inventoryValue;
  }

  public void displayMessage()
  {
    System.out.printf( "DVD Title: %s\n",
                       getDvdName() );

    System.out.printf( "Item #: %s\n",
                       getItemNumber() );

    System.out.printf( "Number in stock: %s\n",
                       getStockNumber() );

    System.out.printf( "Price: $%d\n",
                       getPrice() );

    System.out.printf( "Inventory Value: $%d\n",
                       getInventoryValue() );
  }
}
inventorytest.java
Code:
import java.util.Scanner;

public class inventorytest
{
  public static void main( String args [] )
  {
    Scanner input = new Scanner( System.in);
    inventory myInventory = new inventory("Spiderman","001",11, 19.99);

    System.out.println( "Inventory of DVD Movies: " );
    System.out.println();

    System.out.println();

    myInventory.displayMessage();
  }

}
Here is my second attempt

inventory.java
Code:
public class inventory
{
  public inventory(String name, 
                   String number, 
                   Int quantity, 
                   Double cost)
  {
    this.dvdName = name;
    this.itemNumber = number;
    this.stockNumber = quantity;
    this.price = cost;
  }
  public String getdvdName;
  {
    return name;
  }

  public void displayMessage()
  {
    System.out.printf( "DVD Title: %s\n",vdName);
    System.out.printf( "Item #: %s\n",itemNumber);
    System.out.printf( "Number in stock: %s\n",
                       stockNumber);
    System.out.printf( "Price: $%d\n",
                       price);
    System.out.printf( "Inventory Value: $%d\n");
   }
}
I really just dont understand it all, please help me pass this class below is my next assignment

Week 6

CheckPoint: Inventory Program Part 2

• Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

• Create a method to calculate the value of the entire inventory.

• Create another method to sort the array items by the name of the product.

For this part 2, the new 2 methods are created in the inventory class. They must have a parameter which is an array of the inventory class. In the inventoryTest class, create an array of inventory class. Populate the array. Display the non sorted array. Sort the array by calling the method created in the inventory class. Display the sorted array.

Please see example below.







CheckPoint: Inventory Program Part 3

• Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

• Modify the output to display this additional feature you have chosen and the restocking fee.

For this part 3, the inventory class remains the same. So do not modify the inventory class. Create a new class for a specific product which is a subclass of the inventory class, e.g. a DVD class that has a new feature (e.g. rating). This is to show inheritance and polymorphism. In the inventoryTest class, the array is now the array of the subclass (e.g. DVD subclass). Display non sorted and sorted arrays.

Please see example below.

Last edited by Belisarius; 05-03-2007 at 03:54 PM. Reason: Added code tags, beautified
Negative6 is offline   Reply With Quote
Old 05-03-2007, 04:28 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Unfortunantly, it looks like you've been taught by someone who'd rather be teaching C - the styling and the liberal uses of "printf" are the tell-tale signs. It's no wonder you're confused.

It looks like you're basically trying to learn a semester's worth of programming in a week. Best course of action is to simply sit down in front of the compiler and work through all the errors that come up as you try to get your program to work. There won't be a magic solution - it's just going to be time consuming and hard work.

I'll help you out by walking you through the design of an Inventory object. I'm going to start from scratch so you can understand what's going on.

First off, we have this concept of a DVD that we want to represent. Let's create the basic Object:

Code:
public class DVD{
  public DVD(){

  }
}
Now, this DVD will have properties. What do we want to know? Well, we will have a number of different DVDs in our stock. We'll have a SpiderMan DVD, a BlackAdder DVD, a Bridges of Madison County DVD, etc. So we'll need to know the name of each type of DVD. That's best represented as a String in Java, so we'll add a variable with "class" scope - that is the whole class, every method in it, can see this variable.

Code:
public class DVD{
  String name;
  public DVD(String name){
    this.name = name;
  }
}
Why did I add "String name" to the public method DVD? Because the method DVD is a constructor, and handing it that method will be convienent later down the line.

What we have done by writing this class is create the framework for representing various bits of data. We will need, however, to fill in different data for each different type, or instance, of DVDs - one for Shrek and one for Star Wars. We do that by calling the constructor, and it'd be convienent to pass all the needed data right then, in one line.

Now, we need to keep track of other bits of data - the item number, the stock quantity, and the price. So we add them in a similar fashion, using the appropriate types, or data formats (String for name, int for item number and stock quantity, and a double(because we need to use decimal places) for price).

Code:
public class DVD{
  String name;
  int itemNumber;
  int stockQuantity;
  double price;
  public DVD(String name, int itemNumber, int stockQuantity, double price){
    this.name = name;
    this.itemNumber = itemNumber;
    this.stockQuantity = stockQuantity;
    this.price = price;
  }
}
Now, we want to be able to display the state of any given instance. In Java, there is a specific method set aside for representing an Object (which is what our DVD class is) as a String. It's called the toString method. So let's write it.

Code:
public class DVD{
  String name;
  int itemNumber;
  int stockQuantity;
  double price;
  public DVD(String name, int itemNumber, int stockQuantity, double price){
    this.name = name;
    this.itemNumber = itemNumber;
    this.stockQuantity = stockQuantity;
    this.price = price;
  }

  public String toString(){
    return "DVD Title: " + this.name + "\n" +
             "Item Number: " + this.itemNumber + "\n" +
             "Stock Quantity: " + this.stockQuantity + "\n" +
             "Price: " + this.price + "\n";
  }
}
Now, lastly, your assignment says you need to create "get" and "set", or accessor, methods. This is good programming fundamentals, but won't sink in until later, in more advanced programming course. For now, it's a bit time consuming, but straight-forward enough:
Code:
public class DVD{
  private String name;
  private int itemNumber;
  private int stockQuantity;
  private double price;
  public DVD(String name, int itemNumber, int stockQuantity, double price){
    this.name = name;
    this.itemNumber = itemNumber;
    this.stockQuantity = stockQuantity;
    this.price = price;
  }

  public String toString(){
    return "DVD Title: " + this.name + "\n" +
             "Item Number: " + this.itemNumber + "\n" +
             "Stock Quantity: " + this.stockQuantity + "\n" +
             "Price: " + this.price + "\n";
  }

  public String getName(){
    return this.name;
  }

  public void setName(String name){
    this.name = name;
  }

  public int getItemNumber(){
    return this.itemNumber;
  }

  public void setItemNumber(int itemNumber){
    this.itemNumber = itemNumber;
  }

  public int getStockQuantity(){
    return this.stockQuantity;
  }

  public void setStockQuantity(int stockQuantity){
    this.stockQuantity = stockQuantity;
  }

  public double getPrice(){
    return this.price;
  }

  public void setPrice(double price){
    this.price = price;
  }
}
Why did I make all the class variables "private"? So neither I nor any future programmers using my code will be tempted to bypass the accessor methods and work with the variables directly.

Furthermore, you're probably wondering what the this thing is. Remember, we're creating a framework that we intend to be used to represent any number of different DVDs, as instances. If I want to update the item number for the Ben Hur DVD, I want to call the setItemNumber method on only that instance of DVD - I don't want to set *all* DVD item numbers to that number. Therefore, when I say "this", I'm talking about *this* particular instance, not *every* instance. This is a bit of an advanced concept in programming, and I wouldn't be suprised if you can't get your mind around it right now. For now, just accept the fact that it makes eveything work.

I haven't tried to compile that, so there might be a syntax error or two, but I'm sure you can fix anything that crops up. And that can quite readily form the basis for all your assignments. If you have any more questions, feel free to drop by.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[C#] Casting from derived class to base class Melon00 MS Technologies ( ASP, VB, C#, .NET ) 0 06-06-2006 08:34 AM
Help with C++ & Win32 in a class DIY_Gamer Platform/API C++ 1 07-29-2005 07:45 AM
Class using a specific instance of another class? is this possible? abs Standard C, C++ 5 02-08-2005 03:12 PM
to put data methods inside class or not? sde Java 2 05-25-2004 04:09 PM


All times are GMT -8. The time now is 11:52 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting