View Single Post
Old 05-03-2007, 04:28 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
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