It usefull if you can show us another example by using ArrayList in class child relating with class parent..and then manipulate them and display them in Main....I wish to know how that to get informations and also do calculating the data from the class Vehicule, voit, Camion, Camionnette...in a different way ....by declaring constructor Convoi(ArrayList vec, String nom).... I tried to work with this way but it's not run. I think that I didn't do right something..please help. Thank in advanced.
import java.util.ArrayList;
abstract class Vehicule
{protected String immat;
protected float poidVide=0f;
protected int type;
protected float charge=0f;
Vehicule(String immat,float poidVide)
{
this.immat=immat;
this.poidVide=poidVide;
type=0;
}
public abstract int calVitMax();
public abstract String toString();
}
class Voiture extends Vehicule {
private int mVitesse;
Voiture(String m, float n,float charge)
{super(m,n);
type=1;
this.charge = charge;
}
public int calVitMax()
{
if((type==1)&&(poidVide==1))
mVitesse=150;
else
mVitesse=90;
return mVitesse;
}
public String toString()
{
return "Immatricule :"+this.immat+" categorie : voiture, charge "+this.charge+"ton(s), poid vide:"+this.poidVide+" ton(s)";
}
}
class Camionnette extends Vehicule {
private int mVitesse;
Camionnette(String m, float n,float charge)
{super(m,n);
type=2;
this.charge = charge;
}
public int calVitMax()
{
if(type==2&&poidVide<=1.5)
{
if(this.charge==0)
mVitesse=130;
if(this.charge<=1)
mVitesse=110;
if(this.charge>1)
mVitesse=90;
}
else
System.out.println("Invalid data, Veuillez entrer a value pour le charge < 1.5 tons");
return mVitesse;
}
public String toString()
{
return "Immatricule: "+this.immat+" categorie : Camionnnette , charge "+this.charge+" ton(s),poid vide:"+this.poidVide+" ton(s)";
}
}
class Camion extends Vehicule {
private int maxVitesse;
Camion(String m, float n,float charge)
{super(m,n);
type=3;
this.charge = charge;
}
public int calVitMax()
{
if(type==3&&poidVide<4)
{
if(this.charge==0)
maxVitesse=130;
if(this.charge<=3)
maxVitesse=110;
if(this.charge>3)
maxVitesse=90;
}
else
System.out.println("Invalid data, please enter new value for a charge");
return maxVitesse;
}
public String toString()
{
return "Immatrucule: "+ this.immat+" categorie : camion, charge "+this.charge+" ton(s) poid vide:"+this.poidVide+" ton(s)";
}
}
class Convoi extends Vehicule
{
protected ArrayList<Vehicule> lesVehic;
protected String nomConvoi;
private int som;
Convoi(ArrayList v, String nomConvoi)
{
lesVehic= new ArrayList<Vehicule>();
this.lesVehic = v;
this.nomConvoi=nomConvoi;
som=0;
/*for(Vehicule vehic:lesVehic)
vehic.add(lesVehic,nomConvoi);*/
}
public String toString()
{
return this.nomConvoi+" "+this.lesVehic+" ";
}
public int VitesseMoyen(int vit)
{
som+=les.Vehic.calVitMax(vit);
return som/lesVehic.size();
}
}
public class TestVehicule
{
public static void main(String args[])
{
Vehicule[] vehicule = new Vehicule[6];
Voiture voit = new Voiture("ADR-45687",1f,1.5f);
Camion camion = new Camion("ERT-78945",1.5f,3f);
Camionnette camionnet = new Camionnette("RTC-453288",1.5f,1f);
Voiture voit2 = new Voiture("DRt-68733",1f,1f);
Camion camion2 = new Camion("TAZ-94221",1.5f,4f);
Camionnette camionnet2 = new Camionnette("CQA-32844",1.5f,0f);
vehicule[0] = voit;
vehicule[1]= camion;
vehicule[2]= camionnet;
vehicule[3] = voit2;
vehicule[4]= camion2;
vehicule[5]= camionnet2;
for(int i=0;i<6;i++)
{System.out.println(vehicule[i].toString());
System.out.println("max vitesse: "+vehicule[i].calVitMax()+" km/h");
}
ArrayList<Convoi> convoi = new ArrayList<Convoi>(Voiture v,"AER-77865");
for(Vehicule vehicule:lesVehic)
{
convoi.toString();
convoi.VitesseMoyen(135);
}
}
}
need help with ArrayListbamiday October 27, 2011 at 7:51 PM
It usefull if you can show us another example by using ArrayList in class child relating with class parent..and then manipulate them and display them in Main....I wish to know how that to get informations and also do calculating the data from the class Vehicule, voit, Camion, Camionnette...in a different way ....by declaring constructor Convoi(ArrayList vec, String nom).... I tried to work with this way but it's not run. I think that I didn't do right something..please help. Thank in advanced. import java.util.ArrayList; abstract class Vehicule {protected String immat; protected float poidVide=0f; protected int type; protected float charge=0f; Vehicule(String immat,float poidVide) { this.immat=immat; this.poidVide=poidVide; type=0; } public abstract int calVitMax(); public abstract String toString(); } class Voiture extends Vehicule { private int mVitesse; Voiture(String m, float n,float charge) {super(m,n); type=1; this.charge = charge; } public int calVitMax() { if((type==1)&&(poidVide==1)) mVitesse=150; else mVitesse=90; return mVitesse; } public String toString() { return "Immatricule :"+this.immat+" categorie : voiture, charge "+this.charge+"ton(s), poid vide:"+this.poidVide+" ton(s)"; } } class Camionnette extends Vehicule { private int mVitesse; Camionnette(String m, float n,float charge) {super(m,n); type=2; this.charge = charge; } public int calVitMax() { if(type==2&&poidVide<=1.5) { if(this.charge==0) mVitesse=130; if(this.charge<=1) mVitesse=110; if(this.charge>1) mVitesse=90; } else System.out.println("Invalid data, Veuillez entrer a value pour le charge < 1.5 tons"); return mVitesse; } public String toString() { return "Immatricule: "+this.immat+" categorie : Camionnnette , charge "+this.charge+" ton(s),poid vide:"+this.poidVide+" ton(s)"; } } class Camion extends Vehicule { private int maxVitesse; Camion(String m, float n,float charge) {super(m,n); type=3; this.charge = charge; } public int calVitMax() { if(type==3&&poidVide<4) { if(this.charge==0) maxVitesse=130; if(this.charge<=3) maxVitesse=110; if(this.charge>3) maxVitesse=90; } else System.out.println("Invalid data, please enter new value for a charge"); return maxVitesse; } public String toString() { return "Immatrucule: "+ this.immat+" categorie : camion, charge "+this.charge+" ton(s) poid vide:"+this.poidVide+" ton(s)"; } } class Convoi extends Vehicule { protected ArrayList<Vehicule> lesVehic; protected String nomConvoi; private int som; Convoi(ArrayList v, String nomConvoi) { lesVehic= new ArrayList<Vehicule>(); this.lesVehic = v; this.nomConvoi=nomConvoi; som=0; /*for(Vehicule vehic:lesVehic) vehic.add(lesVehic,nomConvoi);*/ } public String toString() { return this.nomConvoi+" "+this.lesVehic+" "; } public int VitesseMoyen(int vit) { som+=les.Vehic.calVitMax(vit); return som/lesVehic.size(); } } public class TestVehicule { public static void main(String args[]) { Vehicule[] vehicule = new Vehicule[6]; Voiture voit = new Voiture("ADR-45687",1f,1.5f); Camion camion = new Camion("ERT-78945",1.5f,3f); Camionnette camionnet = new Camionnette("RTC-453288",1.5f,1f); Voiture voit2 = new Voiture("DRt-68733",1f,1f); Camion camion2 = new Camion("TAZ-94221",1.5f,4f); Camionnette camionnet2 = new Camionnette("CQA-32844",1.5f,0f); vehicule[0] = voit; vehicule[1]= camion; vehicule[2]= camionnet; vehicule[3] = voit2; vehicule[4]= camion2; vehicule[5]= camionnet2; for(int i=0;i<6;i++) {System.out.println(vehicule[i].toString()); System.out.println("max vitesse: "+vehicule[i].calVitMax()+" km/h"); } ArrayList<Convoi> convoi = new ArrayList<Convoi>(Voiture v,"AER-77865"); for(Vehicule vehicule:lesVehic) { convoi.toString(); convoi.VitesseMoyen(135); } } }
Post your Comment