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?
Laidler 0 Junior Poster in Training
Recommended Answers
Jump to PostNot 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){
…
Jump to PostIt 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[] …
All 6 Replies
BestJewSinceJC 700 Posting Maven
Zibo 2 Light Poster
Laidler 0 Junior Poster in Training
BestJewSinceJC 700 Posting Maven
Laidler 0 Junior Poster in Training
BestJewSinceJC 700 Posting Maven
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.