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);
}
}