Fórum Erro ao executar programa #509873
07/02/2015
0
Essa é minha classe porta:
class Porta {
String cor = "Marrom";
boolean aberta = true;
double dimensaoX = 80, dimensaoY = 180, dimensaoZ = 10;
void abre(){
this.aberta = true;
}
void fecha(){
this.aberta = false;
}
void pinta(String s){
this.cor = s;
}
boolean estaAberta(){
if(aberta == true){
System.out.println("Aberta");
}else{
System.out.println("Fechada");
}
return this.aberta;
}
}Essa é minha classe casa:
class Casa {
String cor;
Porta porta1, porta2, porta3;
int i = 0;
void pinta(String s){
this.cor = s;
}
int quantasPortasEstaoAbertas(){
if(porta1.aberta == true){
this.i++;
}
if(porta2.aberta == true){
this.i++;
}
if(porta3.aberta == true){
this.i++;
}
return this.i;
}
}E esse é o método main que utilizo para testar as classes é quando tento compilar ele utilizando algum dos metodos das classes como o quantasPortasEstaoAbertas() da o erro:
class TestaPorta{
public static void main(String[] args){
Casa casa = new Casa();
casa.pinta("Azul");
System.out.println(casa.cor);
casa.quantasPortasEstaoAbertas();
}
}Jonathan
Curtir tópico
+ 0Post mais votado
07/02/2015
class Casa {
String cor;
Porta porta1 new Porta();
Porta porta2 = new Porta();
Porta porta3 = new Porta();
int i = 0;
void pinta(String s){
this.cor = s;
}
int quantasPortasEstaoAbertas(){
if(porta1.aberta == true){
this.i++;
}
if(porta2.aberta == true){
this.i++;
}
if(porta3.aberta == true){
this.i++;
}
return this.i;
}
}
Ronaldo Lanhellas
Gostei + 1
Mais Posts
07/02/2015
Ronaldo Lanhellas
class Casa {
String cor;
Porta porta1 new Porta();
Porta porta2 = new Porta();
Porta porta3 = new Porta();
int i = 0;
void pinta(String s){
this.cor = s;
}
int quantasPortasEstaoAbertas(){
if(porta1.aberta == true){
this.i++;
}
if(porta2.aberta == true){
this.i++;
}
if(porta3.aberta == true){
this.i++;
}
return this.i;
}
}
Gostei + 1
07/02/2015
Ronaldo Lanhellas
class Casa {
String cor;
Porta porta1 new Porta();
Porta porta2 = new Porta();
Porta porta3 = new Porta();
int i = 0;
void pinta(String s){
this.cor = s;
}
int quantasPortasEstaoAbertas(){
if(porta1.aberta == true){
this.i++;
}
if(porta2.aberta == true){
this.i++;
}
if(porta3.aberta == true){
this.i++;
}
return this.i;
}
}
Gostei + 1
07/02/2015
Ronaldo Lanhellas
class Casa {
String cor;
Porta porta1 new Porta();
Porta porta2 = new Porta();
Porta porta3 = new Porta();
int i = 0;
void pinta(String s){
this.cor = s;
}
int quantasPortasEstaoAbertas(){
if(porta1.aberta == true){
this.i++;
}
if(porta2.aberta == true){
this.i++;
}
if(porta3.aberta == true){
this.i++;
}
return this.i;
}
}
Gostei + 1
08/02/2015
Jonathan
Gostei + 0
10/02/2015
Ronaldo Lanhellas
VEja bem, no seu caso o retorno do método ser 'int' é desnecessário já que este não é usado. Quando você retorna algum valor de um método (int, boolean, String e etc) é esperado que alguém vá usar este retorno, caso contrário não precisaria de retorno e consequentemente seria void.
class TestaPorta{
public static void main(String[] args){
Casa casa = new Casa();
casa.pinta("Azul");
System.out.println(casa.cor);
int qtdPortasAbertas = casa.quantasPortasEstaoAbertas();
System.out.println(qtdPortasAbertas);
}
}
No exemplo acima estou usando o retorno.
Gostei + 0
12/02/2015
Jonathan
Gostei + 0
12/02/2015
Ronaldo Lanhellas
Não necessariamente um atributo. Bom, para você saber se o seu método precisa ou não de um retorno faça a seguinte pergunta a si mesmo: "Eu vou usar este retorno em algum lugar ? "
Gostei + 0
12/02/2015
Ronaldo Lanhellas
Não necessariamente um atributo. Bom, para você saber se o seu método precisa ou não de um retorno faça a seguinte pergunta a si mesmo: "Eu vou usar este retorno em algum lugar ? "
Gostei + 0
13/02/2015
Jonathan
Gostei + 0
14/02/2015
Ronaldo Lanhellas
Gostei + 1
14/02/2015
Jonathan
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)