Cannot print boolean value from class!
This is my assignment
Write 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.
My Fan class is here
Code :
public class Fan {
public final int SLOW = 1;
public final int MEDIUM = 2;
public final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "white";
public Fan ( ) { }
public Fan ( int newSpeed, boolean newOn, double newRadius, String newColor ) {
setSpeed(newSpeed);
setRadius(newRadius);
setColor(newColor);
}
public int getSpeed ( ) {
return speed;
}
public void setSpeed ( int speed ) {
if (speed == SLOW) {
System.out.println("Speed is 1");
} else if (speed == MEDIUM) {
System.out.println("Speed is 2");
}
else if (speed == FAST) {
System.out.println("Speed is 3(Maximum Speed)");
}
else {
System.out.println("Invalid Speed");
}
}
public boolean isOn ( boolean newOn ) {
return newOn;
}
public void setOn ( boolean newOn ) {
if (on = false) {
System.out.println("Fan is Turned on");
} else {
System.out.println("Fan is turned off");
}
}
public double getRadius ( ) {
return radius;
}
public void setRadius ( double newRadius ) {
if (newRadius <= 0) {
System.out.println("Radius cannot be zero or negative");
} else {
radius = newRadius;
}
}
public String getColor ( ) {
return color;
}
public void setColor ( String newColor ) {
color = newColor;
}
@Override
public String toString ( ) {
return "Dimensions are" + radius;
}
}
My Code for testing the class
Code :
public class TestApplication {
public static void main (String[] args) {
Fan testfan1 = new Fan(3, false, 10, "blue");
System.out.println(testfan1.getSpeed() + " " + testfan1.getColor() + " "
+ testfan1.getRadius() + " " + [COLOR="#FF0000"]testfan1.isOn()[/COLOR]);
}
}
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!
Please copy the full text of the error messages and paste it here.
Re: Cannot print boolean value from class!
Quote:
Originally Posted by
Norm
Please copy the full text of the error messages and paste it here.
Here is the full error message in Bold
Public method isOn cannot be applied to given types;
required: boolean
found: no arguements
reason: actual and formal arguement lists differ in length
Re: Cannot print boolean value from class!
The compiler found a definition of the isOn() method that takes a boolean as argument.
The code tried to call the method with no arguments.
Two ways to solve this:
1) add a boolean arg to the call of the method: isOn(true)
2) remove the boolean arg from the methods definition: boolean isOn() {
I'd recommend the second choice. I don't see what use passing the boolean is to the method.
Re: Cannot print boolean value from class!
Some one help please!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Re: Cannot print boolean value from class!
Did you understand what was said in post#4?
Did you try either of the suggested solutions?
Re: Cannot print boolean value from class!
Quote:
Originally Posted by
Norm
The compiler found a definition of the isOn() method that takes a boolean as argument.
The code tried to call the method with no arguments.
Two ways to solve this:
1) add a boolean arg to the call of the method: isOn(true)
2) remove the boolean arg from the methods definition: boolean isOn() {
I'd recommend the second choice. I don't see what use passing the boolean is to the method.
Norm my professor says to
Display the object by invoking its toString method.
How do I do that? I just displayed it using print statement but how do I do it with toString?
Re: Cannot print boolean value from class!
Quote:
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..
And this method only print the boolean variable because you return only the boolean variable on this method like this..
Code :
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..
Code :
public void setOn ( boolean newOn ) {
if (on == false) {
System.out.println("Fan is Turned on");
} else {
System.out.println("Fan is turned off");
}
}
so you call this setOn method in your print statement on your main method or in your isOn(arg) method
I hope you get your expected output...
then also you made another mistake in your if condition like
that is not use single = symbol for check the condition use == like..
[code] if (on == false) [code]
I hope this will be help to you
Re: Cannot print boolean value from class!
Quote:
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.