i am trying to code a toString() method which returns the string "Power: ON" if the power variable is equal to true and "Power: Off" if the power variable is equal to false (the power variable is the boolean type). However i dont know how to do this as the only example i found just prints the information stored in the variable (e.g. if variable x equals10 it prints "X=10"). any ideas of how i should do this?

Recommended Answers

All 6 Replies

Not supposed to give away code, as this doesn't really help you learn, but any explanation I give you will be much more complicated than the code itself is, and might confuse you, so here is how you do it.

public String toString(){
String s = "";
if (power==true){
s = "Power: ON";
} else{
s = "Power: OFF";
}
return s;
}

All that code does is has an if statement that decides if power was true, and if so, sets a String to say "Power = on". Otherwise, the else statement gets executed and it says "Power = off". Also, please consider that the toString() method is supposed to convey the meaning of whatever Object you have. So the toString() method of a Car class might tell you what type of Car it is (Toyota, Honda, etc). So I'd recommend only override the toString method (what you are doing is called overriding, since the Object class already defines a toString method) if what you're doing actually describes the Object.

Well, I think that:

@Override public String toString()
{
      if( power )
             return "Power: On";
      else
             return "Power: Off";
}

EDIT: Oh, I'm too late. :)

Thanks for that code.

Im still having a problem with a set of constructors im using heres the class with the constructors:

import javax.swing.*;

class Lamp extends Appliance
{
	Lamp()
	{
		power=false;
		ImageIcon picture = new ImageIcon("lamp.jpg");
	}
	
	Lamp(boolean po)
	{
		ImageIcon picture = new ImageIcon("lamp.jpg");
	}		
	
	ImageIcon getPicture()
	{
		return picture;
	}
}

This class is to be used when i run the following code:

public class TestLamp
{
    public static void main(String[] args) 
    {
        // Test no-args constructor
		Lamp deskLamp = new Lamp();

		// Test alternative constructor 
		Lamp floorLamp = new Lamp(true);
		
		// Test the inherited getPower() method
		System.out.println("Desk lamp");
		System.out.println(deskLamp.getPower());
	
		// Test the toString() method, which is called automatically by the following code
		System.out.println(deskLamp);
		System.out.println();

		// Test the inherited getPower() method
		System.out.println("Floor lamp");
		System.out.println(floorLamp.getPower());
	
		// Test the toString() method, which is called automatically by the following code
		System.out.println(floorLamp);
    }
}

When i run this code i should get the following output:

Expected output:
> Desk Lamp
> false
> Power: OFF
>
> Floor lamp
> true
> Power: ON

However when i do run the code floor lamp's power is off. I assume there is something wrong with my lamp classes second constructor. can someone tell me the problem

NOTE: ive tried having "po=power" in the constructor. the power and picture variables are in the super class called appliance.

It should say power = po; in the Lamp(boolean) constructor. That sets the power to whatever value (true or false) that was passed into the constructor. Consider this example:

public class ExampleClass{
boolean something = false;

public ExampleClass(boolean value){
     something = value;
}

public static void main(String[] args){
     new ExampleClass(true);
}

}

^ The example above will work, because what's on the left side of the "=" sign gets it's value set to whatever is on the right of the "=" sign. So if the constructor was

public ExampleClass(boolean value){
     value = something;
}

It would just set value (the parameter of the constructor), to whatever something was. So since something = false (where it says boolean something = false), we'd be setting value to false, which is not what we want to do.

It should say power = po; in the Lamp(boolean) constructor. That sets the power to whatever value (true or false) that was passed into the constructor.

thanks for that, it must be cause i put po=power originally instead of the other way round

Yes, it is. While you were posting, I was editing with a code example so that you understand how it works, so go check it out. & If you don't get it, don't hesitate to ask any more questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.