Results 1 to 20 of 30
Thread: [SOLVED] Please help with java
- 11-11-2008, 10:34 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
-
You need to look up and use enums. Check the Sun tutorials for info on this.
- 11-11-2008, 10:46 AM #3
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, 10:52 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-12-2008, 10:35 AM #5
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 }
-
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, 04:09 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes enums can holds and return any primitive data type. Seems to me you don't have clearly read about enums.
- 11-12-2008, 07:01 PM #8
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, 03:52 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Java 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, 10:48 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
hello hai i want help to develop java coding
- 11-13-2008, 06:32 PM #11
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, 07:41 PM #12
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, 04:26 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
An array is a collection of same data type.
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.Java Code:private double[] cars;
Until you added any object, null objects are contain.Java Code:Cars[] car_array = new Cars[6];
- 11-14-2008, 01:19 PM #14
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
ah so thats how you link it...thx!
- 11-14-2008, 04:52 PM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you solve the question? What you mean by link?
- 11-14-2008, 05:59 PM #16
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
dont worry that part is solved :)
- 11-15-2008, 03:20 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So it' better if you can mark the thread solved.
- 11-15-2008, 01:10 PM #18
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
ok calm down :D
- 11-17-2008, 04:28 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-18-2008, 12:09 AM #20
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks