The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Cannot print boolean value from class!

This is my assignmentWrite a class named Fan to model fans. The properties are speed, on, radius, and color. You need to provide the accessor methods for the properties, and the toString method for returning a string consisting of all the string values of all the properties in this class. Suppose the fan has three fixed speeds. Use constants 1, 2, and 3 to denote slow, medium, and fast speeds.

and my professor gave me an outline of the class and what I have to do is Write a client program(test application) to test the Fanclass. In the client program, create a Fan object.
Assign maximum speed, radius 10, color yellow, and turn it on.
Display the object by invoking its toString method.

I have gone till the end, created Fan class without any errors and also a client program to test the class but I cannot display whether the fan is ON/OFF. I simply blind coded the On class here. SO In my test class, I cannot print whether the fan is on or off, it simply shows me an error.

SO as u see all the code work except testfan1.isOn, error says (req: boolean found: no args), even if I give testfan.isOn(false); no error shows up but it shows up 'false' in the output but I want it to display "Fan is turned on"

Re: Cannot print boolean value from class!

Originally Posted by adnan.alvee

SO as u see all the code work except testfan1.isOn, error says (req: boolean found: no args), even if I give testfan.isOn(false); no error shows up but it shows up 'false' in the output but I want it to display "Fan is turned on"

you create the public boolean isOn ( boolean newOn ) method with parameter.

So you must call this method with parameter like this..

testfan1.isOn(false);

And this method only print the boolean variable because you return only the boolean variable on this method like this..

public boolean isOn ( boolean newOn ) {
return newOn;
}

Then you want to print this statement "fan is on or off" into your ouptut. But you create this statement in setOn method like this..

Re: Cannot print boolean value from class!

how do I do it with toString?

The toString() method returns a String. See the API doc for the Object class.
Classes should override the toString() method and have it return a String that describes the contents to the class.
When you print a reference to a class, the toString() method is called to get the String that describes the class.