diff --git a/1ano/2semestre/poo/guides/POO-2022-aula10.pdf b/1ano/2semestre/poo/guides/POO-2022-aula10.pdf new file mode 100644 index 0000000..cac1e56 Binary files /dev/null and b/1ano/2semestre/poo/guides/POO-2022-aula10.pdf differ diff --git a/1ano/2semestre/poo/src/aula10/ex1/Book.java b/1ano/2semestre/poo/src/aula10/ex1/Book.java new file mode 100644 index 0000000..49f16d3 --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex1/Book.java @@ -0,0 +1,39 @@ +package aula10.ex1; + +import java.util.Objects; + +public class Book { + private String title; + private String author; + private int year; + + public Book(String title, String author, int year) { + this.setTitle(title); + this.setAuthor(author); + this.setYear(year); + } + + public String getTitle() { return this.title; } + public void setTitle(String title) { this.title = title; } + public String getAuthor() { return this.author; } + public void setAuthor(String author) { this.author = author; } + public int getYear() { return this.year; } + public void setYear(int year) { this.year = year; } + + @Override + public String toString() { + return String.format("%s, %s, %d", this.getTitle(), this.getAuthor(), this.getYear()); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book book)) return false; + return getYear() == book.getYear() && Objects.equals(getTitle(), book.getTitle()) && Objects.equals(getAuthor(), book.getAuthor()); + } + + @Override + public int hashCode() { + return Objects.hash(getTitle(), getAuthor(), getYear()); + } +} diff --git a/1ano/2semestre/poo/src/aula10/ex1/BooksTest.java b/1ano/2semestre/poo/src/aula10/ex1/BooksTest.java new file mode 100644 index 0000000..36a0b12 --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex1/BooksTest.java @@ -0,0 +1,30 @@ +package aula10.ex1; + +import java.util.HashMap; + +public class BooksTest { + static HashMap books = new HashMap<>() { + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (String genre : this.keySet()) { + sb.append(genre).append(" : ").append(this.get(genre)).append("\n"); + } + return sb.toString(); + } + }; + + public static void main(String[] args) { + books.put("Fantasia", new Book("Harry Potter", "J. K. Rowling", 1997)); + books.put("Ficção Científica", new Book("The Martian", "Andy Weir", 2011)); + books.put("Romance", new Book("Pride and Prejudice", "Jane Austen", 1813)); + books.put("Terror", new Book("The Shining", "Stephen King", 1977)); + System.out.println(books); + for (String genre : books.keySet()) { + System.out.println(genre); + } + for (Book book : books.values()) { + System.out.println(book); + } + } +} diff --git a/1ano/2semestre/poo/src/aula10/ex2/BooksTest.java b/1ano/2semestre/poo/src/aula10/ex2/BooksTest.java new file mode 100644 index 0000000..8dc7519 --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex2/BooksTest.java @@ -0,0 +1,44 @@ +package aula10.ex2; + +import aula10.ex1.Book; + +import java.util.HashMap; +import java.util.LinkedList; + +public class BooksTest { + static HashMap> books = new HashMap<>() { + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (String genre : this.keySet()) { + sb.append(genre).append(":"); + for (Book book : this.get(genre)) { + sb.append("\n\t").append(book); + } + sb.append("\n"); + } + return sb.toString(); + } + }; + + public static void main(String[] args) { + books.put("Fantasia", new LinkedList<>()); + books.put("Ficção Científica", new LinkedList<>()); + books.put("Romance", new LinkedList<>()); + books.put("Terror", new LinkedList<>()); + books.get("Fantasia").add(new Book("Harry Potter", "J. K. Rowling", 1997)); + books.get("Ficção Científica").add(new Book("The Martian", "Andy Weir", 2011)); + books.get("Romance").add(new Book("Pride and Prejudice", "Jane Austen", 1813)); + books.get("Terror").add(new Book("The Shining", "Stephen King", 1977)); + books.get("Fantasia").add(new Book("The Hobbit", "J. R. R. Tolkien", 1937)); + books.get("Ficção Científica").add(new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams", 1979)); + books.get("Romance").add(new Book("Jane Eyre", "Charlotte Brontë", 1847)); + books.get("Terror").add(new Book("Frankenstein", "Mary Shelley", 1818)); + System.out.println(books); + System.out.print(selectRandomBook("Fantasia")); + } + + public static Book selectRandomBook(String genre) { + return books.get(genre).get((int) (Math.random() * books.get(genre).size())); + } +} diff --git a/1ano/2semestre/poo/src/aula10/ex3/CharacterCounter.java b/1ano/2semestre/poo/src/aula10/ex3/CharacterCounter.java new file mode 100644 index 0000000..854cc88 --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex3/CharacterCounter.java @@ -0,0 +1,45 @@ +package aula10.ex3; + +import java.util.HashMap; +import java.util.LinkedList; + +public class CharacterCounter { + private final HashMap counter = new HashMap<>(); + private final HashMap> indexes = new HashMap<>(); + private final String text; + + public CharacterCounter(String text) { + this.text = text; + } + + public void count() { + for (char c : this.text.toCharArray()) { + if (this.counter.containsKey(c)) { + this.counter.put(c, this.counter.get(c) + 1); + } else { + this.counter.put(c, 1); + } + } + } + + public void indexes() { + for (int i = 0; i < this.text.length(); i++) { + char c = this.text.charAt(i); + if (this.indexes.containsKey(c)) { + this.indexes.get(c).add(i); + } else { + LinkedList list = new LinkedList<>(); + list.add(i); + this.indexes.put(c, list); + } + } + } + + public HashMap getCounter() { + return this.counter; + } + + public HashMap> getIndexes() { + return this.indexes; + } +} diff --git a/1ano/2semestre/poo/src/aula10/ex3/Main.java b/1ano/2semestre/poo/src/aula10/ex3/Main.java new file mode 100644 index 0000000..0dd8795 --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex3/Main.java @@ -0,0 +1,11 @@ +package aula10.ex3; + +public class Main { + public static void main(String[] args) { + CharacterCounter counter = new CharacterCounter("Hello World!"); + counter.count(); + System.out.println(counter.getCounter()); + counter.indexes(); + System.out.println(counter.getIndexes()); + } +} diff --git a/1ano/2semestre/poo/src/aula10/ex4/FileReaderTest.java b/1ano/2semestre/poo/src/aula10/ex4/FileReaderTest.java new file mode 100644 index 0000000..ac5ef6d --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex4/FileReaderTest.java @@ -0,0 +1,29 @@ +package aula10.ex4; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.LinkedList; +import java.util.Scanner; + +public class FileReaderTest { + public static void main(String[] args) { + LinkedList words = new LinkedList<>(); + Scanner input; + try { + input = new Scanner(new FileReader("src/aula10/ex4/words.txt")); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + while(input.hasNext()){ + String word = input.next(); + if (word.length() > 2) { + words.add(word); + } + System.out.println(word); + } + for (String word : words) + if (word.endsWith("s")) + System.out.println(word); + words.removeIf(word -> !word.matches("^[a-zA-Záàãâéẽêíóõôúç]+$")); + } +} diff --git a/1ano/2semestre/poo/src/aula10/ex4/words.txt b/1ano/2semestre/poo/src/aula10/ex4/words.txt new file mode 100644 index 0000000..290bd17 --- /dev/null +++ b/1ano/2semestre/poo/src/aula10/ex4/words.txt @@ -0,0 +1,2 @@ +Testing the file reader with some simple and random text. +Should also have words ended with s \ No newline at end of file