[POO] aula04 ex3 added

This commit is contained in:
TiagoRG 2023-03-06 15:20:46 +00:00
parent 2f8ec9d8b1
commit 5297ca6829
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package aula04;
class Car {
public String make;
public String model;
public int year;
public int kms;
public Car(String make, String model, int year, int kms) {
this.make = make;
this.model = model;
this.year = year;
this.kms = kms;
}
public void drive(int distance) {
//TODO: acumular distância percorrida
this.kms += distance;
}
}

View File

@ -0,0 +1,49 @@
package aula04;
import java.util.Scanner;
public class SimpleCarDemo {
static Scanner sc = new Scanner(System.in);
static void listCars(Car[] cars) {
// TODO: lista todos os carros registados
// Exemplo de resultado
// Carros registados:
// Renault Megane Sport Tourer, 2015, kms: 35356
// Toyota Camry, 2010, kms: 32456
// Mercedes Vito, 2008, kms: 273891
System.out.println();
System.out.println("Carros registados: ");
for (int i=0; i<cars.length; i++) {
System.out.printf("%s %s, %d, kms: %d\n", cars[i].make, cars[i].model, cars[i].year, cars[i].kms);
}
System.out.println();
}
public static void main(String[] args) {
Car[] cars = new Car[3];// TODO: completar
cars[0] = new Car("Renault", "Megane Sport Tourer", 2015, 35356); // TODO: completar
cars[1] = new Car("Toyota", "Comry", 2010, 32456); // TODO: completar
cars[2] = new Car("Mercedes", "Vito", 2008, 273891); // TODO: completar
listCars(cars);
// Adicionar 10 viagens geradas aleatoriamente
for (int i=0; i<10; i++) {
int j = (int)Math.round(Math.random()*2); // escolhe um dos 3 carros
int kms = (int)Math.round(Math.random()*1000); // viagem até 1000 kms
System.out.printf("Carro %d viajou %d quilómetros.\n", j, kms);
// TODO: adicionar viagem ao carro j
cars[j].drive(kms);
}
listCars(cars);
sc.close();
}
}