• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Object trouble.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 69823
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 .
     
    Marshal
    Posts: 25682
    69
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.
     
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Ranch Hand
    Posts: 130
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Hey! You're stepping on my hand! Help me tiny ad!
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic