From 1c74cbb006b7842b0e4e37a877496a5b33269aeb Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 6 Mar 2023 10:25:51 +0000 Subject: [PATCH 1/3] [POO] aula04 ex1, ex2 added --- .../poo/src/aula04/CashRegisterDemo.java | 85 +++++++++++++ 1ano/2semestre/poo/src/aula04/Shapes.java | 118 ++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 1ano/2semestre/poo/src/aula04/CashRegisterDemo.java create mode 100644 1ano/2semestre/poo/src/aula04/Shapes.java diff --git a/1ano/2semestre/poo/src/aula04/CashRegisterDemo.java b/1ano/2semestre/poo/src/aula04/CashRegisterDemo.java new file mode 100644 index 0000000..36f6c44 --- /dev/null +++ b/1ano/2semestre/poo/src/aula04/CashRegisterDemo.java @@ -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); + + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula04/Shapes.java b/1ano/2semestre/poo/src/aula04/Shapes.java new file mode 100644 index 0000000..af75682 --- /dev/null +++ b/1ano/2semestre/poo/src/aula04/Shapes.java @@ -0,0 +1,118 @@ +package aula04; + +class Circle { + public double radius; + + public Circle(double radius) { + if (radius <= 0) + return; + this.radius = radius; + } + + public double getRadius() { + return this.radius; + } + + public void setRadius(double radius) { + 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) { + if (side1 <= 0 || side2 <= 0 || side3 <= 0) + return; + 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) { + 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) { + if (side1 <= 0 || side2 <= 0) + return; + this.side1 = side1; + this.side2 = side2; + } + + public double[] getSides() { + return new double[] {this.side1, this.side2}; + } + + public void setSides(double side1, double side2) { + 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) { + } +} From 2f8ec9d8b1470fe48b82e944fd7f099642c69512 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 6 Mar 2023 15:20:20 +0000 Subject: [PATCH 2/3] [POO] Shapes.java asserts --- 1ano/2semestre/poo/src/aula04/Shapes.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1ano/2semestre/poo/src/aula04/Shapes.java b/1ano/2semestre/poo/src/aula04/Shapes.java index af75682..7e317da 100644 --- a/1ano/2semestre/poo/src/aula04/Shapes.java +++ b/1ano/2semestre/poo/src/aula04/Shapes.java @@ -4,8 +4,7 @@ class Circle { public double radius; public Circle(double radius) { - if (radius <= 0) - return; + assert radius > 0; this.radius = radius; } @@ -14,6 +13,7 @@ class Circle { } public void setRadius(double radius) { + assert radius > 0; this.radius = radius; } @@ -40,8 +40,7 @@ class Triangle { public double side3; public Triangle(double side1, double side2, double side3) { - if (side1 <= 0 || side2 <= 0 || side3 <= 0) - return; + assert side1 > 0 && side2 > 0 && side3 > 0; this.side1 = side1; this.side2 = side2; this.side3 = side3; @@ -52,6 +51,7 @@ class Triangle { } 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; @@ -80,8 +80,7 @@ class Rectangle { public double side2; public Rectangle(double side1, double side2) { - if (side1 <= 0 || side2 <= 0) - return; + assert side1 > 0 && side2 > 0; this.side1 = side1; this.side2 = side2; } @@ -91,6 +90,7 @@ class Rectangle { } public void setSides(double side1, double side2) { + assert side1 > 0 && side2 > 0; this.side1 = side1; this.side2 = side2; } From 5297ca6829a7a1b0d198a4bfb04723e0e0aa8147 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 6 Mar 2023 15:20:46 +0000 Subject: [PATCH 3/3] [POO] aula04 ex3 added --- 1ano/2semestre/poo/src/aula04/Car.java | 21 ++++++++ .../poo/src/aula04/SimpleCarDemo.java | 49 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 1ano/2semestre/poo/src/aula04/Car.java create mode 100644 1ano/2semestre/poo/src/aula04/SimpleCarDemo.java diff --git a/1ano/2semestre/poo/src/aula04/Car.java b/1ano/2semestre/poo/src/aula04/Car.java new file mode 100644 index 0000000..3b1076b --- /dev/null +++ b/1ano/2semestre/poo/src/aula04/Car.java @@ -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; + } + +} diff --git a/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java b/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java new file mode 100644 index 0000000..a11d768 --- /dev/null +++ b/1ano/2semestre/poo/src/aula04/SimpleCarDemo.java @@ -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