Results 1 to 7 of 7
  1. #1
    Hoverboy is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default 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!

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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,
    Erik
    Last 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. :)

  3. #3
    Hoverboy is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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);

  5. #5
    Hoverboy is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by Hoverboy View Post
    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
    Your welcome.

    I am not sure but I think there is no way to print them since you printed it already when you call report_elevator_name(). Unless report_elevator_name() will return a String just like stop_elevator(...). Also its is the instruction right.

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

    Default

    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

  1. If Statement return
    By ricardo5222 in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 11:43 AM
  2. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM
  3. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM
  4. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM
  5. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04: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
  •