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 8 of 8

Thread: Design of class named Fan to represent a Fan

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Design of class named Fan to represent a Fan

    Hello programmers ^^
    i 'am new here... so..
    i got assignment from my lecture.
    this is the first assignment.

    before this my lecture teach me about how to make a cake using constructor. but i can't catch up what she said in class, so she give this assignment and i don't know how to write it.
    now my class using NetBeans IDE 6.5.

    Design a class named Fan to represent a fan. The class contains:
    1. Three constants named SLOW, MEDIUM and FAST with value 1, 2, and 3 to denote the fan speed.
    2. An int data field named speed that specifies the speed of the fan(default SLOW).
    3. A boolean data field named on that specifies whether the fan is on(default false).
    4. A double data field named radius that specifies the radius of the fan(default 5).
    5. A string data field named color that specifies the color of the fan (default blue).
    6. A no-argument constructor that creates a default fan.
    7. The accessor and mutator methods for all four data field.
    8. A method named toString() that returns a string description for the fan. if the fan is on, the method returns the fan speed, color and radius in one combined string. if the fan is not on, the method returns fan color and radius along with the string "fan is off" in one combine string.


    this is my code that im doing this, if got any wrong pls tell me.
    package lab1;
     
    public class FanTest {
    public static void main(String args[]){
    Fan fan_is_on=new Fan(1,false,5,"blue");
    fan_is_on.seton$off(true);
    fan_is_on.setcolor("blue");
    fan_is_on.setspeed(3);
     
    System.out.println("Fan is on: ");
    System.out.println("Speed: "+fan_is_on.getspeed());
    System.out.println("switch on: "+fan_is_on.ison$off());
    System.out.println("radius: "+fan_is_on.getradius());
    System.out.println("color: "+fan_is_on.getcolor());
     
     }
    }
     
    class Fan{
        int speed=1;
        boolean on$off=false;
        double radius=5;
        String color="blue";
     
       Fan(int speed, boolean on$off, double radius, String color){
        this.speed=speed;
        this.on$off=on$off;
        this.radius=radius;
        this.color=color;
        }
       Fan(){}
     
        void setspeed(int s){ speed=s; }
        void seton$off(boolean open){ on$off=open;}
        void setradius(double r){ radius=r; }
        void setcolor(String c){ color=c; }
     
     
        int getspeed(){ return speed; }
        boolean ison$off(){ return on$off; }
        double getradius(){ return radius; }
        String getcolor(){ return color; }
     
    }

    Last edited by Deep_4; November 8th, 2012 at 12:18 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help me this please..

    Hello qaromi. Welcome to the Java Programming Forums.

    This looks pretty good to me... Don't forget number 8.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me this please..

    anyway i duno the last 1. number 8... how to combined?
    toString()? is it new method? can show me?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help me this please..

    Hmm..

    It could possible be something like this:

    package JavaProgrammingForums;
     
    public class FanTest {
     
        public static void main(String args[]) {
     
            Fan fan = new Fan(1, false, 5, "blue");
            fan.seton$off(true);
            fan.setcolor("blue");
            fan.setspeed(3);
     
            FanTest ft = new FanTest();
     
            System.out.println(ft.toString());
     
        }
     
        public String toString(){
     
            Fan fan = new Fan();
            String printme = null;
     
            if(fan.on$off = true){
                printme = ("Speed: " + fan.getspeed() + " Color: " + fan.color + " Radius: " + fan.radius);    
            }
            if(fan.on$off = false){
                printme = ("Color: " + fan.color + " Radius: " + fan.radius + " Fan is not on");        
            }
     
            return(printme);
     
        }    
    }
     
    class Fan {
        int speed = 1;
        boolean on$off = false;
        double radius = 5;
        String color = "blue";
     
        Fan(int speed, boolean on$off, double radius, String color) {
            this.speed = speed;
            this.on$off = on$off;
            this.radius = radius;
            this.color = color;
        }
     
        Fan() {
        }
     
        void setspeed(int s) {
            speed = s;
        }
     
        void seton$off(boolean open) {
            on$off = open;
        }
     
        void setradius(double r) {
            radius = r;
        }
     
        void setcolor(String c) {
            color = c;
        }
     
        int getspeed() {
            return speed;
        }
     
        boolean ison$off() {
            return on$off;
        }
     
        double getradius() {
            return radius;
        }
     
        String getcolor() {
            return color;
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me this please..

    oh i see... u put toString() method at public class.. that method for if-else right?
    so u make new caller to call that method... am i rite?

    i think this 1 no need to write..
     Fan fan = new Fan(1, false, 5, "blue");
            fan.seton$off(true);
            fan.setcolor("blue");
            fan.setspeed(3);

    thx JavaPF.. ur really help me..
    thx alot

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help me this please..

    You can't have a method named toString() without it being a String type because the toString() method already exists within the String class. I hope that is right anyway..

    Yes you can call that method like this

    FanTest ft = new FanTest();
    System.out.println(ft.toString());

    Because it's returning a String we can use System.out.println(ft.toString());

    Number 6 says - A no-argument constructor that creates a default fan.

    I think it should be like this:

    public class FanTest {
     
        public static void main(String args[]) {
     
            Fan fan = new Fan();
            fan.seton$off(true);
            fan.setcolor("blue");
            fan.setspeed(3);
     
            FanTest ft = new FanTest();                
            System.out.println(ft.toString());
     
        }
     
        public String toString(){
     
            Fan fan = new Fan();
            String printme = null;
     
            if(fan.on$off = true){
                printme = ("Speed: " + fan.getspeed() + " Color: " + fan.color + " Radius: " + fan.radius);    
            }
            if(fan.on$off = false){
                printme = ("Color: " + fan.color + " Radius: " + fan.radius + " Fan is not on");        
            }
     
            return(printme);
     
        }    
    }
     
    class Fan {
        int speed = 1;
        boolean on$off = false;
        double radius = 5;
        String color = "blue";
     
        Fan() {
            this.speed = speed;
            this.on$off = on$off;
            this.radius = radius;
            this.color = color;
        }
     
        void setspeed(int s) {
            speed = s;
        }
     
        void seton$off(boolean open) {
            on$off = open;
        }
     
        void setradius(double r) {
            radius = r;
        }
     
        void setcolor(String c) {
            color = c;
        }
     
        int getspeed() {
            return speed;
        }
     
        boolean ison$off() {
            return on$off;
        }
     
        double getradius() {
            return radius;
        }
     
        String getcolor() {
            return color;
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. The Following User Says Thank You to JavaPF For This Useful Post:

    qaromi (June 23rd, 2009)

  8. #7
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me this please..

    aa... i put this in class same like u did... it said "Add @Override Annotation", is it something wrong?

    in this code, how to do the input the fan is on or off, like if i said on, they switch on, and i said off they switch off?
    orry for many question

     public String toString(){
     
            Fan fan = new Fan();
            String printfan = null;
     
            if(fan.on$off = true){
                printfan = ("Speed: " + fan.getspeed() + " Color: " + fan.color + " Radius: " + fan.radius);
            }
            if(fan.on$off = false){
                printfan = ("Color: " + fan.color + " Radius: " + fan.radius + " Fan is not on");
            }
            return(printfan);
        }

  9. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Help me this please..

    Your last code block there will always print the first line and never the second because you are actually setting your on$off value in the if. To actually check what the on$off value is you need to use double equals.

     public String toString(){
     
            Fan fan = new Fan();
            String printfan = null;
     
            if(fan.on$off == true){
                printfan = ("Speed: " + fan.getspeed() + " Color: " + fan.color + " Radius: " + fan.radius);
            }
            if(fan.on$off == false){
                printfan = ("Color: " + fan.color + " Radius: " + fan.radius + " Fan is not on");
            }
            return(printfan);
        }

    // Json