I have a question about the following exercise, in the NOTE it says that duplicate elements are not allowed in the classes, and in the exercise solution, in 3 classes (classes: AutoLujo, AutoCompacto and Wagon) I put the same code (same methods and attributes) because I did not find another possible solution for the exercise, is there a way to correct the repeated code? Or is the solution fine now?
Exercise: A car dealership wants a computer system to manage its vehicle data and classify it by type. All cars have the following data:
- Engine serial number
- Brand
- Year
- Price
Vehicles are categorized into compact cars, luxury cars, pickup trucks, and station wagons . For cars and vans, it is also important to store the number of passengers; while for trucks, the load capacity in kgs must be controlled. and the number of axles and tracks.
Model this system and instantiate each of the classes, assigning data through their respective properties. Add a parameterized constructor to each class to initialize its data and call the base class's constructor from each derived class's constructor (do not use default constructors). Implement the ToString() method override to display the data for each car type.
NOTE: Duplicate components in classes and empty classes (no elements) are not allowed.
interface Pasajeros
{
int getCantidadPasajeros();
void setCantidadPasajeros(int n);
}
class Auto
{
private int numSerieMotor,año;
private String marca;
private double precio;
Auto(){}
Auto(int numSerieMotor, int año, String marca,double precio)
{
this.numSerieMotor=numSerieMotor;
this.año=año;
this.marca=marca;
this.precio=precio;
}
public String toString()
{
return numSerieMotor+" "+ año+" "+marca+" "+ precio;
}
}
class AutoCompacto extends Auto implements Pasajeros
{ private int pasajeros;
AutoCompacto(int numSerieMotor, int año, String marca,double precio)
{
super(numSerieMotor,año,marca,precio);
}
AutoCompacto(){}
public String toString()
{
return super.toString()+" "+pasajeros;
}
public int getCantidadPasajeros()
{
return pasajeros;
}
public void setCantidadPasajeros(int n)
{
pasajeros=n;
}
}
class AutoLujo extends Auto implements Pasajeros
{ private int pasajeros;
AutoLujo(int numSerieMotor, int año, String marca,double precio)
{
super(numSerieMotor,año,marca,precio);
}
public String toString()
{
return super.toString()+" "+pasajeros;
}
public int getCantidadPasajeros()
{
return pasajeros;
}
public void setCantidadPasajeros(int n)
{
pasajeros=n;
}
}
class Camioneta extends Auto
{
private int capacidadCarga;
private int cantidadEjes;
private int cantidadRodadas;
Camioneta (int numSerieMotor, int año, String marca,double precio,int
capacidadCarga,int cantidadEjes,
int cantidadRodadas)
{
super(numSerieMotor,año,marca,precio);
this.capacidadCarga=capacidadCarga;
this.cantidadEjes=cantidadEjes;
this.cantidadRodadas=cantidadRodadas;
}
int getCarga()
{
return capacidadCarga;
}
int getEjes()
{
return cantidadEjes;
}
int getRodadas()
{
return cantidadRodadas;
}
void setCarga(int n)
{
capacidadCarga=n;
}
void setEjes(int n)
{
cantidadEjes=n;
}
void setRodadas(int n)
{
cantidadRodadas=n;
}
public String toString()
{
return super.toString()+" "+capacidadCarga+" "+cantidadEjes+"
"+cantidadRodadas;
}
}
class Vagon extends Auto implements Pasajeros
{ private int pasajeros;
Vagon(int numSerieMotor, int año, String marca,double precio)
{
super(numSerieMotor,año,marca,precio);
}
public String toString()
{
return super.toString()+" "+pasajeros;
}
public int getCantidadPasajeros(){
return pasajeros;
}
public void setCantidadPasajeros(int n)
{
pasajeros=n;
}
}
public class Tarea22ago
{
public static void main(String[] args)
{
Auto x[]=new Auto[4];
x[1]=new AutoCompacto();//analogo a AutoLujo();
Scanner s=new Scanner(System.in);
System.out.println("Introduzca los datos del auto de lujo:"+"\n");
System.out.println("Introduzca el numero de serie del motor");
int a=s.nextInt();
System.out.println("Introduzca la marca");
String b=s.next();
System.out.println("Introduzca el año");
int c=s.nextInt();
System.out.println("Introduzca el precio");
double d=s.nextDouble();
System.out.println("Introduzca la cantidad de pasajeros");
int e=s.nextInt();
x[0]=new AutoLujo(a,c,b,d);
((AutoLujo)x[0]).setCantidadPasajeros(e);
System.out.println(x[0].toString());//xq ponerle el s.o.p, si return
//ya regresa la cadena?
System.out.println("Introduzca los datos del vagon:"+"\n");
System.out.println("Introduzca el numero de serie del motor");
int f=s.nextInt();
System.out.println("Introduzca la marca");
String g=s.next();
System.out.println("Introduzca el año");
int h=s.nextInt();
System.out.println("Introduzca el precio");
double i=s.nextDouble();
System.out.println("Introduzca la cantidad de pasajeros");
int j=s.nextInt();
x[2]=new Vagon(f,h,g,i);
((Vagon)x[2]).setCantidadPasajeros(j);
System.out.println(x[2].toString());
System.out.println("Introduzca los datos de la camioneta:"+"\n");
System.out.println("Introduzca el numero de serie del motor");
int k=s.nextInt();
System.out.println("Introduzca la marca");
String l=s.next();
System.out.println("Introduzca el año");
int m=s.nextInt();
System.out.println("Introduzca el precio");
double n=s.nextDouble();
System.out.println("Introduzca la cantidad de carga");
int o=s.nextInt();
System.out.println("Introduzca la cantidad de ejes");
int p=s.nextInt();
System.out.println("Introduzca la cantidad de rodadas");
int q=s.nextInt();
x[3]=new Camioneta(k,m,l,n,o,p,q);
System.out.println(x[3].toString());
}
}
To solve your problem it is necessary to implement two hierarchies of abstract classes, in your case they would be Vehicle and Car, of which Van and Car extend or inherit from Vehicle, with the Car class being the super class of AutoLuxury, CompactCar and Wagon in which they are implements the attribute to define the number of passengers. In this way the other classes are not contaminated with attributes and methods that they do not need to implement.
Code
An example of how you should create the instances of the different classes