Page 1 of 3 123 LastLast
Results 1 to 20 of 55
Like Tree1Likes

Thread: What do I get?

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

    Default What do I get?

    To the forum,

    In an exercise that I am working on, I am asked to pass an object from the originating class to a destination class, manipulate the data in the " originating class" and then return the results. I am able to pass the object to the destination class, but I am unable to manipulate the variables of the originating class, because they are private, which is a requirement.

    My question is, is this possible to manipulate the variables in the originating class (they need to remain private)?

    If I cannot manipulate the variables, is their a way that I can make a calculation on the destination class and somehow pass it back to an originating class method?

    Can someone provide a simple example of an originating class passing off control to a destination class and then passing information back?

    thanks

    Steve

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

    Default Re: What do I get?

    I suspect this is far to vague to answer in a meaningful way.
    If you need access to the data in the originating class then I suppose you could always give it some getters?

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

    Default Re: What do I get?

    Thank you for your response. I have another question: What does the below statement in java do? And how dow i populate it?

    Floor floors[] = new Floor[MAX_FLOORS];

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

    Default Re: What do I get?

    Have you read the Java Tutorial about arrays? Go to this site and Find Array
    Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: What do I get?

    Quote Originally Posted by smartino View Post
    Can someone provide a simple example of an originating class passing off control to a destination class and then passing information back?
    I don't know if I understood you correctly, but maybe you want this: assume an Originator object can have it's int value set and it can create a Messenger object:


    Java Code:
    public class Originator {
       private int value;
       public void setValue(int value) { this.value= value; }
       public Messenger getMessenger() { return new Messenger(this); }
    }
    Here is the class messenger that can set a value in an Originator object:

    Java Code:
    public class Messenger {
       private Originator originator;
       public Messenger(Originator originator) { this.originator= originator; }
       public void setValue(int value) { originator.setValue(value); }
    }
    A class Destination can set a value in an Orignator object as follows:

    Java Code:
    public class Destination { 
       private Messenger messenger; 
       public Destination(Messenger messenger) { this.messenger= messenger; }
       public void passValue(int value) { messenger.setValue(value); }
    }
    Note that the destination class doesn't know anything about the Orinator object; it is all handled by the Messenger object. Does this clarify things a bit?

    kind regards,

    Jos
    Last edited by JosAH; 09-26-2011 at 09:43 PM.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: What do I get?

    I did, but did not see any reference to this type of statement, can you provide further assistance?

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

    Default Re: What do I get?

    reference to this type of statement,
    Can you explain and show an example.

    Floor floors[] = new Floor[MAX_FLOORS];
    Is this the "type of statement"?
    It defines an array to hold MAX_FLOORS number of Floor objects. I assume that MAX_FLOORS is defined as an int variable.
    Last edited by Norm; 09-22-2011 at 06:10 PM.

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

    Default Re: What do I get?

    You mean you read this page and couldn't see how it applied to the line of code you asked about?

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: What do I get?

    In the mean time I'm sitting quitely in the corner, wondering what those silly arrays have to do with the original problem ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: What do I get?

    I apologize! I am mixing issues....Let me start over.

    thank you for the response to my original question below:

    To the forum,

    In an exercise that I am working on, I am asked to pass an object from the originating class to a destination class, manipulate the data in the " originating class" and then return the results. I am able to pass the object to the destination class, but I am unable to manipulate the variables of the originating class, because they are private, which is a requirement.

    My question is, is this possible to manipulate the variables in the originating class (they need to remain private)?

    If I cannot manipulate the variables, is their a way that I can make a calculation on the destination class and somehow pass it back to an originating class method?

    Can someone provide a simple example of an originating class passing off control to a destination class and then passing information back?

    thanks

    Steve

    I was able to solve this as a result of the responses received and talking with others in my class, again Thank You!

    I posted an totally unrelated question, and I apologize...if you want me to post this question under a new thread I am more than happy to.

    Thank you for your response. I have another question: What does the below statement in java do? And how dow i populate it?

    Floor floors[] = new Floor[MAX_FLOORS];

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

    Default Re: What do I get?

    What does the below statement in java do? And how dow i populate it?
    Have you read the previous posts? See post#7 for example
    Did you read the Java tutorial about arrays? It has examples of how you populate an array.

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

    Default Re: What do I get?

    Yes, I have read the tutorials, but do not see a reference to an array of objects. and I have read post number 7. My question is is once the array of objects is defined, how do I populate it and then reference it in later code?

    Thank you.

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

    Default Re: What do I get?

    There is no difference in the technique used to populate an array that changes with what you are putting in the array. The array name and index on the left and the value on the right.
    Java Code:
    theArray[ix] = <a value to go in the array>;
    reference it in later code?
    Again there is no differences. Put the definition of the array outside of any methods, at the class level and any of the methods in the class will be able to reference it.

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

    Default Re: What do I get?

    Sorry to comment, but this is HILARIOUS! Are these the programmers of the future? OMG This is not a Java question ... this is more basic stuff!
    Sorry but I couldn't help to say this ...

    Nevertheless, and punishing myself for the previous comment, I'll say:
    ...
    // we have an array of Floor objs ... with MAX_FLOORS, and we assume MAX_FLOORS=10
    Floor floors[] = new Floor[MAX_FLOORS];

    //... then, to populate the array, we must create (or obtain from some source) a Floor. Lets assume:
    final Floor floor1 = new Floor(/*assuming the constructor has no params*/);

    // ... then if we want the floor1 to be added to the index 1 (remember the index of the first element????)
    floors[1] = .... // for you to complete ...

    // and so on ... and so on ...
    ...

    RGV

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

    Default Re: What do I get?

    I feel it should be noted that I don't claim to be the programmer of the future..this why I posted this in the forum "New to Java" I don't dare post any follow up questions.

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

    Default Re: What do I get?

    Don't pay any attention to ruivale. That was his first post here. I have no idea what he expects beginning students to know.
    Fubarable likes this.

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

    Default Re: What do I get?

    Ok, thanks, I apologize, but I know how to setup arrays and set their values, I am just getting hung up on an array of type object.

    The error I get is this when I try and compile...

    cscie160/backup/Elevator.java:29: non-static variable floors cannot be referenced from a static context
    floors[1].passengersWaiting = 6;


    What I am trying to do is to create an array of Floor objects each with its own passengersWaiting value that I can reference in other methods as the currentFloor changes, so in reality, I want to ultimately be able to print the value of floors[1].passengersWaiting and substitute in currentFloor for the [1]

    thanks

    Steve

    Here are the two java programs (Elevator.java and Floor.java)

    ********************1st Program: Elevator.java *********************
    Java Code:
    // File Elevator.java
    /** The Elevator class produces a simulation of a working elevator by loading passengers destined for floors and discharging them upon reaching the destined floor.
    
    package cscie160.backup;
    
       public class Elevator
       {
    
       public Elevator () {
    
            currentFloor = 0;
    
            }
    
            public String toString() {
              return "Currently " + floors[currentFloor] + " waiting!";
            }
    
          public static void main(String argv[])
          {
              final Floor floor1  = new Floor();
    
    /**The goal is to set for each floor the number of passengers waiting on the floor using the passengersWaiting variable in Floor.java, the floors[1] = statement below does not compile without errors, what am I doing wrong?? */
    
              floors[1].passengersWaiting = 6;  
              System.out.println(floor1);
    
          }
    
            protected final int MAX_FLOORS = 2;
            Floor floors[] = new Floor[MAX_FLOORS];
            int currentFloor;
    
       }
    
    
    ********************2nd Program:  Floor.java *********************
    
    /**
     *
     */
    
    package cscie160.backup;
    
    /**
     * @author spm3
     *
     */
    
    public class Floor {
    
            public Floor () {
              passengersWaiting = 0;
            }
    
    public String toString() {
    
           return "Passengers waiting on Floor " + currentFloor +  "  = " + passengersWaiting;
    
           }
    
    public static void main(String[] args) {
    
    }
            protected int passengersWaiting;
            protected int currentFloor;
    }
    Last edited by Norm; 09-27-2011 at 02:29 AM. Reason: added code tags

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

    Default Re: What do I get?

    non-static variable floors cannot be referenced from a static context
    This error has nothing to do with arrays. Your code in a static method is trying to reference a variable that will only exist when there is an instance of the class(non-static).
    You need to move the code that initializes the class variables like floors[] to the class's constructor.
    The main method should call the class's constructor where the initialization will be done.
    You call a constructor by using the new <CLASSNAME>(...) statement.

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

    Default Re: What do I get?

    Ok, thank you, I did this, see Elevator.java below, but I get the following now:

    Exception in thread "main" java.lang.NullPointerException
    at cscie160.backup.Elevator.<init>(Elevator.java:10)
    at cscie160.backup.Elevator.main(Elevator.java:21)

    **************Elevator.java *****************


    // File Elevator.java

    package cscie160.backup;

    public class Elevator
    {

    public Elevator () {

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

    }

    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];
    int currentFloor;

    }

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

    Default Re: What do I get?

    java.lang.NullPointerException
    at cscie160.backup.Elevator.<init>(Elevator.java:10)
    At line 10 in the Elevator.java file there was a variable with a null value.

    Do the slots in the floors array have any values? You have defined the array. That means you have MAX_FLOORS slots in which you can put Floor objects. You need to create Floor objects and put them into the array.

Page 1 of 3 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
  •