Design a class named Fan to represent a fan. The class contains Class Fan Object
ID: 3637103 • Letter: D
Question
Design a class named Fan to represent a fan. The class containsClass Fan
Object
extended by Fan
public class Fan
extends Object
Field Summary
static int FAST
static int MEDIUM
static int SLOW
Constructor Summary
Fan()
Constructor sets default values: radius = 5.0 color = "blue" speed = SLOW on = false
Fan(double radius, String color, int speed, boolean on)
Constructor with parameters for all data fields
Method Summary
String getColor()
Get the color of the fan
double getRadius()
Get the radius of the fan
int getSpeed()
Get the speed of the fan
boolean isOn()
Check whether fan is on
void setColor(String color)
Change the color of the fan
void setOn(boolean on)
Set whether fan is on / off
void setRadius(double radius)
Change value of the radius
void setSpeed(int speed)
Change the fan speed
String toString()
Returns string that describes the Fan (one type of string if the fan is on - another if it is off)
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Field Detail
FAST
public static final int FAST
See Also:
Constant Field Values
MEDIUM
public static final int MEDIUM
See Also:
Constant Field Values
SLOW
public static final int SLOW
See Also:
Constant Field Values
Constructor Detail
Fan
public Fan()
Constructor sets default values: radius = 5.0 color = "blue" speed = SLOW on = false
Fan
public Fan(double radius,
String color,
int speed,
boolean on)
Constructor with parameters for all data fields
Parameters:
radius -
color -
speed -
on - true / false
Throws:
IllegalArgumentException - if radius is <= 0
IllegalArgumentException - if speed is not one of the defined values
Method Detail
getColor
public String getColor()
Get the color of the fan
Returns:
the color
getRadius
public double getRadius()
Get the radius of the fan
Returns:
the radius
getSpeed
public int getSpeed()
Get the speed of the fan
Returns:
the speed
isOn
public boolean isOn()
Check whether fan is on
Returns:
true of fan is on ; false otherwise
setColor
public void setColor(String color)
Change the color of the fan
Parameters:
color - the color to set
setOn
public void setOn(boolean on)
Set whether fan is on / off
Parameters:
on - true to turn on fan; false for off
setRadius
public void setRadius(double radius)
Change value of the radius
Parameters:
radius - the radius to set
Throws:
IllegalArgumentException - if radius is <= 0
setSpeed
public void setSpeed(int speed)
Change the fan speed
Parameters:
speed - the speed to set
Throws:
IllegalArgumentException - if speed is not one of the defined values
toString
public String toString()
Returns string that describes the Fan (one type of string if the fan is on - another if it is off)
Overrides:
toString in class Object
Explanation / Answer
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DrawArcs extends JFrame { public DrawArcs() { Timer timer = new Timer(1000, new TimerListener()); timer.start(); setTitle("Show Arcs"); getContentPane().add(new ArcsPanel()); } private class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { repaint(); } } public static void main (String [] args) { DrawArcs frame = new DrawArcs(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); } class ArcsPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4); int x = xCenter - radius; int y = yCenter - radius; int sLength = (int)(radius * 0.8); g.setColor(Color.blue); g.fillArc(x, y, 2 * radius, 2 * radius, 0, 45); g.setColor(Color.blue); g.fillArc(x, y, 2 * radius, 2 * radius, 90, 45); g.setColor(Color.blue); g.fillArc(x, y, 2 * radius, 2 * radius, 180, 45); g.setColor(Color.blue); g.fillArc(x, y, 2 * radius, 2 * radius, 270, 45); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.