
11-11-2008, 11:34 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
[SOLVED] Please help with java
Last edited by SKYLINEGTR; 11-21-2008 at 02:45 PM.
|
|

11-11-2008, 11:43 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,431
Rep Power: 8
|
|
|
You need to look up and use enums. Check the Sun tutorials for info on this.
|
|

11-11-2008, 11:46 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
|
enums?? never even heard of these but ok ill take a look
thx
|
|

11-11-2008, 11:52 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
There is a nice tutorial on Suns tutorial about enum in Java.
Java enum
|
|

11-12-2008, 11:35 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
|
ok iv been reading some of them tutorials but im still stuck
I need to create an integer field to hold the directions and with enums where can i state its an int? And where do i stick the constants 1,2,3,4?? Well apparently i 'can' use these...but i guess i dont need to if i dont?
It cant just be this can it
private enum Direction { UP, DOWN, LEFT, RIGHT }
|
|

11-12-2008, 01:52 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,431
Rep Power: 8
|
|
|
Enums can hold and return ints or Strings or anything else just like a regular class. Look at the tutorial and it will show you how to do this.
|
|

11-12-2008, 05:09 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
|
Yes enums can holds and return any primitive data type. Seems to me you don't have clearly read about enums.
|
|

11-12-2008, 08:01 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
indeed actually its only my 2nd month leanring java and iv never heard or read about enums before
If I write a possible solution can someone atleast say if its right or wrong please..im going to work on it now..
|
|

11-13-2008, 04:52 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
Now you have create an enum Direction, and it's hold all directions you want.
Define an object of Direction and in simple way, in a switch-case block refer the direction you want.
|
Code:
|
public class DirectionUse {
Direction direction;
public DirectionUse(Direction direction) {
this.direction = direction;
}
public void EnumUsingHere() {
switch(direction) {
case UP:
System.out.println("Up");
break;
// all other cases as you need
}
}
public static void main(String[] args) {
DirectionUse use = new DirectionUse(Direction.UP);
use.EnumUsingHere()
}
} |
|
|

11-13-2008, 11:48 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 1
Rep Power: 0
|
|
|
hello hai i want help to develop java coding
|
|

11-13-2008, 07:32 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
Thx for your help guys, i managed to complete that class now but i just need a question answering on the next part now
OK, say. I had a class called Cars and now another called Person.
Im going to create a field which contains an array of car objects, but initially it wont contain any cars
So for this do i just have something like the following field and declare later in the constructor about the CAR Objects?:
private double[] cars;
Thanks
|
|

11-13-2008, 08:41 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
|
actually forget that...that isnt the problem tbh
iv declared the following field (array)
private double[] cars;
now in the constructor im trying to initialise the array so it can hold 6 objects from the 'Cars' class itself....im not sure what else to enter in the code..this is what i have...
Cars = new double[6];
but this doesnt refer to the cars class so how do i do that?? :S
|
|

11-14-2008, 05:26 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
An array is a collection of same data type.
|
Code:
|
private double[] cars; |
This is an array of double values, data type is double. If you want to hold an object, array must holds relevant object types. You have to do something like this.
|
Code:
|
Cars[] car_array = new Cars[6]; |
Until you added any object, null objects are contain.
|
|

11-14-2008, 02:19 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
|
ah so thats how you link it...thx!
|
|

11-14-2008, 05:52 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
|
Did you solve the question? What you mean by link?
|
|

11-14-2008, 06:59 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
dont worry that part is solved
|
|

11-15-2008, 04:20 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
|
So it' better if you can mark the thread solved.
|
|

11-15-2008, 02:10 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
ok calm down
|
|

11-17-2008, 05:28 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
|
|
Originally Posted by SKYLINEGTR
|
ok calm down
|
I'm always. Want to make our community is the best place.
|
|

11-18-2008, 01:09 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 14
Rep Power: 0
|
|
|
jhujbvyeddc tf tf ju
Last edited by SKYLINEGTR; 11-21-2008 at 02:46 PM.
|
|
| 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
|
|
|
All times are GMT +2. The time now is 11:00 AM.
|
|