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:
- Three constants named SLOW, MEDIUM and FAST with value 1, 2, and 3 to denote the fan speed.
- An int data field named speed that specifies the speed of the fan(default SLOW).
- A boolean data field named on that specifies whether the fan is on(default false).
- A double data field named radius that specifies the radius of the fan(default 5).
- A string data field named color that specifies the color of the fan (default blue).
- A no-argument constructor that creates a default fan.
- The accessor and mutator methods for all four data field.
- 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.
Code :
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; }
}
:confused:
Re: Help me this please..
Hello qaromi. Welcome to the Java Programming Forums.
This looks pretty good to me... Don't forget number 8.
Re: Help me this please..
anyway i duno the last 1. number 8... how to combined?
toString()? is it new method? can show me?
Re: Help me this please..
Hmm..
It could possible be something like this:
Code :
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;
}
}
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..
Code :
Fan fan = new Fan(1, false, 5, "blue");
fan.seton$off(true);
fan.setcolor("blue");
fan.setspeed(3);
thx JavaPF.. ur really help me.. :-bd
thx alot
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
Code :
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:
Code :
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;
}
}
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 :o
Code :
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);
}
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.
Code :
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