Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Objects, classes, lists true and false HELP!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Objects, classes, lists true and false HELP!

    Hello, I'm getting depressed over here, can't get anything done, so I figured I'd post one of my problems here, to see what happends.
    Let's say I have this hotel, and I want to see what rooms are available, can I add my rooms to a list, and make them true & false, then only display 'true' rooms when checking what rooms are available?
    I'm sorry I explain badly, it's getting late, but please, try to help.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Objects, classes, lists true and false HELP!

    Yes, you could. Try that and come back with code when you'd like some help.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Objects, classes, lists true and false HELP!

    Quote Originally Posted by GregBrannon View Post
    Yes, you could. Try that and come back with code when you'd like some help.
    Well I have alittle, I don't know if im on the right track or anything, but here it is

    It's just a few rooms, with like.. a boolean, then it prints out everything, what I would like is for it to only print the 'true', but idk

    public class RoomsTest {
     
        public static void main(String[] args) {
            List<Rooms> Room = new ArrayList<Rooms>();
     
            Rooms RoomA1 = new Rooms();
            Rooms RoomA2 = new Rooms();
            Rooms RoomA3 = new Rooms();
            Rooms RoomA4 = new Rooms();
     
            RoomA1.setBuilding("A");
            RoomA1.setNumber("1");
            RoomA1.setAvailable(true);
     
     
            RoomA2.setBuilding("A");
            RoomA2.setNumber("2");
            RoomA2.setAvailable(true);
     
            RoomA3.setBuilding("A");
            RoomA3.setNumber("3");
            RoomA3.setAvailable(true);
     
            RoomA4.setBuilding("A");
            RoomA4.setNumber("4");
            RoomA4.setAvailable(false);
     
            Room.add(RoomA1);
            Room.add(RoomA2);
            Room.add(RoomA3);
            Room.add(RoomA4);
     
            for ( Rooms x: Room )
            	System.out.print("\n"+(x)+" Available: "+x.isAvailable());

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Objects, classes, lists true and false HELP!

    Your variable naming could be improved and might help you sort it and keep it straight in your mind.

    Rooms should be a class called "Room" that contains the attributes for a single room. A hotel, nursing home, or some other place with multiple Room(s) would then have a collection of Room(s), like in . . .

    An ArrayList is a collection of like objects, so I would call the ArrayList rooms:

    List<Room> rooms = new ArrayList<Room>();

    Notice that class names are capitalized and variable names are not.

    Then, you could change this part:
    Rooms RoomA1 = new Rooms();
    Rooms RoomA2 = new Rooms();
    Rooms RoomA3 = new Rooms();
    Rooms RoomA4 = new Rooms();
    To:
    for ( int i = 0 ; i < 4 ; i++ )
    {
        rooms.add( new Room() );
    }
    That eliminates the (incorrectly) named Room objects with different endings, like "RoomA2". Naming variables that way is annoying and difficult to keep track of and program efficiently.

    To do this part with the ArrayList of Room objects:
    RoomA1.setBuilding("A");
    RoomA1.setNumber("1");
    RoomA1.setAvailable(true);
    You'd use the statements:
    rooms.get( 1 ).setBuilding( "A" );
    rooms.get( 1 ).setNumber( "1" );
    rooms.get( 1 ).setAvailable( true );
    which can now be done inside a loop that changes the index variable.

    To print the available rooms, iterate the collection rooms, and print those where room.available = true.

    Go down this path for a while, changing your code over as I've shown above, and come back when you need help. Be sure to post your latest code when you come back.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    klskl (October 18th, 2013)

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Objects, classes, lists true and false HELP!

    Wow, thanks, super helpful! I've accually gotten quite a bit on the rest of my code since my last post, so this was sweet, maybe it will become something in the end after all ;D thanks again.

Similar Threads

  1. Objects and Classes
    By Worst Programmer Ever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 03:13 PM
  2. Objects and Classes
    By Kristenw17 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 23rd, 2013, 12:43 AM
  3. Replies: 0
    Last Post: January 22nd, 2013, 10:15 PM
  4. Replies: 0
    Last Post: May 10th, 2012, 07:59 PM
  5. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM