Merge pull request #61 from TiagoRG/dev-tiagorg

Removed unnecessary toString() skull
[POO] aula11: implemented SimpleGradeCalculator
[POO] Simplification
[POO] material added
[POO] aula12 ex1 added
[POO] aula12 ex2 added
[POO] aula12 ex3 added
[POO] imports optimization
	Optimized imports in:
	- aula02
	- aula03
	- aula06
	- aula08
	- aula09
	- aula11

[OTHER] Maths material added
This commit is contained in:
Tiago Garcia 2023-05-15 21:53:22 +01:00 committed by GitHub
commit 5c6b685895
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 179 additions and 48 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,11 +1,9 @@
package aula02; package aula02;
import java.util.Scanner;
// Código da package utils disponível em
// https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/utils
import utils.UserInput; import utils.UserInput;
import java.util.Scanner;
// Solução do exercício 5 // Solução do exercício 5
public class AverageSpeed { public class AverageSpeed {

View File

@ -1,6 +1,7 @@
package aula03; package aula03;
import utils.UserInput; import utils.UserInput;
import java.util.Scanner; import java.util.Scanner;
// Solução do exercício 5 // Solução do exercício 5

View File

@ -1,8 +1,9 @@
package aula03; package aula03;
import java.util.Scanner;
import utils.UserInput; import utils.UserInput;
import java.util.Scanner;
// Solução do exercício 4 com o utilizador a introduzir as notas individualmente // Solução do exercício 4 com o utilizador a introduzir as notas individualmente
public class Grades { public class Grades {

View File

@ -1,6 +1,7 @@
package aula03; package aula03;
import utils.UserInput; import utils.UserInput;
import java.util.Random; import java.util.Random;
import java.util.Scanner; import java.util.Scanner;

View File

@ -1,6 +1,7 @@
package aula03; package aula03;
import utils.UserInput; import utils.UserInput;
import java.util.Scanner; import java.util.Scanner;
// Solução do exercício 2 // Solução do exercício 2

View File

@ -2,6 +2,7 @@ package aula03;
import utils.MathTools; import utils.MathTools;
import utils.UserInput; import utils.UserInput;
import java.util.Scanner; import java.util.Scanner;
// Solução do exercício 1 // Solução do exercício 1

View File

@ -1,8 +1,9 @@
package aula03; package aula03;
import java.util.Scanner;
import utils.StringMethods; import utils.StringMethods;
import java.util.Scanner;
public class StringExtras { public class StringExtras {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);

View File

@ -1,8 +1,9 @@
package aula06.ex1; package aula06.ex1;
import java.util.Scanner;
import utils.DateYMD; import utils.DateYMD;
import java.util.Scanner;
public class PersonTest { public class PersonTest {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);

View File

@ -1,6 +1,7 @@
package aula06.ex1; package aula06.ex1;
import utils.DateYMD; import utils.DateYMD;
import java.time.LocalDate; import java.time.LocalDate;
public class Student extends Person { public class Student extends Person {

View File

@ -1,8 +1,8 @@
package aula08.ex1.Vehicles; package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IKmTravelled; import aula08.ex1.Interfaces.IKmTravelled;
import aula08.ex1.VehicleComp;
import aula08.ex1.Main; import aula08.ex1.Main;
import aula08.ex1.VehicleComp;
import utils.Validations; import utils.Validations;
import java.util.Objects; import java.util.Objects;

View File

@ -1,6 +1,7 @@
package aula08.ex2.Dishes; package aula08.ex2.Dishes;
import aula08.ex2.Aliments.Aliment; import aula08.ex2.Aliments.Aliment;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Objects; import java.util.Objects;

View File

@ -1,8 +1,13 @@
package aula08.ex2; package aula08.ex2;
import aula08.ex2.Aliments.*; import aula08.ex2.Aliments.*;
import aula08.ex2.Dishes.*; import aula08.ex2.Dishes.DietDish;
import aula08.ex2.Enums.*; import aula08.ex2.Dishes.Dish;
import aula08.ex2.Dishes.VeganDish;
import aula08.ex2.Enums.AlimentType;
import aula08.ex2.Enums.DishType;
import aula08.ex2.Enums.FishState;
import aula08.ex2.Enums.MeatType;
import utils.Enums.Weekday; import utils.Enums.Weekday;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;

View File

@ -1,6 +1,7 @@
package aula08.ex3; package aula08.ex3;
import aula08.ex3.Interfaces.IPurchase; import aula08.ex3.Interfaces.IPurchase;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
public class Purchase implements IPurchase { public class Purchase implements IPurchase {

View File

@ -1,6 +1,6 @@
package aula09.ex3; package aula09.ex3;
import java.util.*; import java.util.LinkedList;
public class PlaneManager { public class PlaneManager {
private final LinkedList<Plane> planes = new LinkedList<>(); private final LinkedList<Plane> planes = new LinkedList<>();

View File

@ -1,27 +1,17 @@
package aula11.ex1; package aula11.ex1;
import java.io.*; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;
public class WordPairCounter { public class WordPairCounter {
public static void main(String[] args) { public static void main(String[] args) {
TreeMap<String, HashMap<String, Integer>> wordPairs = new TreeMap<>() { TreeMap<String, HashMap<String, Integer>> wordPairs = new TreeMap<>();
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (String word1 : this.keySet()) {
sb.append(word1).append("={");
for (String word2 : this.get(word1).keySet()) {
sb.append(word2).append("=").append(this.get(word1).get(word2)).append(", ");
}
sb.delete(sb.length()-2, sb.length()).append("}\n");
}
return sb.toString();
}
};
String text = ""; String text = "";
Path path = Paths.get((new Scanner(System.in)).nextLine()); Path path = Paths.get((new Scanner(System.in)).nextLine());
@ -42,6 +32,6 @@ public class WordPairCounter {
word1Pair.put(word2, word1Pair.getOrDefault(word2, 0) + 1); word1Pair.put(word2, word1Pair.getOrDefault(word2, 0) + 1);
} }
System.out.println(wordPairs); wordPairs.entrySet().forEach(System.out::println);
} }
} }

View File

@ -1,15 +1,15 @@
package aula11.ex2; package aula11.ex2;
import utils.MathTools; import java.io.IOException;
import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.Arrays;
import java.util.LinkedList;
public class Gradebook implements IGradeCalculator { public class Gradebook {
private final LinkedList<Student> students = new LinkedList<>(); private final LinkedList<Student> students = new LinkedList<>();
private final IGradeCalculator gradeCalculator = new SimpleGradeCalculator();
public void load(String filename) { public void load(String filename) {
LinkedList<String> lines = new LinkedList<>(); LinkedList<String> lines = new LinkedList<>();
@ -42,16 +42,11 @@ public class Gradebook implements IGradeCalculator {
} }
public double calculateAverageGrade(String name) { public double calculateAverageGrade(String name) {
return calculate(getStudent(name).getGrades()); return gradeCalculator.calculate(getStudent(name).getGrades());
}
@Override
public double calculate(LinkedList<Double> grades) {
return MathTools.media(grades);
} }
public void printAllStudents() { public void printAllStudents() {
for (Student student : students) for (Student student : students)
System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), calculate(student.getGrades())); System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), gradeCalculator.calculate(student.getGrades()));
} }
} }

View File

@ -0,0 +1,13 @@
package aula11.ex2;
import java.util.LinkedList;
public class SimpleGradeCalculator implements IGradeCalculator {
@Override
public double calculate(LinkedList<Double> grades) {
double sum = 0;
for (double n : grades)
sum += n;
return sum / grades.size();
}
}

View File

@ -8,7 +8,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
public class EnergyUsageReport { public class EnergyUsageReport {
@ -48,13 +47,9 @@ public class EnergyUsageReport {
return MathTools.sum(getCustomer(id).getMeterReadings()); return MathTools.sum(getCustomer(id).getMeterReadings());
} }
public void generateReport(String path) { public void generateReport(String path) throws IOException {
for (Customer customer : customers) { for (Customer customer : customers) {
try {
Files.writeString(Path.of(path), String.format("Customer ID: %d%nTotal usage: %.1f%n%n", customer.getCustomerId(), calculateTotalUsage(customer.getCustomerId())), (new File(path)).exists() ? java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE); Files.writeString(Path.of(path), String.format("Customer ID: %d%nTotal usage: %.1f%n%n", customer.getCustomerId(), calculateTotalUsage(customer.getCustomerId())), (new File(path)).exists() ? java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }
} }

View File

@ -1,9 +1,11 @@
package aula11.ex4; package aula11.ex4;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.io.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class FlightManager { public class FlightManager {

View File

@ -0,0 +1,22 @@
package aula12.ex1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
public class DifferentWordCounter {
public static void main(String[] args) throws FileNotFoundException {
Scanner reader = new Scanner(new FileReader((new Scanner(System.in)).nextLine()));
List<String> words = new ArrayList<>();
while (reader.hasNext()) words.add(reader.next());
HashSet<String> differentWords = new HashSet<>(words);
System.out.println("Number of words: " + words.size());
System.out.println("Number of different words: " + differentWords.size());
}
}

View File

@ -0,0 +1,39 @@
package aula12.ex2;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;
public class WordCounterByInitial {
public static void main(String[] args) throws IOException {
TreeMap<Character, TreeMap<String, Integer>> words = new TreeMap<>() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Character initial : this.keySet()) {
sb.append(initial).append(": ");
for (String word : this.get(initial).keySet())
sb.append(word).append(", ").append(this.get(initial).get(word)).append("; ");
sb.append("\b\n");
}
return sb.toString();
}
};
Arrays.stream(Files.readString(Path.of((new Scanner(System.in)).nextLine()))
.split("[\\s.,:';?!\\-*{}=+&/()\\[\\]”“\"]+"))
.filter(word -> word.length() > 2)
.map(String::toLowerCase)
.forEach(word -> {
char initial = word.charAt(0);
words.putIfAbsent(initial, new TreeMap<>());
TreeMap<String, Integer> initialWords = words.get(initial);
initialWords.put(word, initialWords.getOrDefault(word, 0) + 1);
});
System.out.println(words);
}
}

View File

@ -0,0 +1,54 @@
package aula12.ex3;
import utils.UserInput;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
List<Movie> movies = new ArrayList<>();
TreeSet<String> titles = new TreeSet<>();
List<Movie> unsortedMovies = new ArrayList<>();
Scanner reader = new Scanner(new FileReader("datafiles/aula12/movies.txt"));
reader.nextLine();
while (reader.hasNextLine()) {
String[] line = reader.nextLine().split("\t");
Movie movie = new Movie(line[0], Double.parseDouble(line[1]), line[2], line[3], Integer.parseInt(line[4]));
unsortedMovies.add(movie);
titles.add(movie.title().toLowerCase());
}
for (String title : titles)
movies.add(unsortedMovies.stream().filter(movie -> movie.title().equalsIgnoreCase(title)).findFirst().get());
System.out.println(movies);
System.out.println("\n\n\n");
System.out.println(movies.stream().sorted(Comparator.comparing(Movie::score).reversed()).toList());
System.out.println("\n\n\n");
System.out.println(movies.stream().sorted(Comparator.comparing(Movie::duration)).toList());
System.out.println("\n\n\n");
TreeMap<String, Integer> genres = new TreeMap<>();
movies.forEach(movie -> genres.put(movie.genre(), genres.getOrDefault(movie.genre(), 0) + 1));
System.out.println(genres);
System.out.println("\n\n\n");
String genre = UserInput.input(new Scanner(System.in), "Genre: ");
movies.stream().filter(movie -> movie.genre().equalsIgnoreCase(genre) && movie.score() > 60).forEach(movie -> {
try {
Files.writeString(Path.of("datafiles/aula12/myselection.txt"), movie.toString());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

View File

@ -0,0 +1,8 @@
package aula12.ex3;
public record Movie(String title, double score, String rating, String genre, int duration) {
@Override
public String toString() {
return String.format("%s\n\tScore: %.1f\n\tRating: %s\n\tGenre: %s\n\tDuration: %d\n\n", title, score, rating, genre, duration);
}
}