Merge pull request #31 from TiagoRG/dev-tiagorg

[POO] aula04 ex1, ex2, ex3 added
This commit is contained in:
Tiago Garcia 2023-03-06 15:30:47 +00:00 committed by GitHub
commit 02254e9f27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 273 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,85 @@
package aula04;
class Product {
private final String name;
private final double price;
private final int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public double getTotalValue() {
return price * quantity;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
}
class CashRegister {
// TODO: completar implementação da classe
private final Product[] products = new Product[5];
public void addProduct(Product p) {
for (int i = 0; i < products.length; i++) {
if (products[i] == null) {
products[i] = p;
break;
}
}
}
public double getTotalValue() {
double total = 0;
for (Product product : products) {
if (product != null) {
total += product.getTotalValue();
}
}
return total;
}
public String toString() {
String result = String.format("%-15s %10s %10s %7s\n", "Product", "Price", "Quantity", "Total");
for (Product product : products) {
if (product != null) {
result += String.format("%-15s %10.2f %10d %7.2f\n", product.getName(), product.getPrice(), product.getQuantity(), product.getTotalValue());
}
}
result += String.format("%s %.2f\n", "Total value: ", getTotalValue());
return result;
}
}
public class CashRegisterDemo {
public static void main(String[] args) {
// Cria e adiciona 5 produtos
CashRegister cr = new CashRegister();
cr.addProduct(new Product("Book", 9.99, 3));
cr.addProduct(new Product("Pen", 1.99, 10));
cr.addProduct(new Product("Headphones", 29.99, 2));
cr.addProduct(new Product("Notebook", 19.99, 5));
cr.addProduct(new Product("Phone case", 5.99, 1));
// TODO: Listar o conteúdo e valor total
System.out.println(cr);
}
}

View File

@ -0,0 +1,118 @@
package aula04;
class Circle {
public double radius;
public Circle(double radius) {
assert radius > 0;
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
assert radius > 0;
this.radius = radius;
}
public boolean equals(Circle c2) {
return this.radius == c2.radius;
}
public String toString() {
return "Circle with radius " + this.radius;
}
public double getArea() {
return Math.PI * Math.pow(this.radius, 2);
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
}
class Triangle {
public double side1;
public double side2;
public double side3;
public Triangle(double side1, double side2, double side3) {
assert side1 > 0 && side2 > 0 && side3 > 0;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double[] getSides() {
return new double[] {this.side1, this.side2, this.side3};
}
public void setSides(double side1, double side2, double side3) {
assert side1 > 0 && side2 > 0 && side3 > 0;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public boolean equals(Triangle c2) {
return this.side1 == c2.side1 && this.side2 == c2.side2 && this.side3 == c2.side3;
}
public String toString() {
return "Triangle with sides " + this.side1 + ", " + this.side2 + ", " + this.side3;
}
public double getArea() {
double p = this.getPerimeter() / 2;
return Math.sqrt(p * (p - this.side1) * (p - this.side2) * (p - this.side3));
}
public double getPerimeter() {
return this.side1 + this.side2 + this.side3;
}
}
class Rectangle {
public double side1;
public double side2;
public Rectangle(double side1, double side2) {
assert side1 > 0 && side2 > 0;
this.side1 = side1;
this.side2 = side2;
}
public double[] getSides() {
return new double[] {this.side1, this.side2};
}
public void setSides(double side1, double side2) {
assert side1 > 0 && side2 > 0;
this.side1 = side1;
this.side2 = side2;
}
public boolean equals(Rectangle c2) {
return this.side1 == c2.side1 && this.side2 == c2.side2;
}
public String toString() {
return "Rectangle with sides " + this.side1 + ", " + this.side2;
}
public double getArea() {
return this.side1 * this.side2;
}
public double getPerimeter() {
return 2 * (this.side1 + this.side2);
}
}
public class Shapes {
public static void main(String[] args) {
}
}

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