How do I average the method: calculate AverageWeightDownload? POO Java

0

I have to do this program, my question is how to do point 4), which is to calculate the average of the Locomotive class. At the time it occurred to me to do a summation for Carload weight that belongs to the vagon class and another sum for amount of Voxons of the locomotive class: average = sum of weight / sum of cargo, but in eclipse I get an error, I'm sure I thought it wrong, but I can not think of another way to do it.

It is necessary to generate the following classes for a railway company:

1) Generates the Vagon class with the following attributes:

VAGONID: String

description: String

brand: String

Maximum weight that supports: int

pesoDeCarga: int

Generate 2 constructors with the following restrictions for the Vagon class

a) Constructor that receives all the parameters of the vagon class

b) The fields ofVagon identifier, brand and maximumWeight that are required

c) In case the loadweight is omitted, you must assign the value 0

d) From an example how to use both constructors

2) Create the Locomotive class with the following attributes:

a) Locomotive identifier: String

wagons []: Vagon

b) Create a constructor for the Locomotive class that receives the following parameters (String identificationLocomotive, int amount of Voladores)

3) Generate the following methods respecting the JAVA conventions

VAGON CLASS:

• getVagonIdentifier

• setIdentifierDeVagon

• getPesoDeCarga

• setPesoDeCarga

• toString

LOCOMOTIVE CLASS:

• getVagones

• setVagones

4) You must create the method Calculate Average Weight Load class Locomotive. This method must calculate the average of the loads of all the wagons.

5) Generate a method that verifies the format of theVagonIdentifier attribute. The format of that field must be the first character one character and the rest of the characters an integer. Example A12345 in case that condition is met the method must return a true and therefore a false.

public class Vagon {


//Atributos

private String identificadorDeVagon ;
private  String descripcion ;
private String marca ;
private int pesoMaximoQueSoporta ;
private int pesoDeCarga;

//Constructores

public Vagon (String identificadorDeVagon , String descripcion , String marca , int pesoMaximoQueSoporta , int pesoDeCarga ){
    this.identificadorDeVagon = identificadorDeVagon ;
    this.descripcion = descripcion;
    this.marca = marca;
    this.pesoMaximoQueSoporta = pesoMaximoQueSoporta ;
    this.pesoDeCarga = pesoDeCarga;

}

public Vagon (String identificadorDeVagon , String marca ,  int pesoMaximoQueSoporta) {

    this.identificadorDeVagon = identificadorDeVagon ;
    this.marca = marca;
    this.pesoMaximoQueSoporta = 0;

}

public String getidentificadorDeVagon () {

    return this.identificadorDeVagon;
}

public void setidentificadorDeVagon(String identificadorDeVagon) {

    this.identificadorDeVagon = identificadorDeVagon ;
}

public int getpesoDeCarga () {

    return this.pesoDeCarga;
}

public void setpesoDeCarga(int pesoDeCarga) {

    this.pesoDeCarga = pesoDeCarga;


}

public String toString () {         return "Vagon [VAGON_ID=" + VAGON_ID + ", CARG_Weight=" + CARGO_Weight + "]";     }

public boolean VAGON identifier (char id) {

 boolean estado = false; 
 char unCaracter[] = new char[1];
 unCaracter [0] = 'A';
 int entero [] = new int[1];
 entero[0] = 12345;


 for (int i = 0; i < 1; i++) {



     if((id ==unCaracter[0])&&(id ==entero[0])) {

         estado = true;


     }

     else {

         estado = false;
     }
}


 return estado;

} }

public class Locomotora {

//Atributos

    private String identificacionLocomotora ;
    private Vagon vagones[];

    //Constructor

    public Locomotora(String identificadorLocomotora , int cantidadDeVagones) {
        this.identificacionLocomotora = identificadorLocomotora;
        this.vagones = new Vagon [cantidadDeVagones];

    }


    public Vagon[] getvagones() {

        return this.vagones;

    }

    public void setVagones(Vagon[] vagones) {

        this.vagones = vagones ;
    }

    public double calcularPromedioPesoDescarga() {

        double promedio ;

        int sumatoriaPeso;
        int sumatoriaCarga;

        sumatoriaPeso += this.getpesoDeCarga;
        sumatoriaCarga+=this.vagones;

        promedio = sumatoriaPeso / sumatoriaCarga;

        return promedio;
    }

}

public class PruebaVagon {

public static void main(String[] args) {

    //1)d)Ejemplos de como usar los dos contructores
     Vagon vagon1 = new Vagon ("678u" ,"azul" , "ffs" , 4576 ,234);
     Vagon vagonn = new Vagon ("htr780","lolop" , 87089);

}

}

    
asked by computer96 26.11.2018 в 00:49
source

1 answer

1

Note that the class Locomotora receives an array of wagons, then it is necessary to open a loop for to add the total weight that the locomotive supports, then dividing that total among all the wagons.

Starting with Java 8, instead of the for loop you can use lambda expressions to do the calculation.

The for loop that we use here uses elements of the class Vagon , so we can use the getpesoDeCarga() method to obtain the weight of each one.

The method would then look like this:

public double calcularPromedioPesoDescarga() {
    int sumatoriaPeso=0;
    int totalVagones=this.vagones.length;
    for (Vagon vagon: this.vagones) {  
        sumatoriaPeso+=vagon.getpesoDeCarga();
    }
    double promedioCarga=sumatoriaPeso/totalVagones;
    return promedioCarga;
}

As for the test code, if you want you can write it like that, so that you do not have to set each wagon separately to the class:

     Vagon[] vagones = new Vagon[2];
     vagones[0] = new Vagon ("678u" ,"azul" , "ffs" , 4576 ,234);
     vagones[1] = new Vagon ("htr780","lolop" ,87089);
     Locomotora biVagon=new Locomotora("loc1",2);
     biVagon.setVagones(vagones);
     System.out.println(biVagon.calcularPromedioPesoDescarga());

The output, with the test data that you sample is as follows:

117.0

That's the average weight of your two-car locomotive.

  

NOTE:

     

Some of your methods do not meet the naming convention of   Java. For example, this method: getpesoDeCarga() , must be   written like this so that it respects the naming convention:    getPesoDeCarga() .

    
answered by 26.11.2018 / 04:48
source