[POO] aula09 added
This commit is contained in:
parent
87d82c4e6d
commit
8cb4a02a0d
|
@ -0,0 +1,42 @@
|
||||||
|
package aula09.ex1;
|
||||||
|
|
||||||
|
import aula06.ex1.Person;
|
||||||
|
import utils.DateYMD;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ALDemo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ArrayList<Integer> c1= new ArrayList<>();
|
||||||
|
for(int i= 10; i<= 100; i+=10) c1.add(i);System.out.println("Size: "+ c1.size());
|
||||||
|
|
||||||
|
for(int i= 0; i< c1.size(); i++)
|
||||||
|
System.out.println("Elemento: "+ c1.get(i));
|
||||||
|
|
||||||
|
ArrayList<String> c2= new ArrayList<>();
|
||||||
|
c2.add("Vento");
|
||||||
|
c2.add("Calor");
|
||||||
|
c2.add("Frio");
|
||||||
|
c2.add("Chuva");
|
||||||
|
System.out.println(c2);
|
||||||
|
Collections.sort(c2);
|
||||||
|
System.out.println(c2);
|
||||||
|
c2.remove("Frio");
|
||||||
|
c2.remove(0);
|
||||||
|
System.out.println(c2);
|
||||||
|
|
||||||
|
Set<Person> c3 = new HashSet<>();
|
||||||
|
c3.add(new Person("Ana", 12345678, new DateYMD(1, 1, 2000)));
|
||||||
|
c3.add(new Person("Joao", 42342289, new DateYMD(1, 1, 2000)));
|
||||||
|
c3.add(new Person("Maria", 12346789, new DateYMD(1, 1, 2000)));
|
||||||
|
c3.add(new Person("Marco", 12356789, new DateYMD(1, 1, 2000)));
|
||||||
|
c3.add(new Person("Ana", 12346789, new DateYMD(1, 1, 2000)));
|
||||||
|
|
||||||
|
for (Person p : c3) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package aula09.ex2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class CollectionTester {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int DIM= 5000;
|
||||||
|
Collection<Integer> col= new ArrayList<>();
|
||||||
|
checkPerformance(col, DIM);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkPerformance(Collection<Integer> col, int DIM) {
|
||||||
|
double start, stop, delta;
|
||||||
|
// Add
|
||||||
|
start = System.nanoTime(); // clock snapshot before
|
||||||
|
for(int i=0; i<DIM; i++)
|
||||||
|
col.add(i);
|
||||||
|
stop = System.nanoTime(); // clock snapshot after
|
||||||
|
delta = (stop-start)/1e6; // convert to milliseconds
|
||||||
|
System.out.println(col.size()+ ": Add to "+col.getClass().getSimpleName() +" took "+ delta+ "ms");
|
||||||
|
|
||||||
|
// Search
|
||||||
|
start = System.nanoTime(); // clock snapshot before
|
||||||
|
for(int i=0; i<DIM; i++) {
|
||||||
|
int n = (int) (Math.random()*DIM);
|
||||||
|
if(!col.contains(n))
|
||||||
|
System.out.println("Not found???"+n);
|
||||||
|
}
|
||||||
|
stop = System.nanoTime(); // clock snapshot after
|
||||||
|
delta = (stop-start)/1e6; // convert nanoseconds to milliseconds
|
||||||
|
System.out.println(col.size()+ ": Search to "+ col.getClass().getSimpleName() +" took "+ delta+ "ms");
|
||||||
|
|
||||||
|
// Remove
|
||||||
|
start = System.nanoTime(); // clock snapshot before
|
||||||
|
Iterator<Integer> iterator = col.iterator();
|
||||||
|
while(iterator.hasNext()) {
|
||||||
|
iterator.next();
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
stop = System.nanoTime(); // clock snapshot after
|
||||||
|
delta = (stop-start)/1e6; // convert nanoseconds to milliseconds
|
||||||
|
System.out.println(col.size() + ": Remove from "+ col.getClass().getSimpleName() +" took "+ delta+ "ms");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package aula09.ex3;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class CommercialPlane extends Plane {
|
||||||
|
private int numOfCrewMembers;
|
||||||
|
|
||||||
|
public CommercialPlane(String id, String manufacturer, String model, int year, int maxNumOfPassengers, double maxSpeed, int numOfCrewMembers) {
|
||||||
|
super(id, manufacturer, model, year, maxNumOfPassengers, maxSpeed);
|
||||||
|
this.numOfCrewMembers = numOfCrewMembers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumOfCrewMembers() {
|
||||||
|
return this.numOfCrewMembers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumOfCrewMembers(int numOfCrewMembers) {
|
||||||
|
this.numOfCrewMembers = numOfCrewMembers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlaneType() {
|
||||||
|
return "Commercial";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CommercialPlane {" +
|
||||||
|
"\n\tid='" + super.getId() + '\'' +
|
||||||
|
",\n\tmanufacturer='" + super.getManufacturer() + '\'' +
|
||||||
|
",\n\tmodel='" + super.getModel() + '\'' +
|
||||||
|
",\n\tproductionYear=" + super.getProductionYear() +
|
||||||
|
",\n\tmaxPassengers=" + super.getMaxPassengers() +
|
||||||
|
",\n\tmaxSpeed=" + super.getMaxSpeed() +
|
||||||
|
",\n\tnumOfCrewMembers=" + this.getNumOfCrewMembers() +
|
||||||
|
",\n}";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof CommercialPlane plane)) return false;
|
||||||
|
if (!super.equals(o)) return false;
|
||||||
|
return this.getNumOfCrewMembers() == plane.getNumOfCrewMembers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(super.hashCode(), this.getNumOfCrewMembers());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package aula09.ex3;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class MilitaryPlane extends Plane {
|
||||||
|
private int numMissiles;
|
||||||
|
|
||||||
|
public MilitaryPlane(String id, String manufacturer, String model, int year, int maxPassengers, double maxSpeed, int numMissiles) {
|
||||||
|
super(id, manufacturer, model, year, maxPassengers, maxSpeed);
|
||||||
|
this.numMissiles = numMissiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumMissiles() {
|
||||||
|
return this.numMissiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumMissiles(int numMissiles) {
|
||||||
|
this.numMissiles = numMissiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlaneType() {
|
||||||
|
return "Military";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MilitaryPlane {" +
|
||||||
|
"\n\tid='" + super.getId() + '\'' +
|
||||||
|
",\n\tmanufacturer='" + super.getManufacturer() + '\'' +
|
||||||
|
",\n\tmodel='" + super.getModel() + '\'' +
|
||||||
|
",\n\tproductionYear=" + super.getProductionYear() +
|
||||||
|
",\n\tmaxPassengers=" + super.getMaxPassengers() +
|
||||||
|
",\n\tmaxSpeed=" + super.getMaxSpeed() +
|
||||||
|
",\n\tnumMissiles=" + this.getNumMissiles() +
|
||||||
|
",\n}";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof MilitaryPlane plane)) return false;
|
||||||
|
if (!super.equals(o)) return false;
|
||||||
|
return this.getNumMissiles() == plane.getNumMissiles();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(super.hashCode(), this.getNumMissiles());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package aula09.ex3;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Plane {
|
||||||
|
private final String id;
|
||||||
|
private String manufacturer;
|
||||||
|
private String model;
|
||||||
|
private int productionYear;
|
||||||
|
private int maxPassengers;
|
||||||
|
private double maxSpeed;
|
||||||
|
|
||||||
|
public Plane(String id, String manufacturer, String model, int productionYear, int maxPassengers, double maxSpeed) {
|
||||||
|
this.id = id;
|
||||||
|
this.manufacturer = manufacturer;
|
||||||
|
this.model = model;
|
||||||
|
this.productionYear = productionYear;
|
||||||
|
this.maxPassengers = maxPassengers;
|
||||||
|
this.maxSpeed = maxSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getManufacturer() {
|
||||||
|
return manufacturer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setManufacturer(String manufacturer) {
|
||||||
|
this.manufacturer = manufacturer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModel(String model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getProductionYear() {
|
||||||
|
return productionYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProductionYear(int productionYear) {
|
||||||
|
this.productionYear = productionYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxPassengers() {
|
||||||
|
return maxPassengers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxPassengers(int maxPassengers) {
|
||||||
|
this.maxPassengers = maxPassengers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMaxSpeed() {
|
||||||
|
return maxSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxSpeed(double maxSpeed) {
|
||||||
|
this.maxSpeed = maxSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Plane {" +
|
||||||
|
"\n\tid='" + id + '\'' +
|
||||||
|
",\n\tmanufacturer='" + manufacturer + '\'' +
|
||||||
|
",\n\tmodel='" + model + '\'' +
|
||||||
|
",\n\tproductionYear=" + productionYear +
|
||||||
|
",\n\tmaxPassengers=" + maxPassengers +
|
||||||
|
",\n\tmaxSpeed=" + maxSpeed +
|
||||||
|
",\n}";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Plane plane)) return false;
|
||||||
|
return this.getProductionYear() == plane.getProductionYear() && this.getMaxPassengers() == plane.getMaxPassengers() && Double.compare(plane.getMaxSpeed(), this.getMaxSpeed()) == 0 && Objects.equals(this.getId(), plane.getId()) && Objects.equals(this.getManufacturer(), plane.getManufacturer()) && Objects.equals(this.getModel(), plane.getModel());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(this.getId(), this.getManufacturer(), this.getModel(), this.getProductionYear(), this.getMaxPassengers(), this.getMaxSpeed());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package aula09.ex3;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class PlaneManager {
|
||||||
|
private final LinkedList<Plane> planes = new LinkedList<>();
|
||||||
|
|
||||||
|
public void addPlane(Plane plane) {
|
||||||
|
planes.add(plane);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removePlane(String id) {
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
if (plane.getId().equals(id)) {
|
||||||
|
planes.remove(plane);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Plane searchPlane(String id) {
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
if (plane.getId().equals(id)) {
|
||||||
|
return plane;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LinkedList<Plane> getCommercialPlanes() {
|
||||||
|
LinkedList<Plane> commercialPlanes = new LinkedList<>();
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
if (plane instanceof CommercialPlane) {
|
||||||
|
commercialPlanes.add(plane);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return commercialPlanes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LinkedList<Plane> getMilitaryPlanes() {
|
||||||
|
LinkedList<Plane> militaryPlanes = new LinkedList<>();
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
if (plane instanceof MilitaryPlane) {
|
||||||
|
militaryPlanes.add(plane);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return militaryPlanes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Plane getFastestPlane() {
|
||||||
|
Plane fastestPlane = null;
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
if (fastestPlane == null || plane.getMaxSpeed() > fastestPlane.getMaxSpeed()) {
|
||||||
|
fastestPlane = plane;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fastestPlane;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printAllPlanes() {
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printAllPlanes(String type) {
|
||||||
|
for (Plane plane : planes) {
|
||||||
|
if (plane instanceof CommercialPlane && type.equals("commercial")) {
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
else if (plane instanceof MilitaryPlane && type.equals("military")) {
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,104 @@
|
||||||
|
package aula09.ex3;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class PlaneTester {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
PlaneManager planeManager = new PlaneManager();
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int choice;
|
||||||
|
|
||||||
|
do {
|
||||||
|
System.out.println("\nPlane Fleet Menu:");
|
||||||
|
System.out.println("1. Add a plane to the fleet");
|
||||||
|
System.out.println("2. Remove a plane from the fleet");
|
||||||
|
System.out.println("3. Search for a plane");
|
||||||
|
System.out.println("4. Print summary of all planes in the fleet");
|
||||||
|
System.out.println("5. Print list of all commercial planes in the fleet");
|
||||||
|
System.out.println("6. Print list of all military planes in the fleet");
|
||||||
|
System.out.println("7. Print the fastest plane in the fleet");
|
||||||
|
System.out.println("0. Exit");
|
||||||
|
|
||||||
|
System.out.print("Enter your choice: ");
|
||||||
|
choice = Integer.parseInt(scanner.nextLine());
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
case 1 -> addPlane(planeManager, scanner);
|
||||||
|
case 2 -> removePlane(planeManager, scanner);
|
||||||
|
case 3 -> searchPlane(planeManager, scanner);
|
||||||
|
case 4 -> printAllPlanes(planeManager);
|
||||||
|
case 5 -> printCommercialPlanes(planeManager);
|
||||||
|
case 6 -> printMilitaryPlanes(planeManager);
|
||||||
|
case 7 -> printFastestPlane(planeManager);
|
||||||
|
case 0 -> System.out.println("Exiting program...");
|
||||||
|
default -> System.out.println("Invalid choice. Please try again.");
|
||||||
|
}
|
||||||
|
} while (choice != 0);
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addPlane(PlaneManager planeManager, Scanner scanner) {
|
||||||
|
System.out.print("Enter the plane's ID: ");
|
||||||
|
String id = scanner.nextLine();
|
||||||
|
System.out.print("Enter the plane's manufacturer: ");
|
||||||
|
String manufacturer = scanner.nextLine();
|
||||||
|
System.out.print("Enter the plane's model: ");
|
||||||
|
String model = scanner.nextLine();
|
||||||
|
System.out.print("Enter the plane's year of manufacture: ");
|
||||||
|
int year = Integer.parseInt(scanner.nextLine());
|
||||||
|
System.out.print("Enter the plane's passenger count: ");
|
||||||
|
int passengerCount = Integer.parseInt(scanner.nextLine());
|
||||||
|
System.out.print("Enter the plane's maximum speed: ");
|
||||||
|
int maxSpeed = Integer.parseInt(scanner.nextLine());
|
||||||
|
System.out.print("Enter the plane's type (commercial/military): ");
|
||||||
|
String type = scanner.nextLine();
|
||||||
|
if (type.equals("commercial")) {
|
||||||
|
System.out.print("Enter the plane's crew members count: ");
|
||||||
|
int crewMembersCount = Integer.parseInt(scanner.nextLine());
|
||||||
|
planeManager.addPlane(new CommercialPlane(id, manufacturer, model, year, passengerCount, maxSpeed, crewMembersCount));
|
||||||
|
}
|
||||||
|
else if (type.equals("military")) {
|
||||||
|
System.out.print("Enter the plane's missile count: ");
|
||||||
|
int missileCount = Integer.parseInt(scanner.nextLine());
|
||||||
|
planeManager.addPlane(new MilitaryPlane(id, manufacturer, model, year, passengerCount, maxSpeed, missileCount));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void removePlane(PlaneManager planeManager, Scanner scanner) {
|
||||||
|
System.out.print("Enter the plane's ID: ");
|
||||||
|
String id = scanner.nextLine();
|
||||||
|
if (planeManager.searchPlane(id) == null) {
|
||||||
|
System.out.println("Plane not found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
planeManager.removePlane(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void searchPlane(PlaneManager planeManager, Scanner scanner) {
|
||||||
|
System.out.print("Enter the plane's ID: ");
|
||||||
|
String id = scanner.nextLine();
|
||||||
|
Plane plane = planeManager.searchPlane(id);
|
||||||
|
if (plane == null) {
|
||||||
|
System.out.println("Plane not found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printAllPlanes(PlaneManager planeManager) {
|
||||||
|
planeManager.printAllPlanes();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printCommercialPlanes(PlaneManager planeManager) {
|
||||||
|
planeManager.printAllPlanes("commercial");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printMilitaryPlanes(PlaneManager planeManager) {
|
||||||
|
planeManager.printAllPlanes("military");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printFastestPlane(PlaneManager planeManager) {
|
||||||
|
System.out.print(planeManager.getFastestPlane());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue