Results 1 to 7 of 7
- 11-17-2010, 02:23 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Using the return statement to display a string
I was assigned this program for homework, and everything works fine, except where I try to use return to display the words " is stopping." I have no idea what I'm doing wrong. Here's the code, and some background on the purpose of the assignment below:
Java Code:public class Elevators { public static void main (String[] args) { ElevatorController eastelevator = new ElevatorController(); ElevatorController westelevator = new ElevatorController(); ElevatorController northelevator = new ElevatorController(); eastelevator.move_up(); westelevator.move_down(); ElevatorController.stop_elevator(eastelevator); northelevator.move_up(); ElevatorController.stop_elevator(westelevator); ElevatorController.stop_elevator(northelevator); } } class ElevatorController { public static String stop_elevator(ElevatorController elevator_name) { elevator_name.report_elevator_name(); String stopper = " is stopping!"; return stopper; } public void move_up() { System.out.println(this + " moving up!"); } public void move_down() { System.out.println(this + " moving down!"); } private void report_elevator_name() { System.out.println(this); } }
I'm incredibly new to both programming and java, so I'm sure this question seems ridiculous, but I'm stuck!
My homework assignment was to make a program which displayed this text:
ElevatorController@72e3b895 moving up!
ElevatorController@446b7920 moving down!
ElevatorController@72e3b895 is stopping!
ElevatorController@6bdd46f7 moving up!
ElevatorController@446b7920 is stopping!
ElevatorController@6bdd46f7 is stopping!
The professor provided the method signatures he wanted us to use, and when I finished the code everything displayed properly except the parts that say " is stopping!" I've been stuck trying to figure out why forever.
Thanks so much for your help!
- 11-17-2010, 02:39 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Stop_elevator is a static class method and you are using it as such as well. As static class method can be invoked without needing an instance of that class. But that also means you cannot use attributes or non static methods of that class.
Where this all comes down to is: ElevatorController.stop_elevator(eastelevator) will not work.
Why did you declare stop_elevator as static and why is the inner working of this method completely different from the other methods?
Cheers,
ErikLast edited by venerik; 11-17-2010 at 02:39 AM. Reason: typo
I'm new to Java but I like to help where ever I can. :)
- 11-17-2010, 02:45 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Thanks for responding! My professor instructed us to declare it as static, and to make it different from the others. He wrote in the assignment:
In the class ElevatorController, provide the following methods with the following signatures:
public static String stop_elevator(ElevatorController elevator_name)
public void move_up()
pulic void move_down()
priavte void report_elevator_name() // the reason this method is private will be discussed at a later date
Then he wrote:
# stop_elevator(). The static method stop_elevator() has the name of an ElevatorController object as its argument. That name is elevator_name and it will (temporarily) hold the same reference as the ElevatorController object we "send" to the method each time we invoke it. Within the method, you must invoke the report_elevator_name() method. Because the method is Static you cannot use this. So, you would use the the syntax elevator_name.report_elevator_name(); then, on the next line of your program, your method will conclude with the statement: return " is stopping!";
Right now my output looks like this:
ElevatorController@72e3b895 moving up!
ElevatorController@446b7920 moving down!
ElevatorController@72e3b895
ElevatorController@6bdd46f7 moving up!
ElevatorController@446b7920
ElevatorController@6bdd46f7
- 11-17-2010, 03:04 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Try to add String that will hold the returned value of stop_elevator(ElevatorController elevator_name)
Java Code:String east_stop = ElevatorController.stop_elevator(eastelevator); System.out.println(east_stop);
- 11-17-2010, 03:46 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Thanks so much, that worked really well!
Is it possible to make " is stopping" display on the same line as the reference to the elevator? I keep trying to find ways to make two pieces of output display on one line, but all I find is how to concatenate two strings. I missed a couple classes and it's a compressed half-semester course, so I'm struggling hard to catch up! I
- 11-17-2010, 04:40 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
- 11-17-2010, 09:22 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Changing from println() to print() in the report_elevator_name method will cure that problem.
You can also then call report_elevator_name() within the other methods, to maintain consistency, rather than concatenating strings together.
Also, I would point out to your tutor that their naming convention is non-standard, which is not helpful. That should be reportElevatorName() (and similar with the other methods).
Similar Threads
-
If Statement return
By ricardo5222 in forum New To JavaReplies: 4Last Post: 11-01-2010, 11:43 AM -
missing return statement
By bayan in forum New To JavaReplies: 6Last Post: 04-26-2010, 03:15 PM -
Return Statement and Boolean Help
By GhostShaman in forum New To JavaReplies: 8Last Post: 03-09-2010, 11:15 AM -
problem while using return statement
By shaluchandran in forum New To JavaReplies: 10Last Post: 12-12-2008, 06:29 PM -
there is no return statement
By gabriel in forum New To JavaReplies: 17Last Post: 12-03-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks