On off Status program
Hi everybody!. I磎 new to the forum. I expect to learn a lot from all Sun Java experts around the world. Besides, If someday i can help somebody, it will be an honor for me.
ok, here we go
I want to make a program that returns the state of a lamp (on Off), and I have to do 3 methods. I have to do one method that print the actual values that I have, but it doesn磘 work!!!
Here磗 the code:
public class bombillo {
int encendido;
int apagado;
int estado;
public void estado(int a){
estado = a;
}
public void encendido(int a){
estado = a;
if (estado != 1){
estado = 1;
}
}
public void apagado(int a){
estado = a;
if (estado != 2){
estado = 2;
}
}
public int imprime(){
System.out.println(estado);
}
public static void main(String[] args){
bombillo status = new bombillo();
status.encendido(1);
System.out.println("Esta: " + status.imprime);
}
}
Thank you
Best regards
[1070 byte] By [
karma1234a] at [2007-10-3 3:20:02]

1.Hey were you able to compile the program what ever you have written?
2.U have written a method like
public int imprime(){
System.out.println(estado);
}
by the method signature it has to return some int value but where as ur code is not doing so
3.in main method u have written like
System.out.println("Esta: " + status.imprime)
imprime is a method so u have to call like status.imprime()
ragasa at 2007-7-14 21:12:08 >

Note that Java supports the boolean type: true/false. This type is ideal
for the representation of an on/off state of something:public class Lamp {
private boolean on;
public Lamp() { this(false); }
public Lamp(boolean on) { setOn(on); }
public boolean isOn() { return on; }
public void setOn(boolean on; } { this.on= on; }
public String toString() { return on?"on":"off"; }
}
kind regards,
Jos
JosAHa at 2007-7-14 21:12:08 >

The source code that I put don磘 work, or it work but in other way that the one that I want. I am still a bit confused, because I create a method, and then I am calling this method, but it doesnt work. What I have wrong?best regards
0very useful regard, thank you.But I don磘 understand that boolean code that you write there, could you explain me this a little bit?thanks
> The source code that I put don磘 work,
No, it doesn't compile.
> or it work but
> in other way that the one that I want.
No, the code you posted has never run.
> I am still a
> bit confused, because I create a method, and then I
> am calling this method, but it doesnt work. What I
> have wrong?
You don't have the Java syntax right. If javac tells you something is wrong, you can't progress until you fix it.
%
> 0very useful regard, thank you.
> But I don磘 understand that boolean code that you
> write there, could you explain me this a little
> bit?
>
> thanks
boolean can be true or false.
Better go read a Java book before you continue. This is a basic part of the language.
%
> Note that Java supports the boolean type: true/false.
> This type is ideal
> for the representation of an on/off state of
> something:> public class Lamp {
>private boolean on;
> public Lamp() { this(false); }
>public Lamp(boolean on) { setOn(on); }
> public boolean isOn() { return on; }
>public void setOn(boolean on; } { this.on= on; }
> public String toString() { return on?"on":"off"; }
> }
Why not make the no-parameter constructor empty, if you want the value of "on" to be false? It will be false, anyway (default initialization of a boolean variable).
public Lamp() { }
MLRona at 2007-7-14 21:12:08 >

thanks everybody. Duffymo, I磎 studying java in university and by myself. I am beginning with the language and I know the basic concepts like the boolean value. My doubt is in the code of the method that the other guy kindly post, because I didn磘 know all the syntax of the language, so here is my doubt, cause it have private classes etc.
Best regards
> Why not make the no-parameter constructor emptyJust showing off ;-)kind regards,Jos
JosAHa at 2007-7-14 21:12:08 >
