Results 1 to 3 of 3
  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Bulb class need help

    This is a simple program that displays if the bulb is on or not.

    PHP Code:
    package test;
    
    public class Bulb {
    	
    	private boolean on;
    	
    	
    	//-----------------------------------------------------------------
    	//  Sets up this Bulb object with the light off.
    	//-----------------------------------------------------------------
    	public Bulb()
    	{
    		on = false;
    	}
    	
    	//-----------------------------------------------------------------
    	//  Returns true if this bulb is currently on.
        //-----------------------------------------------------------------
    	public boolean isOn()
    	{
    		return on;
    	}
    	
    	//-----------------------------------------------------------------
    	//  "on" status setter of the bulb
    	//-----------------------------------------------------------------
    	public void setOn(boolean status)
    	{
    		on = status;
    	}
    	
    	//-----------------------------------------------------------------
            //  Returns a string representation of this bulb.
            //-----------------------------------------------------------------
    	public boolean getOn()                                                           
    	{
    		return (?????);    // <<--- How do i set the getter to check if it is on or off ?? what do i write after return ___ ???
    	}
    	
    	 //-----------------------------------------------------------------
    	 //  Returns a string representation of this bulb.
    	 //-----------------------------------------------------------------
    	public String toString()
    	{
    		return ("Bulb on: " + on);
    	}
    
    }
    
    
    // MAIN CLASS
    
    package test;
    
    public class Bulbtest {
    
    	public static void main(String[] args) {
    		
    		Bulb bulb1 = new Bulb();
    		
    		System.out.println(bulb1);
    		
    		bulb1.setOn(true);
    		
    		bulb1.getOn();
    		System.out.println(bulb1);
    
    	}
    
    }

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Bulb class need help

    Uhhhhhh I'm not sure what string representation you actually want but you have two options.

    Either:

    Java Code:
    public String getOn() {
         if(on) {
                return "whatever you want for on goes here";
         } else {
                return "whatever you want for off going here";
         }
    }
    If you want to do this in one line, you can use
    Java Code:
    return (on ? "whatever you want for on" : "whatever you want for off");
    This does the exact same thing, in the format of (boolean ? what to do if true : what to do if false)




    EDIT:
    Looks like I misread your method. You want it to return a boolean. Just return on...
    Last edited by AlexGraal; 05-02-2014 at 02:14 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Bulb class need help

    You already have an isOn method. Why do you need a getOn method? They both return the same thing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2013, 10:28 PM
  2. Replies: 1
    Last Post: 12-13-2012, 04:06 AM
  3. Replies: 1
    Last Post: 11-29-2012, 10:46 AM
  4. Replies: 0
    Last Post: 03-27-2011, 06:49 AM

Posting Permissions

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