Results 1 to 20 of 55
Thread: What do I get?
- 09-21-2011, 02:35 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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
- 09-21-2011, 03:15 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 09-22-2011, 05:05 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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];
- 09-22-2011, 05:17 PM #4
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)
- 09-22-2011, 05:39 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: What do I get?
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:
Here is the class messenger that can set a value in an Originator object:Java Code:public class Originator { private int value; public void setValue(int value) { this.value= value; } public Messenger getMessenger() { return new Messenger(this); } }
A class Destination can set a value in an Orignator object as follows:Java Code:public class Messenger { private Originator originator; public Messenger(Originator originator) { this.originator= originator; } public void setValue(int value) { originator.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?Java Code:public class Destination { private Messenger messenger; public Destination(Messenger messenger) { this.messenger= messenger; } public void passValue(int value) { messenger.setValue(value); } }
kind regards,
JosLast edited by JosAH; 09-26-2011 at 09:43 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-22-2011, 06:03 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
Re: What do I get?
I did, but did not see any reference to this type of statement, can you provide further assistance?
- 09-22-2011, 06:08 PM #7
Re: What do I get?
Can you explain and show an example.reference to this type of statement,
Is this the "type of statement"?Floor floors[] = new Floor[MAX_FLOORS];
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.
- 09-22-2011, 06:08 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 09-22-2011, 06:14 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-22-2011, 06:26 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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];
- 09-22-2011, 06:28 PM #11
Re: What do I get?
Have you read the previous posts? See post#7 for exampleWhat does the below statement in java do? And how dow i populate it?
Did you read the Java tutorial about arrays? It has examples of how you populate an array.
- 09-26-2011, 07:42 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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.
- 09-26-2011, 07:48 PM #13
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>;
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.reference it in later code?
- 09-26-2011, 08:00 PM #14
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
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
- 09-26-2011, 10:58 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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.
- 09-26-2011, 11:03 PM #16
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.
- 09-27-2011, 02:22 AM #17
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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
- 09-27-2011, 02:32 AM #18
Re: What do I get?
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).non-static variable floors cannot be referenced from a static context
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.
- 09-27-2011, 02:48 AM #19
Member
- Join Date
- Sep 2011
- Posts
- 64
- Rep Power
- 0
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;
}
- 09-27-2011, 03:14 AM #20
Re: What do I get?
At line 10 in the Elevator.java file there was a variable with a null value.java.lang.NullPointerException
at cscie160.backup.Elevator.<init>(Elevator.java:10)
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.


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks