2
Well, I’ve done a lot of research, I have a sense of how it works, but I couldn’t implement it, I’d like a light. I have the classes House and Building, I need to create a father class Immovable, having House and Building as daughter classes, follows my code:
package br.edu.utfpr.exer05;
public class Porta {
boolean aberta;
String cor;
double dimensaoX, dimensaoY, dimensaoZ;
void abre() {
this.aberta = true;
}
void fecha() {
this.aberta = false;
}
void pinta(String cor) {
this.cor = cor;
}
boolean estaAberta() {
boolean estaAberta = false;
if(aberta == true) estaAberta = true;
return estaAberta;
}
}
package br.edu.utfpr.exer05;
import java.util.ArrayList;
public class Edificio {
String cor;
int totalDePortas;
int totalDeAndares;
ArrayList<Porta> portas = new ArrayList<Porta>();
void pinta(String cor) {
this.cor = cor;
}
int quantasPortasEstaoAbertas() {
int qtdPortasAbertas = 0;
for(int i=0; i<portas.size(); i++) {
portas.get(i);
if(portas.get(i).estaAberta() == true) {
qtdPortasAbertas++;
}
}
return qtdPortasAbertas;
}
void adicionaPorta(Porta porta) {
this.portas.add(porta);
}
int totalDePortas() {
return this.portas.size();
}
void adicionaAndar() {
this.totalDeAndares = totalDeAndares += 1;
}
int totalDeAndares() {
return this.totalDeAndares;
}
}
package br.edu.utfpr.exer05;
public class Casa {
String cor;
Porta porta1, porta2, porta3;
void pinta(String cor) {
this.cor = cor;
}
int quantasPortasEstaoAbertas() {
int qtdPortasAbertas=0;
if(porta1.estaAberta() == true) qtdPortasAbertas++;
if(porta2.estaAberta() == true) qtdPortasAbertas++;
if(porta3.estaAberta() == true) qtdPortasAbertas++;
return qtdPortasAbertas;
}
int totalDePortas() {
return 3;
}
}
And here’s the class I need to make the "father":
package br.edu.utfpr.exer05;
import java.util.ArrayList;
public class Imovel {
String cor;
ArrayList<Porta> portas = new ArrayList<Porta>();
}
The classes House and Building are made in different ways because the exercise asked, but has the same objective. Good, how to proceed to create a parent class as described?
If to create the inheritance, only the
extends
after the class names so:public class Edificio extends Imovel
– rray
Okay, thanks... I could show you an example of what the immovable class would look like?
– rodrigom