Hey all,

I've tried not to ask questions for like a week or two but, I've got the Introduction to Java Programming (sixth edition) and I'm doing the Exercise 7.2 'The Fan Class' where I am to; Design a class named Fan to represent a fan. They have the UML Diagram of:

Fan +SLOW = 1
+MEDIUM = 2
+FAST = 3

-speed: int The speed of this fan (default 1).
-on: Boolean Indicates whether the fan is on (default false).
-radius: double The radius of this fan (default 5).
-color: String The color of this fan (default white).

+Fan() Constructs a fan with default values.
+getSpeed(): int Returns the speed of this fan.
+setSpeed(speed: int): void Sets a new speed for this fan.
+isOn(): Boolean Returns true if this fan is on.
+setOn(on: boolean): void Sets this fan on to true or false.
+getRadius(): double Returns the radius of this fan.
+setRadius(radius: double): void Sets a new radius for this fan.
+getColor(): String Returns the color of this fan.
+setColor(color: String): void Sets a new color for this fan.
+toString(): String Returns a string representation for this fan.

So far, I've got this... I know that I'm supposed to be utilizing these other variables and constructors etc. but I don't know how or why. HELP!?! (lol)

public class Fan
  {
  //main method
  public static void main(String[] args)

  //variables
  slow = 1;
  medium = 2;
  fast = 3;

  boolean on = false;

  int speed = 1;
  double radius = 5;

  String color = blue;


  //make my fan
  Fan()
  {
  }

  //fan's radius
  Fan(double radius = 5)
  {
  }

  //get the colors
  public boolean getColorValue()
  {
   if(getColor() == Color.blue)
   {
    //something goes here
    return true;
   }
    else
   {
    //something goes here
    return false;
   }
   
  //The to String

  String toString();
{

String result;

if(on == true)
{
  String speedString;

  if(speed == slow)
    speedString = "slow"
   else if (speed == medium)
    speedString = "medium"
   else if (speed == fast)
    speedString = "fast"

  result = "Speed = " + speedString; + "Color = " + color + "Radius = " + radius;
}
else
  result = "Color = " + Color + "Radius = " + radius + "fan is off"'
{

}
return result;


System.out.println(fan1.toString());
System.out.println(fan2.toStrint());

You need to re-examine the basic structure of your class. This will not compile. Your main() method is not declared correctly. Several methods and even the class itself has no end brace. At least go through and make sure the methods are properly structured before you worry about putting any code in them.

public class Fan
{
   /**
   * A class to represent a fan, whether it be on/off, with varying speeds, fan size and colors.
   */

   //Main method shouldn't have been here, this class is for your object.

   //Global variable declarations
   private fanSpeed;
   private double radius;
   private Color fanColor;
   private boolean isOn;

   public Fan( )
   {
      fanSpeed = 1; //Default speed
      isOn = false; // Off by Default
      radius = 5; //Default radius
      fanColor = Color.WHITE; //Default color
   }

   //You had Fan(){} here, no point, it didn't do anything, see above method. This initialises everything for you, and its where you're defaults should be set.


  //You had Fan( double radius = 5 ){} here, again, no point, as this won't compile, the argument goes nowhere, and doesn't initialise anything. This is done above.

   /**
   * A method to view the speed of the fan.
   * @return The numerical representation of the speed of the fan.
   */

   public int getSpeed( )
   {
      return fanSpeed;
   }

   /**
   * A method to set the speed of the fan.
   * @param _fanSpeed The new speed of the fan in the range 1 - 3.
   */

   public void setSpeed( int _fanSpeed )
   {
      if ( _fanSpeed >= 1 && _fanSpeed <= 3 )
         fanSpeed = _fanSpeed;
      else // Otherwise leave it where it is
         return;
   }

   /**
   * A method the check if the fan is currently on or off. Wasn't included.
   * @return True if the fan is on.
   */

   public boolean isOn( )
   {
      return isOn;
   }

   /**
   * A method to turn the fan on or off. Wasn't included.
   * @param onOff True if it is being switched on.
   */

   public void setOn( boolean onOff )
   {
      isOn = onOff;
   }

   /**
   * A method to get the current radius of the fan. Wasn't included.
   * @return The radius of the fan.
   */

   public double getRadius( )
   {
      return fanSize;
   }

   /**
   * A method to change the size of the fan. Wasn't included.
   * @param _fanSize The new size of the fan.
   */
   
   public void setRadius( double _fanSize )
   {
      fanSize = _fanSize;
   }

   /**
   * A method to get the current Color of the fan.
   * @return The string representation of the fan.
   */

   //If you'd looked up the API for Color, you'd have found that the Color class has it's own method for returning String representations of itself. The internet is a tool use it.Color API. This will save you a lot of work.

   public String getColor( )
   {
      return fanColor.toString( )
   }

   /**
   * A method to set the Color of the fan.
   * @param _fanColor The new Color of the fan.
   */

   //You had toString( ) here, in my opinion, stuff like that should come last in the class, and if you have a method like getColor( ), then you should  have setColor( ) straight after, it makes you code more logical, and easier to read, also, I recommend you read up on using Javadoc, it'll add a lot to your code's readibility.

   public void setColor( Color _fanColor )
   {
      fanColor = _fanColor;
   }

   /**
   * A method to return a String representation of the current fan.
   * @return The current fan.
   */

   //The toString( ) was very messy it could have been done easier and neater.

   public String toString( )
   {
      String toString = "The current fan has: \nSize: " + radius + "\nColor: " + getColor( ) + "\nSpeed: ";
      if ( fanSpeed == 1 )
         toString += "Slow\n";
      else if ( fanSpeed == 2 )
         toString += "Medium\n";
      else
         toString += "Fast\n";
      toString += "Switched: ";
      if ( isOn )
         toString += "On";
      else
         toString += "Off"
      return toString;      
   }
}

Your class structure was a bit of a mess there mate, I've tidied it up for you a bit.
You could use these methods inside another class after declaring a new Fan object
i.e. Fan blower = new Fan( );
System.out.println( blower.toString( ) );

Comments
Point them in the right direction, do not do their homework for them, that is against the daniweb terms of use, and it doesn't help them. Do you really think the OP will even look at the code you wrote, or simply turn it in, as is, as his own?

Your class structure was a bit of a mess there mate, I've tidied it up for you a bit.
You could use these methods inside another class after declaring a new Fan object
i.e. Fan blower = new Fan( );
System.out.println( blower.toString( ) );

Super thanx to Ezzaral and you mickinator. Sorry for being such a pain but the person in class with me who is also working on this were stopped as to not know exactly what else to do because we're still beginning. the tutorial that you, mickinator, put up was f-ing amazing, this is something i can go over and over until i truly learn this part of class & objects b/c the actual lecture isn't this good.

once again, uber-thanx

peace
l.green

If that person gave up, make sure to let your instructor know that.
He or she deserves no credits for your work.

Don't worry about it anytime, the reason i said to learn more about javadoc, is that it's a better way to comment imho, plus peoples will be more incline to help you if you know "what" you should be doing and where you should be doing it, even if what you have is wrong.

Also, what jwenting said, I'm in second year and I've had people do that to me too, if people are going to be f**ers why would you bother letting them get a grade for you work. Mention it to the lecture, but on the quiet and see if he'll make an exception for it, at the very least you'll get the mark he was going to give you anyway - your work is your work - but the other person might be disallowed for it.

You need any help mate give us a shout.

This article has been dead for over six months. Start a new discussion instead.