Page 1 of 2 12 LastLast
Results 1 to 20 of 32
Like Tree3Likes

Thread: unsure on a method

  1. #1
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default unsure on a method

    Here are the directions:


    1. When the cancelReservation() method executes, the hotel will search for the name of the visitor in each room. If it is found, the occupied attribute will be set to false. In either case a message will state whether or not the reservation was cancelled.This method calls the private utility method findReservation()to scan the list of rooms looking for a guest by name. It will return the index of the room in the Arrayof rooms or NOT_FOUND if the room is not found, which will be declared as:


    private static final int NOT_FOUND = -1;


    1. findReservation()will take in a String representing the occupant’s name and search the occupied rooms for a reservation with that person’s name. It will return the index of the room or NOT_FOUND if not found.

    So I did the findReservation() method first:

    Java Code:
    private int findReservtion(String occName)
    		{	
    			int index = 0;
    			for(int i = 0; i < theRooms.length; i++)
    			{
    				if(theRooms[i].getOccupant()== occName)
    					return index;
    				else
    					index++;				
    			}
    			return NOT_FOUND;					
    		}


    I am not sure if I set it up correctly so that the return NOT_FOUND only returns after the loop has finished and if there is no match.

    I am at a loss as to how to do the cancelReservation() method. I am unsure how to call the findReservation method to the cancelReservation() method. I think that somehow I have to call the return of findReservation and do an if/else depending on the result, but I am not sure how to call the result....String

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: unsure on a method

    Hi,

    For the findReservation() method you can begin by setting the index to NOT_FOUND. Then you iterate the room, when the room's occupant names is equals to occupant name arguments you can set the index to the current iteration index (i) and break the loop. Finally you can return the index from the findReservation method.

    Remember when you need to compare string use the equals() method and not using the == operator.

    The cancelReservation method would simply call the findReservation method and pass the occupant name. Then you can check the return of the findReservation, if the return value is NOT_FOUND you should print a message indicating that no guess found. Otherwise you set the room's occupied index to false.
    jwood likes this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: unsure on a method

    Why don't you simply return i in line #7? You're supposed to return the index of the matching room ... b.t.w. the == comparison in line #6 is incorrect; you can't compare Strings for equality with the == operator; use the String.equals( ... ) method instead.

    kind regards,

    Jos

    edit: too slow again ...
    jwood likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    How do I call the findReservation method and check the return? That is what I am having the hardest time figuring out?

    Does this look better for the findReservation?
    Java Code:
    private int findReservation(String occName)
            {    
                int index = NOT_FOUND;
                for(int i = 0; i < theRooms.length; i++)
                {
                    if(theRooms[i].getOccupant().equals(occName))
                        index = i;
                                    
                }
                return index;                    
            }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: unsure on a method

    Quote Originally Posted by jwood View Post
    How do I call the findReservation method and check the return? That is what I am having the hardest time figuring out?
    Time for a tutorial, then: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Post Re: unsure on a method


    Yeah, not so helpful....I know how to set up a return value in a method. What I can't seem to figure out is calling a private methods return value to a public method. I have no issue public to public...but private to public I am getting errors like this:

    method findReservation in class Hotel cannot be applied to given types; yet the exact same "type" from a public method works....

    here is what I have:

    Java Code:
           public String cancelReservation(String occName)
            {    
                
                int i = findReservation();        //Part with error
                if(i >= 0)
                {
                    theRooms[i].setOccupied(false);
                    occupiedCnt--;
                    System.out.println("Reservation cancelled.");
                }
                else
                    System.out.println("Reservation not found.");            
            }
            
        private int findReservation(String occName)
            {    
                int index = NOT_FOUND;
                for(int i = 0; i < theRooms.length; i++)
                {
                    if(theRooms[i].getOccupant().equals(occName))
                        index = i;
                }
                return index;                    
            }
    Last edited by jwood; 07-30-2013 at 04:48 PM.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,590
    Rep Power
    5

    Default Re: unsure on a method

    You're talking about multiple methods here. So post that code, and please mention which exact line is causing the troubles.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    Quote Originally Posted by gimbal2 View Post
    You're talking about multiple methods here. So post that code, and please mention which exact line is causing the troubles.
    see previous post. Line 4 is the issue.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: unsure on a method

    Java Code:
    int i = findReservation();
    But the method looks like:
    Java Code:
    private int findReservation(String occName)
    It expects a String as a parameter...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    but it returns an int. The return is what I am trying to get I think....

    Here are my directions for these two methods:

    cancelReservation(String)
    findReservation(String): int


    1. When the cancelReservation() method executes, the hotel will search for the name of the visitor in each room. If it is found, the occupied attribute will be set to false. In either case a message will state whether or not the reservation was cancelled.This method calls the private utility method findReservation()to scan the list of rooms looking for a guest by name. It will return the index of the room in the Arrayof rooms or NOT_FOUND if the room is not found, which will be declared as:


    private static final int NOT_FOUND = -1;


    1. findReservation()will take in a String representing the occupant’s name and search the occupied rooms for a reservation with that person’s name. It will return the index of the room or NOT_FOUND if not found.


  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,328
    Rep Power
    5

    Default Re: unsure on a method

    Your program shouldn't even compile. Did you read and understand Tolls' reply? You are calling that method without an argument so the compiler complains.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    OK, I have been sitting and staring at this for hours...

    Java Code:
    public String cancelReservation(String occName)
            {    
                findReservation(occName);
                int i = findReservation;  //ERROR HERE
                if(i >= 0)
                {
                    theRooms[i].setOccupied(false);
                    occupiedCnt--;
                    System.out.println("Reservation cancelled.");
                }
                else
                    System.out.println("Reservation not found.");            
            }
    If I am thinking this through correctly, line 3 calls the findReservation method and sets the parameter to the same param in the cancelReservation method and thus starts the search in the array. Now I need to assign the return of findReservation to int i so I can work through my if/else statement. Am I thinking this through correctly?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: unsure on a method

    line 3 is also returning an int.
    In fact, that's the int you want.
    Forget about the following line, just use the int returned by line 3.
    Java Code:
    int i = findReservation(occName);
    I think I can see where your confusion is, but a forum isn't the easiest place to wheedle it out.
    jwood likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: unsure on a method

    I've already posted a tutorial link; one of the topics listed in the sidebar is 'Passing Information to a Method or a Constructor'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    Maybe not the easiest, but it has helped me the most! OK I made that change and that part compiles! Yay, but now line 13 says missing return statement? My brackets all match up.....any thoughts?

  16. #16
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    NVM...changed the system.out to return and it compiles. Thanks for the help!

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: unsure on a method

    Reading the last post I think the OP is looking at the call to findReservation as two steps...one to pass the occName parameter and the other to pick up the returned int.
    They understand (sort of) that it takes a parameter and returns an int, but not that that is a single line rather than two separate events.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,328
    Rep Power
    5

    Default Re: unsure on a method

    Well, your return type is a String, but nowhere do you return a String.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: unsure on a method

    thanks again. Now working on tester
    Last edited by jwood; 07-30-2013 at 07:28 PM.

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: unsure on a method

    What does the method return when none of the rooms is occupied?

    Additionally, never compare a boolean variable or expression to true or false. That's not only redundant, it's error prone.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  2. Unsure of whether to use key bindings
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 04-15-2012, 07:33 AM
  3. Replies: 3
    Last Post: 10-24-2011, 06:03 PM
  4. Replies: 1
    Last Post: 04-01-2010, 05:14 PM
  5. [SOLVED] Key listener, unsure abt the tab key
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 04-16-2009, 05:49 AM

Posting Permissions

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