Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Cannot print boolean value from class!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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

     
    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"


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Cannot print boolean value from class!

    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot print boolean value from class!

    Quote Originally Posted by Norm View Post
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot print boolean value from class!

    Some one help please!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Cannot print boolean value from class!

    Did you understand what was said in post#4?

    Did you try either of the suggested solutions?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot print boolean value from class!

    Quote Originally Posted by Norm View Post
    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?

  8. #8
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Cannot print boolean value from class!

    Quote Originally Posted by adnan.alvee View Post
    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..

     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
     if (on = false)

    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
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: December 3rd, 2012, 01:35 PM
  2. How do I print item facilities in another class?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 22nd, 2012, 09:43 AM
  3. Replies: 2
    Last Post: November 15th, 2012, 01:58 PM
  4. [SOLVED] Boolean return print
    By Gerardgrundy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 4th, 2012, 04:38 AM
  5. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 12:32 PM