wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Object trouble. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object trouble. " Watch "Object trouble. " New topic
Author

Object trouble.

Brandon Choate
Greenhorn

Joined: Mar 06, 2009
Posts: 14
Hello once again. This week I'm working with objects. I have a hotel program, which is supposed to have five different rooms.

My code is here:



There is more to the program, but as most of it is currently blank and of no bearing to the problem at hand, I decided to keep it out of the code you see here.

I suppose what I need to know is: Am I on the right track? Is this how one would go about assigning these numbers to each room object, or am I completely off track? All my books don't really cover the matter of multiple objects in detail.

Thanks in advance!
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
It seems like you have got confused between Hotel and Room classes. How about working on one at a time, since hotel needs Room lets work on Room first. What variables and methods do you think belong to Room? Can you post the updated Room class? Also remember to add a constructor to Room.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Never write if (xxx == false) or if (xxx == true). You write if (!xxx) or if (xxx). There are two reasons for avoiding that construct:
  • It is stylistically very awkward
  • If you write = instead of == by mistake, you can get all sorts of nasty errors which you didn't expect.

  • And Moojid Hamid's advice is very sound.
    Hesham Abo El-Magd
    Greenhorn

    Joined: Aug 21, 2008
    Posts: 9
    as Campbell said it's like you re-assigning the Boolean value to the variable in the if statement .

    the if condition is then evaluate to true and prints "Test is true here !"
    and use setters it good encapsulation .


    SCJP 5.0
    NetBeans Platform Certified Associate .
    Bachelor of Computer Science .
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8


    The code in your constructor is pointless; you declare some local variables, then assign them values. Those variables vanish as soon as the call to the constructor is complete. Perhaps those variables should be instance variables of the class.
    Rohit Kumar Gupta
    Greenhorn

    Joined: Apr 09, 2009
    Posts: 3
    am also new to java ... but am confused to see how you have assigned the values to Room variables





    here you are setting the valuses of all variables which were passed to you in the function addRoom
    and when you return the roomNumber in the end it send you the value 556 , because its the last value that you set for roomNumber.

    make a separate class Room with constructor for setting the values




    you can either create such an constructor or else create a function in Room class for assigning the values to a particular room


    i cant even understand what is use of this code


    here you need to check the occupied status of all the Room those has been created

    i hope it will be useful for you
    John Dell'Oso
    Ranch Hand

    Joined: Apr 08, 2004
    Posts: 130
    Brandon,

    In addition to the useful advice already given, I would suggest also using an array for your collection of rooms. In fact a collection class such as ArrayList would be even better. That way you can deal with any number of rooms instead of a fixed number of 5. I'm not sure how far along the Java "learning curve" you are, so if you have yet to learn about these data structures, then continue along the line you are currently on in conjunction with the advice already provided.

    Good luck.

    JD
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Object trouble.
     
    Similar Threads
    help with a booking system
    if statement
    Adding JLabels to a vector
    Problem building ArrayList for Hotel class
    Saving details of rooms