hw2: added code

This commit is contained in:
Claudio Maggioni 2022-10-18 16:37:30 +02:00
parent 3b72b84664
commit 1c8b35a55a
9 changed files with 263 additions and 0 deletions

17
hw2/code/pom.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.usi.inf.ajp22</groupId>
<artifactId>Assignment2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -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<People> firstAirplane = new Airplane<>();
Airplane<People> 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<Flyable> flyableList = new ArrayList<>();
flyableList.add(circe);
flyableList.add(merlin);
flyableList.add(firstAirplane);
callFlyMethod(flyableList);
List<People> firstPeopleList = new ArrayList<>();
firstPeopleList.add(joe);
firstPeopleList.add(eva);
List<People> 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<People> 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<People, Airplane<People>> couple1 = new Couple<>(bill, firstAirplane);
System.out.println(couple1);
Couple<Wizard, Wizard> 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());
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,7 @@
package ch.usi.inf.ajp22.flyable;
public interface Flyable {
void fly();
int getSpeed();
}

View File

@ -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.
*/

View File

@ -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;
}
}

View File

@ -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.
*/

View File

@ -0,0 +1,19 @@
package ch.usi.inf.ajp22.flyable;
/**
* 4 Points
* TODO: Let the class implement Flyable and Comparable<Flyable>.
* 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;
}
}