From 4d628f539dd61ce30dd9f07ccd0d693fd8ea3641 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 7 Mar 2023 11:49:43 +0000 Subject: [PATCH 1/3] [POO] aula04 finished --- 1ano/2semestre/poo/src/aula04/Car.java | 4 + 1ano/2semestre/poo/src/aula04/CarDemo.java | 102 ++++++++++++++++++ 1ano/2semestre/poo/src/aula04/Shapes.java | 79 ++++++++++++++ .../poo/src/aula04/SimpleCarDemo.java | 4 +- 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula04/CarDemo.java diff --git a/1ano/2semestre/poo/src/aula04/Car.java b/1ano/2semestre/poo/src/aula04/Car.java index 3b1076b..51d6146 100644 --- a/1ano/2semestre/poo/src/aula04/Car.java +++ b/1ano/2semestre/poo/src/aula04/Car.java @@ -18,4 +18,8 @@ class Car { this.kms += distance; } + public String toString() { + return String.format("%s %s, %d, kms: %d", this.make, this.model, this.year, this.kms); + } + } diff --git a/1ano/2semestre/poo/src/aula04/CarDemo.java b/1ano/2semestre/poo/src/aula04/CarDemo.java new file mode 100644 index 0000000..cef216e --- /dev/null +++ b/1ano/2semestre/poo/src/aula04/CarDemo.java @@ -0,0 +1,102 @@ +package aula04; + +import java.util.Arrays; +import java.util.Scanner; + +public class CarDemo { + + static Scanner sc = new Scanner(System.in); + + static int registerCars(Car[] cars) { + // TODO: pede dados dos carros ao utilizador e acrescenta ao vetor + // registo de carros termina quando o utilizador inserir uma linha vazia + // devolve número de carros registados + for (int i = 0; i < 10; i++) { + boolean validData = false; + while (!validData) { + System.out.print("Insira dados do carro (marca modelo ano quilómetros): "); + String input = sc.nextLine(); + if (input.isEmpty()) return i; + String[] parts = input.split(" "); + validData = validateData(parts); + if (!validData) { + System.out.println("Dados mal formatados. Tente novamente."); + } else { + String model = String.join(" ", Arrays.stream(parts, 1, parts.length - 2).toArray(String[]::new)); + cars[i] = new Car(parts[0], model, Integer.parseInt(parts[parts.length-2]), Integer.parseInt(parts[parts.length-1])); + } + } + } + return 10; + } + + static boolean validateData(String[] parts) { + if (parts.length < 4) return false; + try { + Integer.parseInt(parts[parts.length-1]); + if (String.format("%d", Integer.parseInt(parts[parts.length-2])).length() != 4) return false; + } catch (NumberFormatException e) { + return false; + } + return true; + } + + static void registerTrips(Car[] cars, int numCars) { + // TODO: pede dados das viagens ao utilizador e atualiza informação do carro + // registo de viagens termina quando o utilizador inserir uma linha vazia + String input; + while (true) { + System.out.print("Registe uma viagem no formato \"carro:distância\": "); + if ((input = sc.nextLine()).isEmpty()) break; + String[] parts = input.split(":"); + if (parts.length != 2) { + System.out.println("Dados mal formatados. Tente novamente."); + } else { + try { + int car = Integer.parseInt(parts[0]); + int distance = Integer.parseInt(parts[1]); + if (car < 0 || car >= numCars || distance <= 0) { + System.out.println("Dados mal formatados. Tente novamente."); + } else { + cars[car].drive(distance); + } + } catch (NumberFormatException e) { + System.out.println("Dados mal formatados. Tente novamente."); + } + } + } + } + + + static void listCars(Car[] cars) { + System.out.println("\nCarros registados: "); + // TODO: lista todos os carros registados + // Exemplo de resultado + // Carros registados: + // Toyota Camry, 2010, kms: 234346 + // Renault Megane Sport Tourer, 2015, kms: 32536 + for (Car car : cars) { + if (car != null) { + System.out.println(car); + } + } + + System.out.println("\n"); + } + + public static void main(String[] args) { + + Car[] cars = new Car[10]; + + int numCars = registerCars(cars); + + if (numCars>0) { + listCars(cars); + registerTrips(cars, numCars); + listCars(cars); + } + + sc.close(); + + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula04/Shapes.java b/1ano/2semestre/poo/src/aula04/Shapes.java index 7e317da..e35f54e 100644 --- a/1ano/2semestre/poo/src/aula04/Shapes.java +++ b/1ano/2semestre/poo/src/aula04/Shapes.java @@ -1,5 +1,9 @@ package aula04; +import utils.UserInput; + +import java.util.Scanner; + class Circle { public double radius; @@ -41,6 +45,10 @@ class Triangle { public Triangle(double side1, double side2, double side3) { assert side1 > 0 && side2 > 0 && side3 > 0; + if((Math.abs(side2 - side3) < side1 || side1 < side2 + side3 + || Math.abs(side1 - side3) < side2 || side2 < side1 + side3 + || Math.abs(side1 - side2) < side3 || side3 < side1 + side2)) + throw new IllegalArgumentException("Triangle cannot be created with those sides."); this.side1 = side1; this.side2 = side2; this.side3 = side3; @@ -52,6 +60,10 @@ class Triangle { public void setSides(double side1, double side2, double side3) { assert side1 > 0 && side2 > 0 && side3 > 0; + if((Math.abs(side2 - side3) < side1 || side1 < side2 + side3 + || Math.abs(side1 - side3) < side2 || side2 < side1 + side3 + || Math.abs(side1 - side2) < side3 || side3 < side1 + side2)) + throw new IllegalArgumentException("Triangle cannot be created with those sides."); this.side1 = side1; this.side2 = side2; this.side3 = side3; @@ -114,5 +126,72 @@ class Rectangle { public class Shapes { public static void main(String[] args) { + Scanner sin = new Scanner(System.in); + + // Circle + + System.out.print("Raio do circulo 1: "); + double radius = UserInput.getPositiveNumber(sin); + Circle c1 = new Circle(radius); + System.out.print("Raio do circulo 2: "); + radius = UserInput.getPositiveNumber(sin); + Circle c2 = new Circle(radius); + + System.out.println("Circulo 1: " + c1); + System.out.println("Area do circulo 1: " + c1.getArea()); + System.out.println("Perimetro do circulo 1: " + c1.getPerimeter()); + System.out.println("Circulo 2: " + c2); + System.out.println("Area do circulo 2: " + c2.getArea()); + System.out.println("Perimetro do circulo 2: " + c2.getPerimeter()); + System.out.println("Circulo 1 e igual ao circulo 2? " + c1.equals(c2)); + + + // Triangle + + System.out.print("\n\n\nLado 1 do triangulo 1: "); + double side1 = UserInput.getPositiveNumber(sin); + System.out.print("Lado 2 do triangulo 1: "); + double side2 = UserInput.getPositiveNumber(sin); + System.out.print("Lado 3 do triangulo 1: "); + double side3 = UserInput.getPositiveNumber(sin); + Triangle t1 = new Triangle(side1, side2, side3); + System.out.print("Lado 1 do triangulo 2: "); + side1 = UserInput.getPositiveNumber(sin); + System.out.print("Lado 2 do triangulo 2: "); + side2 = UserInput.getPositiveNumber(sin); + System.out.print("Lado 3 do triangulo 2: "); + side3 = UserInput.getPositiveNumber(sin); + Triangle t2 = new Triangle(side1, side2, side3); + + System.out.println("Triangulo 1: " + t1); + System.out.println("Area do triangulo 1: " + t1.getArea()); + System.out.println("Perimetro do triangulo 1: " + t1.getPerimeter()); + System.out.println("Triangulo 2: " + t2); + System.out.println("Area do triangulo 2: " + t2.getArea()); + System.out.println("Perimetro do triangulo 2: " + t2.getPerimeter()); + System.out.println("Triangulo 1 e igual ao triangulo 2? " + t1.equals(t2)); + + + // Rectangle + System.out.print("\n\n\nLado 1 do retangulo 1: "); + side1 = UserInput.getPositiveNumber(sin); + System.out.print("Lado 2 do retangulo 1: "); + side2 = UserInput.getPositiveNumber(sin); + Rectangle r1 = new Rectangle(side1, side2); + System.out.print("Lado 1 do retangulo 2: "); + side1 = UserInput.getPositiveNumber(sin); + System.out.print("Lado 2 do retangulo 2: "); + side2 = UserInput.getPositiveNumber(sin); + Rectangle r2 = new Rectangle(side1, side2); + + System.out.println("Retangulo 1: " + r1); + System.out.println("Area do retangulo 1: " + r1.getArea()); + System.out.println("Perimetro do retangulo 1: " + r1.getPerimeter()); + System.out.println("Retangulo 2: " + r2); + System.out.println("Area do retangulo 2: " + r2.getArea()); + System.out.println("Perimetro do retangulo 2: " + r2.getPerimeter()); + System.out.println("Retangulo 1 e igual ao retangulo 2? " + r1.equals(r2)); + + sin.close(); } } diff --git a/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java b/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java index a11d768..5fe3eaa 100644 --- a/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java +++ b/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java @@ -16,8 +16,8 @@ public class SimpleCarDemo { System.out.println(); System.out.println("Carros registados: "); - for (int i=0; i Date: Tue, 7 Mar 2023 11:53:37 +0000 Subject: [PATCH 2/3] [POO] Removed unnecessary parenthesis --- 1ano/2semestre/poo/src/aula04/Shapes.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1ano/2semestre/poo/src/aula04/Shapes.java b/1ano/2semestre/poo/src/aula04/Shapes.java index e35f54e..e37cf6d 100644 --- a/1ano/2semestre/poo/src/aula04/Shapes.java +++ b/1ano/2semestre/poo/src/aula04/Shapes.java @@ -45,9 +45,9 @@ class Triangle { public Triangle(double side1, double side2, double side3) { assert side1 > 0 && side2 > 0 && side3 > 0; - if((Math.abs(side2 - side3) < side1 || side1 < side2 + side3 + if(Math.abs(side2 - side3) < side1 || side1 < side2 + side3 || Math.abs(side1 - side3) < side2 || side2 < side1 + side3 - || Math.abs(side1 - side2) < side3 || side3 < side1 + side2)) + || Math.abs(side1 - side2) < side3 || side3 < side1 + side2) throw new IllegalArgumentException("Triangle cannot be created with those sides."); this.side1 = side1; this.side2 = side2; @@ -60,9 +60,9 @@ class Triangle { public void setSides(double side1, double side2, double side3) { assert side1 > 0 && side2 > 0 && side3 > 0; - if((Math.abs(side2 - side3) < side1 || side1 < side2 + side3 + if(Math.abs(side2 - side3) < side1 || side1 < side2 + side3 || Math.abs(side1 - side3) < side2 || side2 < side1 + side3 - || Math.abs(side1 - side2) < side3 || side3 < side1 + side2)) + || Math.abs(side1 - side2) < side3 || side3 < side1 + side2) throw new IllegalArgumentException("Triangle cannot be created with those sides."); this.side1 = side1; this.side2 = side2; From f0450842cebd17406047528b293e266f10c75c84 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 7 Mar 2023 13:21:48 +0000 Subject: [PATCH 3/3] [POO] README for aula04 added --- 1ano/2semestre/poo/src/aula04/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 1ano/2semestre/poo/src/aula04/README.md diff --git a/1ano/2semestre/poo/src/aula04/README.md b/1ano/2semestre/poo/src/aula04/README.md new file mode 100755 index 0000000..57befe6 --- /dev/null +++ b/1ano/2semestre/poo/src/aula04/README.md @@ -0,0 +1,16 @@ +# Programação Orientada a Objetos +## Aula 04 +### Tópico principal da aula: Classes + +* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula04.pdf) + +### Exercise List +| Exercise Number | File Name | +|-----------------|----------------------------------------------------------------------------------------------------------------------------------| +| 1 | [Shapes.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula04/Shapes.java) | +| 2 | [CashRegisterDemo.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula04/CashRegisterDemo.java) | +| 3 | [SimpleCarDemo.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java) | +| 4 | [CarDemo.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula04/CarDemo.java) | +* [Car.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula04/Car.java) • Class de suporte para os exercícios 3 e 4. +--- +*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)