Page 3 of 5 FirstFirst 12345 LastLast
Results 41 to 60 of 93
Like Tree4Likes

Thread: adding an object to an array in another class

  1. #41
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

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

    1. For your isOccupied()method, just return occupied. No if/else needed.
    2. isOccupied() is a method so you need closing (). And when using isOccupied(), don't compare to true or false, just say:
    Java Code:
     if (theRooms[i].isOccupied()) {
                  // do stuff
      }
    3. You keep resetting sum within the loop.

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

  2. #42
    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
    1. For your isOccupied()method, just return occupied. No if/else needed.
    2. isOccupied() is a method so you need closing (). And when using isOccupied(), don't compare to true or false, just say:
    Java Code:
     if (theRooms[i].isOccupied()) {
                  // do stuff
      }
    3. You keep resetting sum within the loop.

    Regards,
    Jim

    Don't I want to keep resetting the sum in the loop? Adding the rate of each occupied room to get the total rates of currently ?

  3. #43
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

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

    Quote Originally Posted by jwood View Post
    Don't I want to keep resetting the sum in the loop? Adding the rate of each occupied room to get the total rates of currently ?
    By resetting I mean setting sum back to 0. You are wiping out whatever sum you have each time.

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

  4. #44
    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

    Ah...fixed it I think...
    Java Code:
    public double getDailySales()
    		{
    			double sum = 0;
    			for(int i = 0; i < numOfRooms; i++)
    			{
    				if(theRooms[i].isOccupied())
    				{					
    					sum  = sum + theRooms[i].getRoomRate();
    				}
    			}
    			return sum;
    		}

  5. #45
    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 think the last thing I am still confused about is the toString.....I can't figure out how to iterate the Room toString through the Hotel Room[] theRooms array....

    Quote Originally Posted by jwood View Post
    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.

  6. #46
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

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

    First, I am not certain why you need the String aRooms statement so for now, remove it.
    Inside the loop replace the single statement with the following:
    hotel = hotel + theRooms[i] + "\n"; // you may not need the \n if the Rooms version of toString provides it.
    Then when you exit the loop, just return "hotel"

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

  7. #47
    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

    Thanks!!!!!!!!!

    OK, so let me think this through....

    hotel = hotel + theRooms[i] ;

    theRooms[i] automatically returns the room toString iteration for each element of the array? Is that something simply integrated into the toStrings method? I have been under the impression this whole time I needed to "manually" tell it to do the iteration.


  8. #48
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

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

    Exactly!! There are some situations where you need to specify the toString() explicitly, like the following:

    Foo foo = new Foo();
    String a = foo; // this will not work!!!
    String a = foo.toString(); // so use this
    or you can force a string context and do this
    String a = foo + "";

    But it is most useful when you use it in a print statement.

    System.out.println(foo);

    As long as you have constructed the toString() method to return a useful string, it really helps.

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

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

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

    Quote Originally Posted by jwood View Post
    theRooms[i] automatically returns the room toString iteration for each element of the array?
    No it doesn't. See #29.

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

  10. #50
    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

    Well, so much for my understanding arrays....I thought I had this part down.

    Directions
    addReservation() will add a car reservation to the rental car company. As each car is added, a Car object should be added to theCars array and the other fields of the RentalCarCompany class should be updated appropriately. Note that the Carobject should be added to the first available location in the array indicated by the rentCnt field.

    I have tried this:
    Java Code:
    public void addReservation(String aCarRenter, int aCarNum, String aCarName, String aCarType, int aCarDoor, double aRate, int aDays)
                {    for(int i = 0; i < NUM_CARS; i++)
                    {
                        theCars[i].setCarRenter(aCarRenter);
                        theCars[i].setCarNum(aCarNum);
                        theCars[i].setCarName(aCarName);
                        theCars[i].setCarType(aCarType);
                        theCars[i].setCarDoor(aCarDoor);
                        theCars[i].setCarRate(aRate);
                        theCars[i].setNumOfDays(aDays);
                        rentCnt++;
                    }
              }
    And this:

    Java Code:
    public void addReservation(String aCarRenter, int aCarNum, String aCarName, String aCarType, int aCarDoor, 
                                double aRate, int aDays)
                {    
                    theCars[rentCnt].setCarRenter(aCarRenter);
                    theCars[rentCnt].setCarNum(aCarNum);
                    theCars[rentCnt].setCarName(aCarName);
                    theCars[rentCnt].setCarType(aCarType);
                    theCars[rentCnt].setCarDoor(aCarDoor);
                    theCars[rentCnt].setCarRate(aRate);
                    theCars[rentCnt].setNumOfDays(aDays);
                    rentCnt++;
              }
    Exception in thread "main" java.lang.NullPointerException
    at RentalCarCompany.addReservation(RentalCarCompany.j ava:158) LINE 4


    Tester

    Java Code:
    public static void main(String[] args)
            {
                RentalCarCompany newCompany = new RentalCarCompany("El Cheapo");
                Car newCar = new Car();
                
                newCar.setCarRenter("bob");
                System.out.print(newCar.getCarRenter());
                        
                newCompany.addReservation("Blake", 123, "Nissian", "compact", 2, 23.99, 5);
                     }
    So how is it not receiving the information I am inputting? I feel like an idiot....

  11. #51
    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

    Please forgive / ignore my last post. After staying up until 4 this morning working on these projects, and getting up early and working on them all day my brain is not functioning very well. My mistake was obvious once I stepped away to eat and came back....I think I shall take an extended break and start fresh tomorrow. Again my apologies.

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

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

    That's right. Get a good night's rest.

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

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

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

    And take a healthy breakfast, no gut-expanding serials ;) Bacon and eggs: the ultimate brain food!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

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

    Quote Originally Posted by gimbal2 View Post
    And take a healthy breakfast, no gut-expanding serials ;) Bacon and eggs: the ultimate brain food!
    TV serials are gut-expanding? ;)
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #55
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

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

    Quote Originally Posted by DarrylBurke View Post
    TV serials are gut-expanding? ;)
    They are if you watch them while eating tv dinners.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

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

    Quote Originally Posted by gimbal2 View Post
    And take a healthy breakfast, no gut-expanding serials ;) Bacon and eggs: the ultimate brain food!
    Hamburger; the basic nutricient for every healthy breakfast!

    kind regards,

    Jos
    Last edited by JosAH; 08-01-2013 at 01:25 PM. Reason: I can't even spell ...
    cenosillicaphobia: the fear for an empty beer glass

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

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

    Quote Originally Posted by DarrylBurke View Post
    TV serials are gut-expanding? ;)
    Crud. Cereals! I meant cereals!

    But yeah, serials are hard to digest too.

    Quote Originally Posted by JosAH View Post
    Hamburger; the basic nutricient for every healthy breakfast!
    It is uttered as a sarcastic joke in the movie, but it's actually kind of true. IF you ditch the bun. Protein and fats ftw.

    Erik ( <-- watched the hilarious "Fat Head" documentary and had his eyes yanked open)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

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

    Quote Originally Posted by gimbal2 View Post
    It is uttered as a sarcastic joke in the movie, but it's actually kind of true. IF you ditch the bun. Protein and fats ftw.
    The strangest breakfast I had once was served by an exchange student from Uganda (or Rwanda, I don't remember); it was a huge maggot rolled up in a palm leaf; I must admit that it tasted quite well;later he told me that the maggot was still alive (until I ate it) ...

    kind regards,

    Jos (<--- just toast and loads of espresso coffee)
    cenosillicaphobia: the fear for an empty beer glass

  19. #59
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

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

    Darryl,

    I guess what I meant was that you don't have to specify the toString() method explicitly. For example, does your explanation explain why the following works?

    For some object foo.

    String a = foo + "";

    Here the foo.toString() is invoked. Perhaps automatically is the wrong term. Perhaps I should have said implicitly. How would you describe it?

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

  20. #60
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

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

    C'mon Darryl,

    Automatic simply means "not having to take explicit action for something to happen." If you don't specify the toString() method call, then it happens automatically, whether its done thru subsequent calls in the JDK via overloading or by the JRE itself. If I write some code that calls a method1() and method2() is called inside method1(), then when method1() is called, method2() is called "automatically." Otherwise, nothing in this world happens automatically because there is always a cause and effect relationship.

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

Page 3 of 5 FirstFirst 12345 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
  •