[POO] aula07 ex1 and ex2 added
This commit is contained in:
parent
fe6d5d17bb
commit
824decc78c
|
@ -0,0 +1,43 @@
|
|||
package aula07.ex1;
|
||||
|
||||
public class Circle extends Shape {
|
||||
private double radius;
|
||||
|
||||
public Circle(double radius) {
|
||||
if (radius <= 0)
|
||||
throw new IllegalArgumentException("Sizes must be positive.");
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return this.radius;
|
||||
}
|
||||
|
||||
public void setRadius(double radius) {
|
||||
if (radius <= 0)
|
||||
throw new IllegalArgumentException("Sizes must be positive.");
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return Math.PI * Math.pow(this.radius, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPerimeter() {
|
||||
return 2 * Math.PI * this.radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Shape s2) {
|
||||
if (s2 instanceof Circle s2Circ)
|
||||
return this.radius == s2Circ.radius;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Circle with radius " + this.radius;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package aula07.ex1;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
public double side1;
|
||||
public double side2;
|
||||
|
||||
public Rectangle(double side1, double side2) {
|
||||
if (!(side1 > 0 && side2 > 0))
|
||||
throw new IllegalArgumentException("Sizes must be positive.");
|
||||
this.side1 = side1;
|
||||
this.side2 = side2;
|
||||
}
|
||||
|
||||
public double[] getSides() {
|
||||
return new double[] {this.side1, this.side2};
|
||||
}
|
||||
|
||||
public void setSides(double side1, double side2) {
|
||||
if (!(side1 > 0 && side2 > 0))
|
||||
throw new IllegalArgumentException("Sizes must be positive.");
|
||||
this.side1 = side1;
|
||||
this.side2 = side2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Shape s2) {
|
||||
if (s2 instanceof Rectangle s2Rect)
|
||||
return this.side1 == s2Rect.side1 && this.side2 == s2Rect.side2;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Rectangle with sides " + this.side1 + ", " + this.side2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return this.side1 * this.side2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPerimeter() {
|
||||
return 2 * (this.side1 + this.side2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package aula07.ex1;
|
||||
|
||||
public abstract class Shape {
|
||||
public abstract boolean equals(Shape c2);
|
||||
public abstract String toString();
|
||||
public abstract double getArea();
|
||||
public abstract double getPerimeter();
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package aula07.ex1;
|
||||
|
||||
public class Triangle extends Shape {
|
||||
private double side1;
|
||||
private double side2;
|
||||
private double side3;
|
||||
|
||||
public Triangle(double side1, double side2, double side3) {
|
||||
if (!(side1 > 0 && side2 > 0 && side3 > 0))
|
||||
throw new IllegalArgumentException("Sizes must be positive.");
|
||||
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) {
|
||||
if (!(side1 > 0 && side2 > 0 && side3 > 0))
|
||||
throw new IllegalArgumentException("Sizes must be positive.");
|
||||
this.side1 = side1;
|
||||
this.side2 = side2;
|
||||
this.side3 = side3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Shape s2) {
|
||||
if (s2 instanceof Triangle s2Triang)
|
||||
return this.side1 == s2Triang.side1 && this.side2 == s2Triang.side2 && this.side3 == s2Triang.side3;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Triangle with sides " + this.side1 + ", " + this.side2 + ", " + this.side3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
double p = this.getPerimeter() / 2;
|
||||
return Math.sqrt(p * (p - this.side1) * (p - this.side2) * (p - this.side3));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPerimeter() {
|
||||
return this.side1 + this.side2 + this.side3;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package aula07.ex2;
|
||||
|
||||
public abstract class Date {
|
||||
public abstract int getAbsDay();
|
||||
public abstract int getDay();
|
||||
public abstract int getMonth();
|
||||
public abstract int getYear();
|
||||
public abstract void increment();
|
||||
public abstract void decrement();
|
||||
public abstract void addDays(int days);
|
||||
public abstract void removeDays(int days);
|
||||
|
||||
|
||||
public static int monthDays(int month, int year) {
|
||||
if (!validMonth(month))
|
||||
return -1;
|
||||
int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
if (month == 2 && isLeapYear(year))
|
||||
return 29;
|
||||
return daysPerMonth[month - 1];
|
||||
}
|
||||
|
||||
public static boolean validMonth(int month) {
|
||||
return month >= 1 && month <= 12;
|
||||
}
|
||||
|
||||
public static boolean validDate(int day, int month, int year) {
|
||||
return day >= 1 && day <= monthDays(month, year);
|
||||
}
|
||||
|
||||
public static boolean isLeapYear(int year) {
|
||||
return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package aula07.ex2;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class DateND extends Date {
|
||||
private int absDay;
|
||||
|
||||
public DateND(int absDay) {
|
||||
set(absDay);
|
||||
}
|
||||
|
||||
public void set(int absDay) {
|
||||
if (absDay <= 0)
|
||||
throw new IllegalArgumentException("Absolute day must be positive!");
|
||||
this.absDay = absDay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAbsDay() {
|
||||
return this.absDay;
|
||||
}
|
||||
|
||||
private int[] getYMD() {
|
||||
int[] date = new int[3];
|
||||
|
||||
int tempAbsDay = this.absDay;
|
||||
int year = 1999;
|
||||
while (tempAbsDay > 0) {
|
||||
year++;
|
||||
tempAbsDay -= isLeapYear(year) ? 366 : 365;
|
||||
}
|
||||
date[2] = year;
|
||||
tempAbsDay += isLeapYear(year) ? 366 : 365;
|
||||
|
||||
int month = 0;
|
||||
while (tempAbsDay > 0) {
|
||||
month++;
|
||||
tempAbsDay -= monthDays(month, year);
|
||||
}
|
||||
date[1] = month;
|
||||
tempAbsDay += monthDays(month, year);
|
||||
date[0] = tempAbsDay;
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDay() {
|
||||
return this.getYMD()[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMonth() {
|
||||
return this.getYMD()[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getYear() {
|
||||
return this.getYMD()[2];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void increment() {
|
||||
this.absDay++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrement() {
|
||||
this.absDay--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDays(int days) {
|
||||
for (int i = 0; i < days; i++)
|
||||
this.increment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeDays(int days) {
|
||||
if (days > this.absDay)
|
||||
this.absDay = 0;
|
||||
else
|
||||
for (int i = 0; i < days; i++)
|
||||
this.decrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Date: %d past 01-01-2000", this.absDay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
DateND dateND = (DateND) o;
|
||||
return absDay == dateND.absDay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(absDay);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package aula07.ex2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class DateTest {
|
||||
public static void main(String[] args) {
|
||||
Scanner sin = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
System.out.print("Class to test (0-Quit;1-DateYMD;2-DateND): ");
|
||||
String option = sin.nextLine();
|
||||
|
||||
switch (option) {
|
||||
case "0" -> {
|
||||
sin.close();
|
||||
System.exit(0);
|
||||
}
|
||||
case "1" -> {
|
||||
System.out.print("Day: ");
|
||||
int day = Integer.parseInt(sin.nextLine());
|
||||
System.out.print("Month: ");
|
||||
int month = Integer.parseInt(sin.nextLine());
|
||||
System.out.print("Year: ");
|
||||
int year = Integer.parseInt(sin.nextLine());
|
||||
|
||||
DateYMD date = new DateYMD(day, month, year);
|
||||
System.out.printf("Initial date: %s%n%n", date);
|
||||
System.out.printf("Absolute day: %d%n", date.getAbsDay());
|
||||
System.out.println("Adding 7 days...");
|
||||
date.addDays(7);
|
||||
System.out.printf("New absolute day: %d%n", date.getAbsDay());
|
||||
System.out.println("Removing 534 days...");
|
||||
date.removeDays(534);
|
||||
System.out.printf("New absolute day: %d%n", date.getAbsDay());
|
||||
System.out.println("Final date: " + date);
|
||||
}
|
||||
case "2" -> {
|
||||
System.out.print("Days past past 01-01-2000: ");
|
||||
int days = Integer.parseInt(sin.nextLine());
|
||||
|
||||
DateND date = new DateND(days);
|
||||
System.out.printf("Initial date: %s%n%n", date);
|
||||
System.out.printf("YMD date: %s%n", new DateYMD(date.getDay(), date.getMonth(), date.getYear()));
|
||||
System.out.println("Adding 74 days...");
|
||||
date.addDays(74);
|
||||
System.out.printf("New YMD date: %s%n", new DateYMD(date.getDay(), date.getMonth(), date.getYear()));
|
||||
System.out.println("Removing 612 days...");
|
||||
date.removeDays(612);
|
||||
System.out.printf("New YMD date: %s%n", new DateYMD(date.getDay(), date.getMonth(), date.getYear()));
|
||||
System.out.println("Final date: " + date);
|
||||
}
|
||||
default -> System.out.println("Invalid option.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
package aula07.ex2;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class DateYMD extends Date {
|
||||
private int day;
|
||||
private int month;
|
||||
private int year;
|
||||
|
||||
public DateYMD(int day, int month, int year) {
|
||||
set(day, month, year);
|
||||
}
|
||||
|
||||
public void set(int day, int month, int year) {
|
||||
if (!validDate(day, month, year))
|
||||
throw new IllegalArgumentException("Invalid date");
|
||||
this.day = day;
|
||||
this.month = month;
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAbsDay() {
|
||||
int absDay = 0;
|
||||
for (int i = 2000; i < this.year; i++) {
|
||||
if (isLeapYear(i))
|
||||
absDay += 366;
|
||||
else
|
||||
absDay += 365;
|
||||
}
|
||||
for (int i = 1; i < this.month; i++) {
|
||||
absDay += monthDays(i, this.year);
|
||||
}
|
||||
return absDay + this.day;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDay() {
|
||||
return this.day;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMonth() {
|
||||
return this.month;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getYear() {
|
||||
return this.year;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void increment() {
|
||||
if (this.day < monthDays(this.month, this.year))
|
||||
this.day++;
|
||||
else {
|
||||
this.day = 1;
|
||||
if (this.month < 12)
|
||||
this.month++;
|
||||
else {
|
||||
this.month = 1;
|
||||
this.year++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrement() {
|
||||
if (this.day > 1)
|
||||
this.day--;
|
||||
else {
|
||||
this.day = monthDays(this.month == 1 ? 12 : this.month-1, this.year);
|
||||
if (this.month > 1)
|
||||
this.month--;
|
||||
else {
|
||||
this.month = 12;
|
||||
this.year--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDays(int days) {
|
||||
for (int i = 0; i < days; i++)
|
||||
this.increment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeDays(int days) {
|
||||
for (int i = 0; i < days; i++)
|
||||
this.decrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%02d-%02d-%04d", this.day, this.month, this.year);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
DateYMD dateYMD = (DateYMD) o;
|
||||
return day == dateYMD.day && month == dateYMD.month && year == dateYMD.year;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(day, month, year);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue