Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: missing return statement

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

    Default missing return statement

    Starting a new assignment, and getting down the skeleton of one of my class files and I am getting a missing return statement on lines 53, 61, and 102. I have checked that all my braces are in pairs, so I am at a loss as to what this error is referring to. Any help?
    Java Code:
    /**   Room class represents information about each room.
             
       
       
    
    
       
       
    */ 
        public class Room
        {        
              /**
              */
            private int roomNum;
          
              /**
              */
              private String bedType;
          
              /**
              */
              private double rate;
          
              /**
              */
              private String occupantName;
          
              /**
              */
              private char smoking;
          
              /**
              */
              private boolean occupied;
          
              /**
              */
              public Room()
              {
              }
          
              /**
              */
              public Room(int roomNum, String bedType, char smoking, double rate)
              {
              }
          
              /**
              */
              public boolean isOccupied()
              {
                occupied = true;          
              }
                      
              /**
              */
              public String toString()
              {
                System.out.println("Room Number: " + getRoomNum() + '\n' + "Occupant name: " + getOccupant() + '\n' + "Smoking Room: " + 
                                        getSmoking() + '\n' + "Bed Type: " + getBedType() + '\n' + "Rate: " + getRoomRate());
              }
          
              /**
              */
              public String getBedType()
              {
                 return bedType;
              }
          
              /**
              */
              public char getSmoking()
              {
                    return smoking;
              }
            
            /**
           */
            public int getRoomNum()
            {
                return roomNum;
            }
            
            /**
           */
            public double getRoomRate()
            {
                return rate;
            }
            
            /**
           */
            public String getOccupant()
            {
                return occupantName;
            }
            
            /**
           */
            public boolean setOccupied()
            {
            }
            
            /**
           */
            public void setOccupant(String newOccupant)
            {
                occupantName = newOccupant;
            }
            
            /**
           */
            public void setRoomNum(int newRoomNum)
            {
                roomNum = newRoomNum;
            }
            
            /**
           */
            public void setBedType(String newBedType)
            {
                bedType = newBedType;
            }
            
            /**
           */
            public void setRate(double newRate)
            {
                rate = newRate;
            }
            
            /**
           */ 
            public void setSmoking(char newSmoking)
            {
                smoking = newSmoking;
            }    
        }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: missing return statement

    Java Code:
              public boolean isOccupied()
              {
                occupied = true;         
              }
    That says the method returns a boolean, so the compiler expects you to return a boolean. You could either change the method to return void (nothing) or you could add a return statement. By convention, a method whose name starts with "is" should return something.
    Get in the habit of using standard Java naming conventions!

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

    Default Re: missing return statement

    OK, so I changed it to this:

    Java Code:
     	public boolean isOccupied()	  	{
    			return occupied;	  	
    	  	}
    and that worked for one of the errors...but I am still getting a missing return statement for line 61 and 102

  4. #4
    AlexHail is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: missing return statement

    Quote Originally Posted by jwood View Post
    OK, so I changed it to this:

    Java Code:
     	public boolean isOccupied()	  	{
    			return occupied;	  	
    	  	}
    and that worked for one of the errors...but I am still getting a missing return statement for line 61 and 102
    Same problem mate. You have methods that returning instances of the type you're calling.

    On line 61 you are calling a return type method for String, so it has to either return a String value or a null value, you can't just print something out in that method. Exact same problem with the boolean value on line 61.

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

    Default Re: missing return statement

    Quote Originally Posted by AlexHail View Post
    Same problem mate. You have methods that returning instances of the type you're calling.

    On line 61 you are calling a return type method for String, so it has to either return a String value or a null value, you can't just print something out in that method. Exact same problem with the boolean value on line 61.
    OK, the directions for the toString read:

    3. toString() provides all the details of aroom - room number, name of guest(if occupied) , bed type, smoking/non-smoking, rental rate. This should all be formatted nicely with one attribute on each line using the '\n' escape character. See above example.

    Example:
    Room Number: 105
    Occupant name: Not Occupied
    Smoking room: n
    Bed Type: queen
    Rate: 99.0

    So I am not sure what exactly to do with that.....

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: missing return statement

    Typically a toString method will concatenate several bits of information into a single string and return that. eg

    Java Code:
    class Person {
        private String firstName;
        private String LastName;
    
        // constructor
    
        public String toString() {
            return firstName + " " + lastName;
        }
    }
    Using a StringBuilder instead of concatenating lots of smaller Strings is advisable.

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

    Default Re: missing return statement

    We have only learned concatenating. I do not know what a StringBuilder is. Basically this assignment covers information learned in chapters 5,6, and 7 in Big Java. The teacher is pretty much wants us to follow his outline...

    Room
    roomNum: int
    bedType: String
    rate: double
    occupantName: String
    smoking: char
    occupied: boolean
    Room()

    Room(int,String,char,double)
    getBedType(): String
    getSmoking(): char
    getRoomNum(): int
    getRoomRate(): double
    getOccupant(): String
    setOccupied(boolean)
    setOccupant(String)
    setRoomNum(int)
    setBedType(String)
    setRate(double)
    setSmoking(char)

    isOccupied(): boolean
    toString(): String

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: missing return statement

    Do you have a question?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: missing return statement

    Quote Originally Posted by jwood View Post
    We have only learned concatenating.
    Your current code has already concatenated the String together...instead of passing that to println just use 'return'.
    jwood likes this.
    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: missing return statement

    Another question...

    How would you create an instance variable given only this:


    theRooms: Array Room[]


    It has me stumped. I have two classes: Hotel and Room. This is suppose to be an instance variable in the Hotel class. private Room[] creates the array named Room...so what is theRooms? Shouldn't I have a type for this? The array will be filled with "rooms", and each "room" will have an int, String, char, double. (addRoom(int,String,char,double).

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: missing return statement

    theRooms is the name of the attribute.
    Room[] is the type (an array of Room objects).
    jwood likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: missing return statement

    OK, so my instance variable would be this?

    private Room[]theRooms;

    So later in my constructor, when I want to assign a length to the array I would write:

    Room[] theRooms = new Room[10];

    that confuses me...I thought the type had to be String, int, double, etc..

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: missing return statement

    Why?
    It's basic Java...Classes and Objects.
    Room is a class (like String, but not like int/double/float which are primitives).
    Room[] is an array of Room objects.

    Note that:
    Java Code:
    Room[] theRooms = new Room[10];
    is an array of rooms, each of which is null until you assign an actual room to it.
    Which I expect is what the addRoom method is going to do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: missing return statement

    Quote Originally Posted by jwood View Post
    that confuses me...I thought the type had to be String, int, double, etc..
    This statement confuses me to be honest. Can you explain a little more WHY you thought this? Of what did you expect the type to be String, int, double, etc. ?

    I ask because I have a gut feeling there is a major misunderstanding going on somewhere here.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: missing return statement

    Ahhh......things have not been explained in the best way in this class...it has not been taught like the intro class it is suppose to be, so I struggled with most of it. That makes sense now!

    Yes addRooms adds the rooms to the array.

    So when I am adding a room to the array am I calling room.add or theRooms.add?

    Here is how I am understanding it so far:

    Room = Type of array variable
    theRooms = Name of array variable
    new Room = Element type
    [10] = length of the array

Similar Threads

  1. missing return statement
    By brian in forum New To Java
    Replies: 2
    Last Post: 05-27-2013, 09:02 PM
  2. missing return statement need help!!
    By windywong in forum New To Java
    Replies: 3
    Last Post: 03-27-2013, 02:30 PM
  3. Missing return statement
    By Aenohe in forum New To Java
    Replies: 8
    Last Post: 03-07-2012, 02:03 PM
  4. Missing Return statement =[
    By avirunes in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 11:34 AM
  5. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 04:15 PM

Posting Permissions

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