Page 2 of 5 FirstFirst 1234 ... LastLast
Results 21 to 40 of 93
Like Tree4Likes

Thread: adding an object to an array in another class

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

    Default Re: adding an object to an array in another class

    I should have made it more clear which ones were in each class.




    IN HOTEL CLASS
    Java Code:
    private Room[]theRooms;
    Now in my Room class I have a toString method:
    toString()provides all the details of a room - 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.

    IN ROOM CLASS
    Java Code:
    public  String toString()          {            
                return("Room Number: " + getRoomNum() + '\n' + "Occupant name: " + getOccupant() + '\n' + "Smoking Room: " + 
                                        getSmoking() + '\n' + "Bed Type: " + getBedType() + '\n' + "Rate: " + getRoomRate() + '\n' + '\n');
              }
    I have realized, as I work through my tester, that I need the toString method to return based on the iterations of the array. Now the Room class toString method is only called in the Hotel class


    IN HOTEL CLASS
    Like here:
    Java Code:
    public void printReservationList()        {  
                            System.out.println("Reservation List");
                for(int i = 0; i < theRooms.length; i++)
                {
                    if(theRooms[i].isOccupied()==true)
                        
                        System.out.print (newRoom.toString());    
                }
            }

    Right now it is simply returning blank values:



    Room Number: 0
    Occupant name:
    Smoking Room:
    Bed Type:
    Rate: 0.0[/QUOTE]

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

    Default Re: adding an object to an array in another class

    OH, and yes the add room appears to be working in the addReservation method which compares smoking /nonsmoking and bed size to see if it is available.

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    In you print statement you don't need to specify toString(). It is done automatically. That is what is so nice about it.

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

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

    Default Re: adding an object to an array in another class

    What?? I have a toString method in both classes. So how would I call to print the toString in the room class from the hotel class AND have it iterate through the array in the hotel class? And please forgive me, toString is something evidently thrown into this assignment for the fun of it....it has also not been covered in this course so it took me a bit to even figure out how to write it.....

    let me give it a guess:

    System.out.print (isRooms[].newRoom());
    Last edited by jwood; 07-31-2013 at 05:28 PM.

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

    Default Re: adding an object to an array in another class

    sorry... that should be

    System.out.print (theRooms[].newRoom());

    I was up until 4 this morning working on this project...my brain is only half functioning...

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: adding an object to an array in another class

    jim is referring to this:
    Java Code:
    System.out.print (newRoom.toString());
    You don't actually need to have the toString() method there as print(Object) calls toString on the given object anyway.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #27
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    You still have to use a reference to the object. You just don't need to explicitly call the toString() method. It is no different than any other method other than when the class reference is used within the context of a String, the toString method is called automatically. I told you about it because it can facilitate debugging.

    The following give identical behavior:
    Java Code:
    System.out.println(foo);
    System.out.println(foo.toString());
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: adding an object to an array in another class

    Ok, so I could just tell it to print (newRoom). And again please forgive my ignorance as I try to figure this out....this class only covers chapter 1 - 8....toString is in chapter 10....the whole course has been like this( even on quizes), so it is very frustrating.

    OK....so here is what I have to do:


    1. toString() – returns a nicely formatted string giving hotel and room details (by calling the toString() in the Room class) for all the rooms in the hotel.

    but this: (by calling the toString() in the Room class) has to iterate through the array in the hotel class.

    I do not know how to do that.....

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

    Default Re: adding an object to an array in another class

    Quote Originally Posted by jim829 View Post
    when the class reference is used within the context of a String, the toString method is called automatically.
    Um, no. There's nothing 'automatic' about it.

    The PrinttStream#println(...) and print(...) methods have several overloads. You can see what each one does by looking at the JDK source (located in scr.zip in your JDK install folder).

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

  10. #30
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    What they are saying is that the Hotel's toString method should return a String of the concatenation of all the rooms. And when you iterate thru the rooms in the Hotel's toString() method, simply use the Room's toString method to help with the process.

    Regards,
    Jim
    Last edited by jim829; 07-31-2013 at 06:29 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: adding an object to an array in another class

    And when you iterate thru the rooms in the Hotel's toString() method, simply use the Room's toString method to help with the process.
    And that is what I can't figure out....here is what I have currently:

    Room Class toString:
    Java Code:
    public  String toString()
              {            
                return("Room Number: " + getRoomNum() + '\n' + "Occupant name: " + getOccupant() + '\n' + "Smoking Room: " + 
                                        getSmoking() + '\n' + "Bed Type: " + getBedType() + '\n' + "Rate: " + getRoomRate() + '\n' + '\n');
              }

    Hotel Class toString
    Java Code:
    public String toString()
            {    
                
                return ("Hotel Name: " + getName() + '\n' + "Number of Rooms: " + numOfRooms + '\n' + "Number of Occupied Rooms: "
                            + occupiedCnt + '\n' + '\n' + newRoom);
            }

    I need newRoom to iterate through theRooms[] I know what I need to do, I just do not know how....
    Last edited by jwood; 07-31-2013 at 06:49 PM.

  12. #32
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    Okay, here is an example (reluctantly):

    Java Code:
    class Bar {
       Mixers [] mixers = new Mixers[10]; // Assume it got filled in
       public String toString() {
             String str = "There are " + mixers.length + " mixers in stock\n";
             for (int j = 0; j < mixers.length; j++) {
                 str = str + mixers[j] + "\n";
            }
            return str;
       }
    }
    
    class Mixers {
       String name;
       public String toString() {
           return name;
       }
    }
    Untested so hopefully I did not make a mistake.

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

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

    Default Re: adding an object to an array in another class

    am I anywhere close?

    Java Code:
    public String toString()
    		{	
    			return ("Hotel Name: " + getName() + '\n' + "Number of Rooms: " + numOfRooms + '\n' + "Number of Occupied Rooms: "
    						+ occupiedCnt + '\n' + '\n' );
    			for(int i = 0; i < numOfRooms; i++)
    			{
    				theRooms[i].newRoom;
    			}
    		}

  14. #34
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    Well, your for loop is not accomplishing much. In fact, that code will never be reached because of the return. Returning a value should be the last thing you do.

    Edit: Well, returning a value is always the last thing one does ;). It should be the last statement.

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

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

    Default Re: adding an object to an array in another class

    I am completely lost ...should I do something like this then?
    Java Code:
        public String toString()
            {    
                System.out.print("Hotel Name: " + getName() + '\n' + "Number of Rooms: " + numOfRooms + '\n' + "Number of Occupied Rooms: "
                            + occupiedCnt + '\n' + '\n' );
                for(int i = 0; i < numOfRooms; i++)
                {
                    theRooms[i].newRoom;
                }
                 return //something here;
            }
    Last edited by jwood; 07-31-2013 at 07:10 PM.

  16. #36
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    Did you see my example?

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

  17. #37
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: adding an object to an array in another class

    What you want to do in all toString methods is to return a string. It can be a simple return of some instance field or a concatenation of multiple instance fields. Some of the instance fields may be references to other classes. This means you can also use the return value of their toString method in the one you are working on.

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

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

    Default Re: adding an object to an array in another class

    Quote Originally Posted by jim829 View Post
    Did you see my example?

    Regards,
    Jim
    No I had not seen it....but here is my problem in that example:

    I need name to iterate through the array, not mixer
    Java Code:
    class Mixers {
       String name;
       public String toString() {
           return name;

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

    Default Re: adding an object to an array in another class

    am I getting closer?
    Java Code:
    public String toString()
            {    String aRooms = newRoom;
                String hotel = "Hotel Name: " + getName() + '\n' + "Number of Rooms: " + numOfRooms + '\n' + "Number of Occupied Rooms: "
                            + occupiedCnt + '\n' + '\n';
                for(int i = 0; i < numOfRooms; i++)
                {
                    theRooms[i].aRooms;
                }
                return hotel + aRooms; 
            }
    I know this part theRooms[i].aRooms; is wrong.
    Last edited by jwood; 07-31-2013 at 07:47 PM.

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

    Default Re: adding an object to an array in another class

    Also, I know this should be simple, but I am having problems with it. I need boolean method isOccupied to return true if occupied is true. so I feel like I need an if statement.

    This is what I have:
    Java Code:
    public boolean isOccupied()
    	  	{			
    			if(occupied == true)
    				return true;
    			else
    				return false;	  	
    	  	}
    but when I call the isOccupied method in another class it is not working properly.
    Java Code:
    public double getDailySales()
    		{
    			
    			for(int i = 0; i < numOfRooms; i++)
    			{
    				if(theRooms[i].isOccupied==true)
    				{
                                            double sum = 0;
    					sum  = sum + theRooms[i].getRoomRate();
    				}
    			}
    			return sum;
    		}
    This is the snippet I am having problems with

    if(theRooms[i].isOccupied==true)

    because I have to determine if isOccupied equals true or false as I go through the iteration.

Page 2 of 5 FirstFirst 1234 ... LastLast

Similar Threads

  1. null pointer when adding object into an array
    By NoobieCode in forum New To Java
    Replies: 6
    Last Post: 07-24-2013, 11:58 PM
  2. Replies: 15
    Last Post: 08-19-2011, 03:08 AM
  3. help adding to array from another class
    By Grid_iso in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 10:00 AM
  4. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 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
  •