NationStates Jolt Archive


I have Java Question.

Jenrak
24-01-2006, 00:22
What if I was to get a certain method from another class? If say, I had to import a value, such as the length of a pencil, from one class into another, then how would I be able to do that?

It always says the variable (which is named getLength - length being the length of the pencil) cannot be found, because the length is in the other class, so any help would be appreciated.
Damor
24-01-2006, 00:34
You're trying to access from one class, a private datamember from another class? Didn't they teach you you can't touch anothers' private parts unless you're friends :p (Actually, declaring the classes friend is probably more a C++ solution)
You could declare the variable public, or make a public function to access it (Which is better since then you can protect it against being changed from outside the class).

Of course, I'm more a C++ person than a java guru, so I might be mistaken.

A hint, the actual code, if it's not too large, might help people find the problem faster. Because otherwise we'll have to guess all the things that might be a problem.
Mondoth
24-01-2006, 00:45
you need to declare it public whatever else you do, if you still get the problem after creatig an accessor to the variable then post the code so I can sink my teeth into it
Jimusopolis
24-01-2006, 00:49
Accessor Functions are your friend :)
Dergamoor
24-01-2006, 00:50
Could someone teach me to be an evil chatroom hacker? I dislike people...
Jenrak
24-01-2006, 00:53
Now, whenever I use the 'length' function, it always says that the variable cannot be found, when the variable is in another class.
Fair Progress
24-01-2006, 01:26
public int getLengthOfPencils()
{
int numberOfPencils = this.pencils.size(), totalLength=0;
Pencil p;

//get each pencil in the list and add it's lengh to the totalLength
for(int i=0; i<numberOfPencils; i++){
p = (Pencil)this.pencils.get(i);
totalLength += p.getLength();
}

return totalLength;
}


I didn't compile this, but it should do what you need. You should focus on understanding it, so you know how to solve a similar problem next time.
Jenrak
24-01-2006, 02:31
Holy crap, you're a F***ing computer.
It works, but as you say, to understand it, I need to know this a bit more...


Okay, to dissect your coding (to make sure that I understand this)...
I don't understand on how the first line works.

Okay, the second line uses a 'For' loop, and since 'i' is zero, as long as the number of 'i' is less than the number of pencils, as 'i' starts at zero, it will keep increasing by one every time it goes through.

What does the two lines after that do exactly? I know you go after the 'i' number in the pencil, which is the 'i'th pencil, but what is the line under that do? Does it add on the pencil that it just received into the totalLength?

I understand after the 'i' reaches the 'equal' status to the pencils, then it goes right to the end.

Thank you for your help, I just now need to find out what these first two do. I'm inclined to think you're my teacher, so I'm being very wary right now...
Fair Progress
24-01-2006, 22:25
I know you go after the 'i' number in the pencil, which is the 'i'th pencil, but what is the line under that do? Does it add on the pencil that it just received into the totalLength?


int numberOfPencils = this.pencils.size(), totalLength=0;
I'm declaring and initializing two int variables:
- numberOfPencils is initialized with the number of elements in the "pencils" ArrayList (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html); the number of elements is an int obtained using the size() (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html#size()) method of the ArrayList object.
- totalLength, which will hold the sum of the individual lengths that I'll obtain later (so it's currently zero)
Then I declare p, a Pencil variable (it's an object reference, to be strict) to hold each Pencil object I retrieve from pencils


You have an ArrayList that contains Objects. Those Objects are of type Pencil. As you already understood, I'm going through each element of the ArrayList, so:

p = (Pencil)this.pencils.get(i);
This line is responsible for getting the Pencil object on the ith position of the ArrayList. ArrayLists are indexed to zero (the first element is in position zero), that's why i starts out with the value 0.
I use the get() (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html#get(int)) method of the pencils ArrayList to retrieve the object at the specified position and store it in the p object reference that I declared previously. The (Pencil) is a typecast (http://www.janeg.ca/scjp/oper/cast.html) and it's needed because the get() method returns an Object (see the Java API) and Pencil is a more specific type of Object, so I have to narrow it down with the cast, otherwise I can't use the p reference to hold the object returned.


totalLength += p.getLength();
+= is an arithmetic operator that just saves space, you can do without it: a += b is exactly the same as a = a+b.
The getLength() method from the Pencil class returns an int with the length of the Pencil object that I stored in p. So in this line I'm simply using that method to retrieve the length and adding it to the total length sum (stored in totalLength ). It would be the same if I'd done it like this: totalLength = totalLength+p.getLength()
Kzord
24-01-2006, 22:29
What if I was to get a certain method from another class? If say, I had to import a value, such as the length of a pencil, from one class into another, then how would I be able to do that?

It always says the variable (which is named getLength - length being the length of the pencil) cannot be found, because the length is in the other class, so any help would be appreciated.

If you give your code, we can find the errors in it. Otherwise, I can only guess what you mean.
Jenrak
25-01-2006, 02:19
snip

Okay, time for me to understand this...
Fair Progress
25-01-2006, 02:23
Okay, time for me to understand this...
Read the documents linked on the post; you should work on getting confortable with the Java2 API (http://java.sun.com/j2se/1.4.2/docs/api/), it documents (almost) everything provided by Java.
Jenrak
25-01-2006, 02:31
Read the documents linked on the post; you should work on getting confortable with the Java2 API (http://java.sun.com/j2se/1.4.2/docs/api/), it documents (almost) everything provided by Java.

Okay. Thanks for the help. I'll get to reading it.