public class Fan {
	final int SLOW = 1;
	final int MEDIUM = 2;
	final int FAST = 3;
	private int speed;
	private boolean on;
	private double radius;
	private String color;

	public Fan(){
		speed = SLOW;
		on = false;
		radius = 5;
		color = "Blue";
	}
	
    public Fan(int speed, boolean on, double radius, String color) {
    }
    
    public void setSpeed(int speed){
    	this.speed = speed;
    }
    public int getSpeed(){
    	return speed;
    }
    
    public void setOn(boolean on){
    	this.on = on;
    }
    public boolean isOn(){
    	return on;
    }
    
    public void setRadius(double radius){
    	this.radius = radius;
    }
    public double getRadius(){
    	return radius;
    }
    
    public void setColor(String color){
    	this.color = color;
    }
    public String getColor(){
    	return color;
    }
    
    public String toString(){
    	if(on == false)
    		return "\nFan Speed: " + getSpeed() + "\nColor: " + getColor() + "\nRadius: " + getRadius();
    	else
    		return "\nColor: " + getColor() + "\nRadius: " + getRadius() + " (Fan is Off)";
    }
}

class TestFan{
	public static void main(String[] args){
		Fan[] arrFan = new Fan[2];
		arrFan[0] = new Fan(FAST, true, 10, "Yellow");
		arrFan[1] = new Fan(MEDIUM, false, 5, "Blue");
		
		for(int i = 0; i < arrFan.length; i++)
			System.out.println(arrFan[i].toString());
	}
}

my TestFan class wanna to assign value, but there's error occur with my array assignment, if i no misstaken, i think failed to get final value, so how? :(

Recommended Answers

All 14 Replies

You can't access the speed value that way. Also, those variables are local to the Fan class. You need to make them public and be static if you are going to use it that way...

public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;

Then when you use it in TestFan...

arrFan[0] = new Fan(Fan.FAST, true, 10, "Yellow");

awh, but i have to the variable "speed" to represent the SLOW, MEDIUM and FAST. how ?
by the way, as the question require to put default value on each variables, is my default value assign correctly in Fan() ?

What's the question about the speed var? What you are doing now is good. Also your default value is good. No problemo.

ps: Those 3 speeds should ideally be an enum. If you have not covered enums in your course then ignore this comment!

in fact, i really haven't cover enums yet. XD
speed is take the value from SLOW, MEDIUM, or FAST.

OK, no problem.
In the same class you can just use SLOW etc just like any other int, as in speed = SLOW; or if (speed == SLOW) ... In any other class you have to use the classname as well so Java can find it, as in Fan.SLOW

okay, i've modified my test class;

class TestFan{
	public static void main(String[] args){
		Fan[] arrFan = new Fan[2];
		arrFan[0] = new Fan(arrFan[0].FAST, true, 10, "Yellow");
		arrFan[1] = new Fan(arrFan[1].MEDIUM, false, 5, "Blue");
		
		for(int i = 0; i < arrFan.length; i++)
			System.out.println(arrFan[i].toString());
	}
}

a problem come out again (NULL POINT EXCEPTION), at assign value into the array.

arrFan[0].FAST tries to use an array element that you haven't initialised yet, so that's why its null.
But that's not what I just posted! Inside any class other than Fan itself, you must refer to those static variables as Fan.SLOW, Fan.MEDIUM and Fan.FAST.

yeap, i also did the static on SLOW MEDIUM and FAST.
and it can display, but wrong information displayed.

arrFan[0] = new Fan(Fan.FAST, true, 10, "Yellow");

is this what you mean?

by the way, how use no-argument constructor to call 3 parameter constuctor? (No Coverd)

I have no idea what "wrong information displayed" means, exactly. What wrong info, where how?
Yes, that's what I meant.
Why do you want to call the 4 parameter constructor from the () constructor? I can't see any reason for you to do that. Anyway, if, in general, you want to call another constructor from inside a constructor (of the same class) you use the "this" keyword, eg

class XXX {
   XXX() { ... }  // no-args constructor
   xxx(int i) {   // one arg constructor
     this();      // executes the no-args constructor for this class
     ...
   }

which mean the display all null to me, is not display the value that i've assigned.

oh, call for the constuctor is another practical question one. :]

which mean the display all null to me, is not display the value that i've assigned.

Let's get serious now... that sentence tells me NOTHING. I don't know what exactly you are trying to display, or where you are trying to display it, or what code you are using to try to display it with. How can I possible comment without all that info?
If you want help with code that not doing what you want you MUST be very precise and very complete in describing exactly what the problem is and which code is involved.

alright, now i repost all my code here
the display output is not that i've been assigned, in fact the output is all become "null"

public class Fan {
	final static int SLOW = 1;
	final static int MEDIUM = 2;
	final static int FAST = 3;
	
	private int speed;
	private boolean on;
	private double radius;
	private String color;

	public Fan(){
		speed = SLOW;
		on = false;
		radius = 5;
		color = "Blue";
	}
	
    public Fan(int speed, boolean on, double radius, String color) {
    }
    
    public void setSpeed(int speed){
    	this.speed = speed;
    }
    public int getSpeed(){
    	return speed;
    }
    
    public void setOn(boolean on){
    	this.on = on;
    }
    public boolean isOn(){
    	return on;
    }
    
    public void setRadius(double radius){
    	this.radius = radius;
    }
    public double getRadius(){
    	return radius;
    }
    
    public void setColor(String color){
    	this.color = color;
    }
    public String getColor(){
    	return color;
    }
    
    public String toString(){
    	if(on == true)
    		return "\nFan Speed: " + getSpeed() + "\nColor: " + getColor() + "\nRadius: " + getRadius();
    	else
    		return "\nColor: " + getColor() + "\nRadius: " + getRadius() + " (Fan is Off)";
    }
}

class TestFan{
	public static void main(String[] args){
		Fan[] arrFan = new Fan[2];
		arrFan[0] = new Fan(Fan.FAST, true, 10, "Yellow");
		arrFan[1] = new Fan(Fan.MEDIUM, false, 5, "Blue");
		
		for(int i = 0; i < arrFan.length; i++)
			System.out.println(arrFan[i].toString());
	}
}

Line 18~19, you have a correct constructor to accept variables, but you do NOTHING in the constructor! You need to assign the incoming arguments to proper class's variables.

oh yeah. -.-
i've made terrible misstake. sorry and Thanks dude anyway. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.