End chapter 13
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
House maison = new House("Route du stade 30", 5);
|
||||
House gp = new House(null, 0);
|
||||
Building hes = new Building("Rue de l'industrie");
|
||||
Building alpolEPFL = new Building();
|
||||
|
||||
System.out.println(maison.toString());
|
||||
System.out.println(gp.toString());
|
||||
System.out.println(hes.toString());
|
||||
System.out.println(alpolEPFL.toString());
|
||||
|
||||
gp.setAddress("Toulouse");
|
||||
gp.setnRooms(4);
|
||||
alpolEPFL.setAddress("Sion");
|
||||
|
||||
System.out.println(gp.toString());
|
||||
System.out.println(alpolEPFL.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public class Building {
|
||||
private String address;
|
||||
|
||||
Building(){
|
||||
|
||||
}
|
||||
Building(String address){
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
void setAddress(String address){
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Building.class.getName() + "@" + address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public class Car extends Vehicle{
|
||||
|
||||
Car(int year, int price) {
|
||||
super(year, price);
|
||||
}
|
||||
|
||||
@Override
|
||||
void start() {
|
||||
System.out.println("Vroum vroum");
|
||||
}
|
||||
|
||||
@Override
|
||||
void accelerate() {
|
||||
System.out.println("Vrooom");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public class House extends Building{
|
||||
private int nRooms;
|
||||
House(String address, int nRooms){
|
||||
super(address);
|
||||
this.nRooms = Math.abs(nRooms);
|
||||
}
|
||||
|
||||
public void setnRooms(int nRooms) {
|
||||
this.nRooms = Math.abs(nRooms);
|
||||
}
|
||||
public int getnRooms() {
|
||||
return nRooms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + "#" + nRooms;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
Car suzuki = new Car(2003, 4000);
|
||||
Truck tracteur = new Truck(2020, 12000);
|
||||
Car citroen = new Car(2006, 8000);
|
||||
|
||||
System.out.println(suzuki);
|
||||
System.out.println(tracteur);
|
||||
System.out.println(citroen);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public class Truck extends Vehicle{
|
||||
|
||||
Truck(int year, int price) {
|
||||
super(year, price);
|
||||
}
|
||||
|
||||
@Override
|
||||
void start() {
|
||||
System.out.println("Peuf-peuf");
|
||||
}
|
||||
|
||||
@Override
|
||||
void accelerate() {
|
||||
System.out.println("Broum");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package C13_Heritage;
|
||||
|
||||
public abstract class Vehicle {
|
||||
private static int nVehicle;
|
||||
private int matricule;
|
||||
private int year;
|
||||
private int price;
|
||||
|
||||
Vehicle(int year, int price){
|
||||
this.year = year;
|
||||
this.price = price;
|
||||
nVehicle++;
|
||||
this.matricule = nVehicle;
|
||||
}
|
||||
|
||||
abstract void start();
|
||||
abstract void accelerate();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vehicle " + matricule + ". Built in " + year + ", price: " + price;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user