Classe Carro :
package Carro.Model;
public class Car {
private String name;
private int year1,year2,year3;
private double price1,price2,price3;
public Car(String name, int year1, int year2, int year3, double price1, double price2, double price3) {
this.name = name;
this.year1 = year1;
this.year2 = year2;
this.year3 = year3;
this.price1 = price1;
this.price2 = price2;
this.price3 = price3;
}
public String showMostAndLessExpensive(){
double mostExpensive = Math.max(price1,Math.max(price2,price3));
double cheapiest = Math.min(price1,Math.min(price2,price3));
return """
Most expensive car : %.2f
Cheapiest car : %.2f
%n""".formatted(mostExpensive,cheapiest);
}
public void showCarInfo(){
String carPrice = showMostAndLessExpensive();
System.out.printf("""
Car name : %s
Years models available and prices :
%d .Price : %.2f
%d .Price : %.2f
%d .Price : %.2f
""", this.name,this.year1,this.price1,this.year2,this.price2,this.year3,this.price3);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice1() {
return price1;
}
public void setPrice1(double price1) {
this.price1 = price1;
}
public double getPrice2() {
return price2;
}
public void setPrice2(double price2) {
this.price2 = price2;
}
public double getPrice3() {
return price3;
}
public void setPrice3(double price3) {
this.price3 = price3;
}
}
Modelo carro :
package Carro.Model;
public class CarModel extends Car{
public CarModel(String name,int year1,double price1,int year2,double price2,
int year3,double price3)
{
super(name,year1,year2,year3,price1,price2,price3);
}
}
Main :
package Carro;
import Carro.Model.Car;
import Carro.Model.CarModel;
public class Main {
public static void main(String[] args) {
CarModel polo = new CarModel("polo",2020,75000,2021,80000,2022,90000);
polo.showCarInfo();
System.out.println(polo.showMostAndLessExpensive());
}
}