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

Thread: JAVA problem : Create a Fan Class

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA problem : Create a Fan Class

    Here are is assignment question: The Fan Class

    • Three static constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speeds.
    • A private int data filed named “speed” that specifies the speed of the fan, default to slow.
    • A private Boolean data field named “on” that specifies whether the fan is on, default to off.
    • A private double data field named “radius” that specifies the radius of the fan, default to 5.
    • A private string data field named “color” that specifies the color of the fan, default to “blue”.
    • A no-argument constructor that creates a default fan.
    • Accessors and Mutators for all 4 data fields.
    • A toString( ) method 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 off, the method returns the fan color and radius along with the string, “fan is off” in one combined string.

    The Fan Application class

    • Write a test program that creates two Fan objects. Sets all values in the mutators, and calls the toString( ) method.
    • Fan Object 1 inputs:
    o Assign Fastest Speed, by passing static constant FAST. Hint pass Fan.FAST to the mutator.
    o Assign a radius 10
    o Assign the color yellow
    o Turn the fan on.
    • Fan Object 2 inputs:
    o Assign Medium Speed, by passing static constant MEDIUM. Hint pass Fan.MEDIUM to the mutator.
    o Assign a radius 5
    o Assign the color blue
    o Turn the fan off.

    Output:
    The Fan Speed is 3, color is yellow, and radius is 10.
    The Fan color is blue, radius is 5, and the fan is off.


    and here is my code thus far:
    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 off!!");
            }
     
            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;
        }
     
    }


    I am confused about creating 2 different objects to satisfy these instructions. help please!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: JAVA problem : Create a Fan Class

    • Three static constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speeds.
    Not done.
    • A private int data filed named “speed” that specifies the speed of the fan, default to slow.
    Not done.
    • A private Boolean data field named “on” that specifies whether the fan is on, default to off.
    Not done.
    • A private double data field named “radius” that specifies the radius of the fan, default to 5.
    Not done.
    • A private string data field named “color” that specifies the color of the fan, default to “blue”.
    Not done.
    • A no-argument constructor that creates a default fan.
    Done but all the statements in the constructor are pointless.
    • Accessors and Mutators for all 4 data fields.
    Done.
    • A toString( ) method 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 off, the method returns the fan color and radius along with the string, “fan is off” in one combined string.
    Not done.

    When you are given explicit instructions in an assignment it helps if you follow them.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA problem : Create a Fan Class

    Okay here is my updated code thus far: I have corrected the following issues,

    • Three static constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speeds.
    DONE
    • A private int data filed named “speed” that specifies the speed of the fan, default to slow.
    DONE
    • A private Boolean data field named “on” that specifies whether the fan is on, default to off.
    DONE
    • A private double data field named “radius” that specifies the radius of the fan, default to 5.
    DONE
    • A private string data field named “color” that specifies the color of the fan, default to “blue”.
    DONE
    public class FanTest {
     
        public static void main(String args[]) {
     
            Fan fan = new Fan();
            fan.seton$off(true);
            fan.setcolor("yellow");
            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 off!!");
            }
     
            return(printme);
     
        }
    }
     
    class Fan1 {
        private int speed = 1;
        private boolean on$off = false;
        private double radius = 5;
        private String color = "Blue";
     
         	public static final int SLOW = 1;
         	public static final int MEDIUM = 2;
    	    public static final int FAST = 3;
     
     
        Fan1() {
            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;
        }
     
    class Fan2 {
        int speed = 1;
        boolean on$off = false;
        double radius = 5;
        String color = "Blue";
     
        Fan2() {
            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;
        }
     
    }
    }

    for this part of the assignment: • A no-argument constructor that creates a default fan.

    I am a little confused as to what you mean here by "pointless".. and would really appreciate a little more explanation. I am making my best efforts here to construct code on my own and understand everything, and though it may seem "below" you I am still struggling. Any help you can offer without attitude is always much appreciated. I am just a novice



    for this part of the assignment:

    • A toString( ) method 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 off, the method returns the fan color and radius along with the string, “fan is off” in one combined string.
    Not done.

    you say it is not done, but isnt this part of the code what this is asking for? :
    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 off!!");
            }
     
            return(printme);
     
        }

    thanks again for anyone who replies to this post.. your assistance is much appreciated
    Last edited by broo7198; August 8th, 2011 at 10:34 AM.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: JAVA problem : Create a Fan Class

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/47390-help-java-coding-assignment-fan-class.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Improving the world one idiot at a time!

Similar Threads

  1. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  2. Replies: 2
    Last Post: January 22nd, 2011, 11:20 PM
  3. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  4. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM
  5. Create a java small class for a bank account!!?
    By jon123 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 24th, 2010, 11:00 AM