Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default [SOLVED] Please help with java

    shasduybhkvk yuvk yu
    Last edited by SKYLINEGTR; 11-21-2008 at 01:45 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to look up and use enums. Check the Sun tutorials for info on this.

  3. #3
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    enums?? never even heard of these but ok ill take a look

    thx

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    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 }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes enums can holds and return any primitive data type. Seems to me you don't have clearly read about enums.

  8. #8
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    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..

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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()
         }
    }

  10. #10
    laxminarayana is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default

    hello hai i want help to develop java coding

  11. #11
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    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

  12. #12
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    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

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    An array is a collection of same data type.

    Java 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.

    Java Code:
    Cars[] car_array = new Cars[6];
    Until you added any object, null objects are contain.

  14. #14
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    ah so thats how you link it...thx!

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  16. #16
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    dont worry that part is solved :)

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  18. #18
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    ok calm down :D

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by SKYLINEGTR View Post
    ok calm down :D
    I'm always. ;) Want to make our community is the best place.

  20. #20
    SKYLINEGTR is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    jhujbvyeddc tf tf ju
    Last edited by SKYLINEGTR; 11-21-2008 at 01:46 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •