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/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..7e317da --- /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) { + 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) { + } +} 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