Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Hi everyone,

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi everyone,

    I just took a java course a couple months ago. Actually, I'm blocked at ArrayList. I don't know how to work with it.
    I got a program to write . This one is included polymorphism, heritage, override, abstract.... the first class is Vehicule, class parent. In this class, it has to defined
    the weight, Immatricule of car(Voiture), truck(Camion), Camionnette... I got the problem in Convoi class and do not knot now how to get data and show the informations in Main(String args[])That is my first time dealing with ArrayList.. Please help. Thanks in advance.

    In the class Convoi, it need to show the result of the max speed of each vehicule's types


    In this class, an ArrayList<Vehicule> lVehicule as an ArrayList Vehicule
    a loop For(Vehicule vehicule:lVehicule)..., a variable type String for the name of Convoi
    Convoi(ArrayList vehicule, String nameConvoi)

    String toString to return nameConvoi and informations of Convoi(voiture, camion, camionnette)
    another function returning average of convoi vitesse
    a convoi can have many different types of vehicule
    show the name of convoi and the informations of vehicule in this convoi

    create a Utilities class, in this class, calculating the consommation of vehicule in funtion of speed and weight ,
    total weight = poid a vide+ charge actually
    gaz consomation = speed km/h/10+weight

    write a public static float consommation(int v,int p) return the consommation (gallon ) for each 100m consommation of 1 vehicule with average speed
    show the consommation of 1 convoi in max speed.

    -----------------------------------------------------------
       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);   
          }    
     
          }
     
     
     
       }
    Last edited by JavaPF; November 1st, 2011 at 04:37 AM. Reason: Please use highlight tags!