Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 55
Like Tree1Likes

Thread: What do I get?

  1. #21
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    And this is where I get stuck, can you assist on how I create the Floor objects and most importantly put them into the array?

    I imagine I simply create the Floor object by:

    Floor floor1 = new Floor()

    How do I put this into the array, say floors[1]

    thanks

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    How do you put ANY value into an array? You use an assignment statement.

    Reread the Tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #23
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Better?


    // File Elevator.java

    package cscie160.backup;

    public class Elevator
    {

    public Elevator () {

    currentFloor = 1;
    floors[currentFloor] = floor1;
    floor1.passengersWaiting = 6;

    }

    public String toString() {
    return "Currently " + floors[currentFloor].passengersWaiting + " waiting!";
    }

    public static void main(String argv[])
    {
    Elevator myElevator = new Elevator();
    System.out.println(myElevator.floors[myElevator.currentFloor].passengersWaiting);
    }

    protected final int MAX_FLOORS = 2;
    Floor floors[] = new Floor[MAX_FLOORS];
    Floor floor1 = new Floor();
    int currentFloor;

    }

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    Does it compile and execute with no errors?

  5. #25
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Yes, is everything defined in the correct area of the program?

    Latest Version of Elevator.java


    // File Elevator.java

    package cscie160.backup;

    public class Elevator
    {

    public Elevator () {

    currentFloor = 1;
    floors[1] = floor1;
    floor1.passengersWaiting = 7;
    floors[2] = floor2;
    floor2.passengersWaiting = 4;

    }

    public String toString() {
    return "Currently " + floors[currentFloor].passengersWaiting + " waiting!";
    }

    public static void main(String argv[])
    {
    Elevator myElevator = new Elevator();
    System.out.println(myElevator.floors[1].passengersWaiting);
    System.out.println(myElevator.floors[2].passengersWaiting);
    }

    protected final int MAX_FLOORS = 3;
    Floor floors[] = new Floor[MAX_FLOORS];
    Floor floor1 = new Floor();
    Floor floor2 = new Floor();
    int currentFloor;

    }


    Output:

    new-host:Desktop spm3$ javac cscie160/backup/Elevator.java
    new-host:Desktop spm3$ java cscie160/backup/Elevator
    7
    4
    new-host:Desktop spm3$

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    If it executes without errors and does what you want, it is probably OK.

  7. #27
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Thanks so much for your help, I feel that I made great progress tonight "understanding" OO programming!

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    Now you need to work on using an array in a loop. Remember that the first index to an array is 0.

  9. #29
    ruivale is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: What do I get?

    OK.

    I think I should make a public apology, first to smartino, and then to Norm! :-(
    I should've kept my "mouth" shut! Didn't mean to be rude ...

    Now, I'm off ...

    ...

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    smartino - You should remove the floor1 and floor2 variables from your code and assign the new Floor objects directly to the array.
    What if you needed to work with hundreds of Floor objects, you would NOT code floor1 thru floor100,

  11. #31
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: What do I get?

    I think this is one of those confusions about references and objects, or at least exacerbated by that.
    Ah, here's the link to the cups description of how it all fits together.

  12. #32
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Ok, I agree that floor1 and floor2 should be removed, but how else would I define the below?

    floors[1] = floor1;
    floor1.passengersWaiting = 7;
    floors[2] = floor2;
    floor2.passengersWaiting = 4;

    I need to have a separate passengersWaiting for each floor...

    thank you...

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    Remove ALL usages of the variables: floor1 and floor2 and assign the new Floor objects directly into the array and use them in the array.
    floors[1].passengersWaiting = 7;
    floors[2].passengersWaiting = 4;

    The floors array contains Floor objects. You get at individual objects by using a subscript: floors[ix] is a Floor object. Where you coded floor1 you can code floors[1]

  14. #34
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Excellent, the only remaining piece I don't get is that I am defining the array of floor objects in one program (Elevator.java) and passengersWaiting is an instance variable of another program (Floor.java), so I get an error since the passengersWaiting is private (which it needs to be). Is their anyway around this (without change the private label of passengersWaiting)?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    How did this work?
    floor2.passengersWaiting = 4;

    Add a set... method to the Floor class that will set the number of passengers waiting

  16. #36
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    floor2.passengersWaiting = 4;

    The above will error with the private label (this variable exists in floor.java), works if I make the label "protected" but that is not what I want.

    I actually put it into a for/loop:


    floors = new Floor[8];
    for (int i = 0; i < 8; i++) {
    floors[i].passengersWaiting = 7;
    }

    Again the issue is this only works if I make passengerwWaiting in Floor.java protected, since the above For/Loop and array of objects exists in Elevator.java.

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    You need to add a new public method to the Floor class that will set the value of the variable. This is a VERY COMMON concept usually discussed under the title of getters and setters. Google for this and read up.

    Also look at the java classes. Many of them have pairs of methods that get/set the contents of an object. For example look at the Component class. It has getSize() and setSize() and many others.
    Last edited by Norm; 09-28-2011 at 09:44 PM.

  18. #38
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    I am not sure (I know I am a rookie, so I hate to challenge you), but what I want to do is the following. I want to be able to set the protected int passengersWaiting; to private int passengersWaiting, and still have the statement floors[i].passengersWaiting = i+1; to work....Any suggestions on how to do this?

    A portion of the Program Elevator.java
    public class Elevator
    {
    public Elevator () {
    floors = new Floor[8];
    for (int i = 0; i < 8; i++) {
    floors[i] = new Floor(i+1);
    floors[i].passengersWaiting = i+1;
    }
    }
    }

    A portion of the Floor.java program:

    public class Floor {

    public Floor (int floorNumber) {
    passengersWaiting = 0;
    }

    protected int passengersWaiting;
    }

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    Add a setTheVariable() method to the Floor's class that changes the value of the variable. Call that method with the value that you want to variable to be set to.

  20. #40
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Thank you for the above information. I was able to finish my project 2 for my class. The next assignment involves replacing our array of objects with a collection, so I need to store 3 different types of passenger objects (passengers going up, and passengers going down, plus passengers just hanging out on the floor) in separate collections, and these passengers obviously exist on the Floor objects. I want to take this slowly, and say swap out my array of passengers waiting on the floor, which I used to reference via floor[i]passengersWaiting, with a collection, I have done some reading on collections but their are so many, not sure which fits the best.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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