hw2: code

This commit is contained in:
Claudio Maggioni 2022-10-18 17:36:53 +02:00
parent 1c8b35a55a
commit af7c772881
14 changed files with 273 additions and 42 deletions

79
hw2/code/.gitignore vendored Normal file
View file

@ -0,0 +1,79 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
target/

8
hw2/code/.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Assignment2" />
</profile>
</annotationProcessing>
</component>
</project>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

12
hw2/code/.idea/misc.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="temurin-18" project-jdk-type="JavaSDK" />
</project>

6
hw2/code/.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

View file

@ -3,16 +3,22 @@ package ch.usi.inf.ajp22;
import ch.usi.inf.ajp22.flyable.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
/**
* 3 Points
* TODO: Create a public static void function called "callFlyMethods".
* 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.
*/
public static void callFlyMethod(Collection<? extends Flyable> list) {
list.forEach(Flyable::fly);
}
/**
* 3 Points
@ -22,9 +28,25 @@ public class Main {
* 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.
*
* <p>
* Remember to use the get-put principle properly.
*/
public static void transformPeople(List<? extends People> src, List<? super People> dst) {
// true -> quelli con la z (lista)
// false -> quelli senza la z (lista)
Map<Boolean, List<People>> startsWithZPartitioned = src.stream().collect(
Collectors.groupingBy(p -> p.getName().matches("^[zZ].*"))
);
// add the non-Wizards
dst.addAll(startsWithZPartitioned.getOrDefault(false, List.of()));
// convert the wizard-elects into Wizards and then add them
dst.addAll(startsWithZPartitioned.getOrDefault(true, List.of())
.stream()
.map(p -> new Wizard(p.getName(), p.getAge() * 100))
.toList());
}
/**
* You can use the main function to help check that you are on the right path for your solution.
@ -69,7 +91,7 @@ public class Main {
*/
PeopleComparator<People> peopleComparator = new PeopleComparator<>();
int res = peopleComparator.compare(bill, circe);
if(res > 0) {
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());
@ -78,7 +100,7 @@ public class Main {
}
int res2 = peopleComparator.compare(merlin, circe);
if(res2 > 0) {
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());
@ -98,9 +120,9 @@ public class Main {
/*
* Example of comparing two Wizards
*/
if ( circe.compareTo(merlin) > 0) {
if (circe.compareTo(merlin) > 0) {
System.out.println("Circe is faster than Merlin");
} else if ( circe.compareTo(merlin) < 0) {
} 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");

View file

@ -0,0 +1,24 @@
package ch.usi.inf.ajp22;
class First {
void doSomething() {
System.out.println(" I am a class ");
}
}
interface Second {
default void doSomething() {
System.out.println(" I am an interface ");
}
}
public class Third extends First implements Second {
@Override
public void doSomething() {
super.doSomething();
}
public static void main(String[] args) {
Third third = new Third();
third.doSomething();
}
}

View file

@ -1,31 +1,32 @@
package ch.usi.inf.ajp22.flyable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* 3 Points
* TODO: Improve the class below by parametrizing it (i.e. using generics).
* 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:
* 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.
*
* <p>
* Remember to use the get-put principle properly.
*/
public class Airplane implements Flyable{
public class Airplane<T extends People> implements Flyable {
private final List passengers;
private final List<T> passengers;
@Override
public int getSpeed() {
return 740;
}
@Override
public void fly() {
System.out.println("Check the engines and fly");
@ -35,12 +36,16 @@ public class Airplane implements Flyable{
this.passengers = new ArrayList<>();
}
public void setPassengers(List list) {
public <U extends T> void setPassengers(Collection<U> list) {
passengers.addAll(list);
}
public List getPassengers() {
return this.passengers;
public void moveToPassengers(final List<? super T> that) {
that.addAll(this.passengers);
this.passengers.clear();
}
public List<T> getPassengers() {
return this.passengers;
}
}

View file

@ -2,24 +2,24 @@ package ch.usi.inf.ajp22.flyable;
/**
* 3 Points
* TODO: Improve the class below by parametrizing it (i.e. using generics).
* 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 {
public class Couple<T extends People, U extends Flyable> {
private final Object passenger;
private final Object flyable;
private final T passenger;
private final U flyable;
public Couple(Object passenger, Object flyable) {
public Couple(T passenger, U flyable) {
this.passenger = passenger;
this.flyable = flyable;
}
@Override
public String toString() {
return String.format("Passenger :%s On :%s", ((People)this.passenger).getName(),
return String.format("Passenger :%s On :%s", this.passenger.getName(),
this.flyable.getClass().getSimpleName());
}
}

View file

@ -1,7 +1,16 @@
package ch.usi.inf.ajp22.flyable;
import java.util.Comparator;
/**
* 3 Points
* TODO: Write a Comparator named FlyableComparator.
* Write a Comparator named FlyableComparator.
* Comparison should be done using the speed of types that implement the Flyable interface.
*/
public final class FlyableComparator implements Comparator<Flyable> {
@Override
public int compare(final Flyable o1, final Flyable o2) {
return o2.getSpeed() - o1.getSpeed();
}
}

View file

@ -1,7 +1,18 @@
package ch.usi.inf.ajp22.flyable;
import java.util.Comparator;
/**
* 3 Points
* TODO: Create a Comparator, named PeopleComparator, for People and its subtypes.
* Create a Comparator, named PeopleComparator, for People and its subtypes.
* Comparison should be done first by age and then by name length.
*/
public final class PeopleComparator<T extends People> implements Comparator<T> {
private static final Comparator<People> AGE_AND_NAME_CMP = Comparator.comparingInt(People::getAge)
.thenComparingInt(p -> p.getName().length());
@Override
public int compare(final T o1, final T o2) {
return AGE_AND_NAME_CMP.compare(o1, o2);
}
}

View file

@ -2,10 +2,10 @@ package ch.usi.inf.ajp22.flyable;
/**
* 4 Points
* TODO: Let the class implement Flyable and Comparable<Flyable>.
* Let the class implement Flyable and Comparable<Flyable>.
* The comparison should be done using the method getSpeed.
*/
public class Wizard extends People {
public class Wizard extends People implements Flyable, Comparable<Flyable> {
private int speed;
public Wizard(String name, int age) {
@ -16,4 +16,19 @@ public class Wizard extends People {
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public void fly() {
System.out.println("Check broom and fly");
}
@Override
public int getSpeed() {
return speed;
}
@Override
public int compareTo(Flyable o) {
return this.speed - o.getSpeed();
}
}