[POO] aula10 added
This commit is contained in:
parent
d17790b275
commit
59321a15f1
Binary file not shown.
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package aula10.ex1;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BooksTest {
|
||||
static HashMap<String, Book> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package aula10.ex2;
|
||||
|
||||
import aula10.ex1.Book;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class BooksTest {
|
||||
static HashMap<String, LinkedList<Book>> 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()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package aula10.ex3;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class CharacterCounter {
|
||||
private final HashMap<Character, Integer> counter = new HashMap<>();
|
||||
private final HashMap<Character, LinkedList<Integer>> 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<Integer> list = new LinkedList<>();
|
||||
list.add(i);
|
||||
this.indexes.put(c, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<Character, Integer> getCounter() {
|
||||
return this.counter;
|
||||
}
|
||||
|
||||
public HashMap<Character, LinkedList<Integer>> getIndexes() {
|
||||
return this.indexes;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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<String> 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áàãâéẽêíóõôúç]+$"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
Testing the file reader with some simple and random text.
|
||||
Should also have words ended with s
|
Loading…
Reference in New Issue