diff --git a/hw2/code/pom.xml b/hw2/code/pom.xml new file mode 100644 index 0000000..9763dd5 --- /dev/null +++ b/hw2/code/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + ch.usi.inf.ajp22 + Assignment2 + 1.0-SNAPSHOT + + + 18 + 18 + UTF-8 + + + \ No newline at end of file diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/Main.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/Main.java new file mode 100644 index 0000000..c142180 --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/Main.java @@ -0,0 +1,112 @@ +package ch.usi.inf.ajp22; + +import ch.usi.inf.ajp22.flyable.*; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + + /** + * 3 Points + * TODO: Create a public static void function called "callFlyMethods". + * input: a List of objects that implement the Flyable interface. + * This function calls the method "fly" of every object inside the list. + */ + + /** + * 3 Points + * TODO: Create a public static void function called "transformPeople". + * input: a List from which People are read, called "src"; + * a List in which People are inserted, called "dst". + * This function iterates over all the elements of "src" and copies them in the "dst" List. + * If the element in "src" starts with the "z" or "Z" character then in the "dst" list is + * added a Wizard instead of a person. The Wizard's age must be 100 times the person's age. + * + * Remember to use the get-put principle properly. + */ + + /** + * You can use the main function to help check that you are on the right path for your solution. + * You don't have to change it yourself. + */ + public static void main(String[] args) { + Wizard circe = new Wizard("Circe", 300); + Wizard merlin = new Wizard("Merlin", 430); + Airplane firstAirplane = new Airplane<>(); + Airplane secondAirplane = new Airplane<>(); + People joe = new People("Joe", 20); + People eva = new People("Eva", 18); + People bill = new People("Bill", 20); + People fiona = new People("Fiona", 18); + People zeke = new People("Zeke", 18); + + + List flyableList = new ArrayList<>(); + flyableList.add(circe); + flyableList.add(merlin); + flyableList.add(firstAirplane); + callFlyMethod(flyableList); + + List firstPeopleList = new ArrayList<>(); + firstPeopleList.add(joe); + firstPeopleList.add(eva); + + List secondPeopleList = new ArrayList<>(); + secondPeopleList.add(bill); + secondPeopleList.add(fiona); + secondPeopleList.add(zeke); + + firstAirplane.setPassengers(firstPeopleList); + + /* + * Example usage of the transformPeople function + */ + transformPeople(secondPeopleList, firstPeopleList); + + /* + * Example of PeopleComparator usage + */ + PeopleComparator peopleComparator = new PeopleComparator<>(); + int res = peopleComparator.compare(bill, circe); + if(res > 0) { + System.out.printf("%s is greeter than %s\n", bill.getName(), circe.getName()); + } else if (res == 0) { + System.out.printf("%s is equal to %s\n", bill.getName(), circe.getName()); + } else { + System.out.printf("%s is less than %s\n", bill.getName(), circe.getName()); + } + + int res2 = peopleComparator.compare(merlin, circe); + if(res2 > 0) { + System.out.printf("%s is greeter than %s\n", merlin.getName(), circe.getName()); + } else if (res2 == 0) { + System.out.printf("%s is equal to %s\n", merlin.getName(), circe.getName()); + } else { + System.out.printf("%s is less than %s\n", merlin.getName(), circe.getName()); + } + + Couple> couple1 = new Couple<>(bill, firstAirplane); + System.out.println(couple1); + + Couple wizardCouple = new Couple<>(circe, circe); + System.out.print(wizardCouple); + + circe.setSpeed(1000); + merlin.setSpeed(200); + + /* + * Example of comparing two Wizards + */ + if ( circe.compareTo(merlin) > 0) { + System.out.println("Circe is faster than Merlin"); + } else if ( circe.compareTo(merlin) < 0) { + System.out.println("Circe is slower than Merlin"); + } else { + System.out.println("Circe has the same speed as Merlin"); + } + + firstAirplane.moveToPassengers(secondAirplane.getPassengers()); + + } +} diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Airplane.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Airplane.java new file mode 100644 index 0000000..5db7b40 --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Airplane.java @@ -0,0 +1,46 @@ +package ch.usi.inf.ajp22.flyable; + +import java.util.ArrayList; +import java.util.List; + +/** + * 3 Points + * TODO: Improve the class below by parametrizing it (i.e. using generics). + * passengers can be of type People or any other subtype of People. + * 3 Points + * TODO: Create the following method: + * 1) moveToPassengers + * Input: a List of People called "dst". + * This method must copy all the passengers of the current plane to the "dst" List. + * After the copy, the List of passenger of the current plane must be cleared. + * + * Remember to use the get-put principle properly. + */ +public class Airplane implements Flyable{ + + private final List passengers; + + @Override + public int getSpeed() { + return 740; + } + + + @Override + public void fly() { + System.out.println("Check the engines and fly"); + } + + public Airplane() { + this.passengers = new ArrayList<>(); + } + + public void setPassengers(List list) { + passengers.addAll(list); + } + + public List getPassengers() { + return this.passengers; + } + +} diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Couple.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Couple.java new file mode 100644 index 0000000..12ea385 --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Couple.java @@ -0,0 +1,25 @@ +package ch.usi.inf.ajp22.flyable; + +/** + * 3 Points + * TODO: Improve the class below by parametrizing it (i.e. using generics). + * passengers can be of type People or any other subtypes of People. + * flyable can be any type that implements the Flyable interface. + * This may lead in changing the type of the fields passenger and flyable. + */ +public class Couple { + + private final Object passenger; + private final Object flyable; + + public Couple(Object passenger, Object flyable) { + this.passenger = passenger; + this.flyable = flyable; + } + + @Override + public String toString() { + return String.format("Passenger :%s On :%s", ((People)this.passenger).getName(), + this.flyable.getClass().getSimpleName()); + } +} diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Flyable.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Flyable.java new file mode 100644 index 0000000..409aeb9 --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Flyable.java @@ -0,0 +1,7 @@ +package ch.usi.inf.ajp22.flyable; + +public interface Flyable { + void fly(); + + int getSpeed(); +} diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/FlyableComparator.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/FlyableComparator.java new file mode 100644 index 0000000..1f16101 --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/FlyableComparator.java @@ -0,0 +1,7 @@ +package ch.usi.inf.ajp22.flyable; + +/** + * 3 Points + * TODO: Write a Comparator named FlyableComparator. + * Comparison should be done using the speed of types that implement the Flyable interface. + */ \ No newline at end of file diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/People.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/People.java new file mode 100644 index 0000000..4ccb46e --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/People.java @@ -0,0 +1,23 @@ +package ch.usi.inf.ajp22.flyable; + +public class People { + private String name; + private int age; + + public People(String name, int age) { + this.name = name; + this.age = age; + } + + public int getAge() { return this.age; } + + public String getName() { return this.name; } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/PeopleComparator.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/PeopleComparator.java new file mode 100644 index 0000000..6d52b45 --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/PeopleComparator.java @@ -0,0 +1,7 @@ +package ch.usi.inf.ajp22.flyable; + +/** + * 3 Points + * TODO: Create a Comparator, named PeopleComparator, for People and its subtypes. + * Comparison should be done first by age and then by name length. + */ \ No newline at end of file diff --git a/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Wizard.java b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Wizard.java new file mode 100644 index 0000000..836fd4c --- /dev/null +++ b/hw2/code/src/main/java/ch/usi/inf/ajp22/flyable/Wizard.java @@ -0,0 +1,19 @@ +package ch.usi.inf.ajp22.flyable; + +/** + * 4 Points + * TODO: Let the class implement Flyable and Comparable. + * The comparison should be done using the method getSpeed. + */ +public class Wizard extends People { + + private int speed; + public Wizard(String name, int age) { + super(name, age); + this.speed = 100; + } + + public void setSpeed(int speed) { + this.speed = speed; + } +}