|  |
03-18-2008, 02:12 PM
|
#1 (permalink)
|
Join Date: Jan 2005 Location: The South Posts: 19,907
| Re: Apok's general Java thread The problem with "reading the book" is that, for me, the book doesn't make sense. I go looking for tutorials, and they say the same thing as the book, so they don't make sense to me, either.
I have found some answers on a coding forum, but even they tend to talk in code-babble. |
| |
03-18-2008, 02:31 PM
|
#2 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: Apok's general Java thread First, you need a "good" book, and it's difficult to know if you have one when you're first learning about a new subject.
Second, in the beginning you will have to just accept some initial concepts because they can't be explained until you have learned other concepts. And those "other" concepts can't be learned until you have at least a small understanding of the initial concepts. Essentially, learning how to program is most difficult when you're just starting out. Don't worry if you don't understand everything as soon as it is presented, just keep practicing and reading. You'll see that learning how to code is an iterative process, and with each iteration, you'll pick up things you missed before. |
| |
03-18-2008, 02:41 PM
|
#3 (permalink)
|
Join Date: Jan 2005 Location: The South Posts: 19,907
| Re: Problems with basic Java concepts My current problem lies with actually using a class. I can build a class, but as far as using it... I have no idea. I can see it, but it makes no sense to me.
My last assignment was simply to make a class for inventory and an application that would display it. The class was no big deal, but the app messed me up. My teacher has been out of contact, my book's illustrations are completely different than what I need to see, and all the tutorials I have found parot the same thing as my book. If I understood the book I wouldn't have a problem. |
| |
03-18-2008, 03:36 PM
|
#4 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: Problems with basic Java concepts A class is just a grouping of data and methods, which you seem to understand. Using it is even easier. I'm sure you've used classes in your own programs. If you've used a string, you've used a class.
Let's say you've created your own Inventory class. I have no idea what your Inventory class is supposed to include, but for this discussion, my Inventory class is shown below. Code: public class Inventory
{
private ArrayList items = new ArrayList();
public void addItem(object item)
{
items.Add(item);
}
public object getItem(int index)
{
return items.get(index);
}
public int getNumberOfItems()
{
return items.size();
}
}
Now let's use the inventory class. Code: public class MyApp
{
public static void main(String[] args)
{
Inventory myInventory = new Inventory();
myInventory.addItem("some item");
myInventory.addItem("another item");
int numberOfItems = myInventory.getNumberOfItems();
for (int i=0; i<numberOfItems; i++)
{
object item = myInventory.getItem(i);
System.out.println((String)item);
}
}
}
|
| |
03-18-2008, 08:49 PM
|
#5 (permalink)
|
Join Date: Jan 2005 Location: The South Posts: 19,907
| Re: Problems with basic Java concepts You completely lost me. "ArrayList"? Never heard of it... but, then, I haven't finished the chapters for this week.
The Inventory I set up has four parts: item number, item name, item quantity, and item cost. I set it up as Code: public InventoryA(int _itemNum, String _name, int _units, double _price
Here's the class: Code: // InventoryA program
public class InventoryA // begin InventoryA class
{
private static int itemNum[] = new int[3];// number of the item
private static String name[] = new String[3]; // name of the item
private static int units[] = new int[3]; // quanity of item in stock
private static double price[] = new double[3]; // price of item
private static int i = 0;
public InventoryA(int _itemNum, String _name, int _units, double _price) // constructor
{
itemNum[i] = _itemNum;
name[i] = _name;
units[i] = _units;
price[i] = _price;
i = i + 1;
}
public static double valueOfInventory(double p,int u)
{
return p*u;
}
public static void showInventory()
{
for(int j=0;j<i;j++)
{
System.out.println("Product Name : " + name[j]); // print name
System.out.println("Item Number : " + itemNum[j]); // print number
System.out.println("Number of Units: " + units[j]); // print units
System.out.println("Unit Price : " + price[j]); // print price
System.out.println("The value of the inventory of " + name[j] + " is = " + valueOfInventory(price[j], units[j]));//call the valueOfInventory() method and display the value
}
}
} // end class
And here is my program. I used help from others and some "creative acquisitioning" to cobble it together: Code: public class InventoryMain
{
public static void main( String[] args)
{
InventoryA p = new InventoryA(1, "pencils", 12, 0.5);
InventoryA q = new InventoryA(2, "Notebooks", 15, 0.5);
InventoryA r = new InventoryA(3, "pens", 20, 0.6);
//show inventory
InventoryA.showInventory();
}
}
I'm not real clear on how I got the second one to work, but it did. I am completely lost with this, though. |
| |
03-25-2008, 10:29 PM
|
#6 (permalink)
|
True Techie Join Date: Dec 2007 Posts: 113
| Re: Problems with basic Java concepts Trotter,
I've used Java before, but I when I program (which rarely happens anymore), I use C/C++. However, OOP in C++ is very similar to Java, so I'll be going off of that. The explanation below is might be confusing to you (it took me a while to understand classes/objects etc...), so I suggest that you sit down for 20 minutes or so and carefully/slowly read each word and try to understand it. You've already shown great initiative by searching elsewhere, other than your book. It's something I didn't do
Anyways,
Just to clarify - a class is something that stores information using variables and operate on them using functions.
Now, what you did in your main program was that you created objects of your classes. For example... takes us humans as an example. The class would be the body - right? What should be inside the body (aka class)? Well... hearts, eyes, brain, legs, arms, shoulders, muscles, etc... Think of these are variables. What functions do we need to perform on ourselves? Well, we need the regulation of our immune system, respiratory system, etc. Think of these human systems as functions of the class (or functions of the human body). Hopefully this should clear up any confusion you might have. This example really helped me when learning.
------------------------------------------------------------
Let's have a look at your code: InventoryA q = new InventoryA(2, "Notebooks", 15, 0.5);
What you're doing here is creating a new object. This new object is q. This is just like creating a new person with the name q... think of someone giving birth and bringing in a new child into the world... you're doing the same thing, sort of
In your first class (not your main program), you defined a function InventoryA that accepts variables into it. In your main program, you're sending the values of:
2
Notebooks
15
0.5 into (respectively)
int _itemNum
String _name
int _units
double _price
when you create it then instantiate it (this is kind of like the official term). So, you create the object q and give it the paramaters of 2, Notebooks, etc. Your new object, q, has the information you gave it above. Even though you can't do this in reality, think of this as giving your child attributes when he or she is first born... the color hair you want the child to have, the eye color, weight, height, etc.
This information is now stored in the variables above. Now, once the variables of int _itemNum, String _name, int _units, double _price are given values, it will assign them INTO the object's own internal/personal variables, which you have labeled as itemNum[i], name[i], units[i], price[i].
In your class InventoryA, the function InventoryA is defined as a constructor. It constructs the object with the specified information. It is good practice to include some sort of parameters, even if you don't have any. For example, in C++, you may have a class and then create an object of the class with no initial parameters... you still should have a constructor in your class.
-undefined
Last edited by undefined; 03-25-2008 at 10:34 PM.
|
| |
03-26-2008, 07:52 AM
|
#7 (permalink)
|
Join Date: Jan 2005 Location: The South Posts: 19,907
| Re: Problems with basic Java concepts Thanks. I get that part pretty much. Classes themselves are not a problem any more. Using the blasted things, though...
I get tripped up trying to keep it straight in my head. JAVA won't compile everything together in one big lump of code... if it would, I would have a much easier time of it. Breaking it up into parts makes little sense to me, and trying to use the various parts makes even less.
I am used to good old BASIC where a program is a program and not bits and pieces. Even my preious class on algorithms put all the psuedocode together as a whole. Now, they turn right around and want to do everything backwards... Go figure. |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |