From a5a96a47404099cb895557cb1bd2c03dcecc6312 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 25 Feb 2023 12:18:54 +0000 Subject: [PATCH] POO: aula03 ex1-3 added --- 1ano/2semestre/poo/README.md | 1 + 1ano/2semestre/poo/src/README.md | 7 ++++ 1ano/2semestre/poo/src/aula02/README.md | 3 +- 1ano/2semestre/poo/src/aula03/HiLo.java | 37 +++++++++++++++++++ 1ano/2semestre/poo/src/aula03/Investment.java | 27 ++++++++++++++ 1ano/2semestre/poo/src/aula03/PrimesSum.java | 33 +++++++++++++++++ 1ano/2semestre/poo/src/aula03/README.md | 13 ++++--- 7 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula03/HiLo.java create mode 100644 1ano/2semestre/poo/src/aula03/Investment.java create mode 100644 1ano/2semestre/poo/src/aula03/PrimesSum.java diff --git a/1ano/2semestre/poo/README.md b/1ano/2semestre/poo/README.md index 66ea6dd..5880bc1 100755 --- a/1ano/2semestre/poo/README.md +++ b/1ano/2semestre/poo/README.md @@ -15,6 +15,7 @@ |-----------------------------------------------------------------------------------------|----------------------| | [01](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula01) | Introduction, Basics | | [02](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula02) | Flux Control | +| [03](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula03) | Classes | --- *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) diff --git a/1ano/2semestre/poo/src/README.md b/1ano/2semestre/poo/src/README.md index 4cf3d18..c85c255 100644 --- a/1ano/2semestre/poo/src/README.md +++ b/1ano/2semestre/poo/src/README.md @@ -2,5 +2,12 @@ ## Código fonte para as diversas aulas de POO ### Métodos extra no diretório [*utils*](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/utils) +## Índice +| Aula nº | Tópicos | +|-----------------------------------------------------------------------------------------|----------------------| +| [01](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula01) | Introduction, Basics | +| [02](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula02) | Flux Control | +| [03](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/src/aula03) | Classes | + --- *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) diff --git a/1ano/2semestre/poo/src/aula02/README.md b/1ano/2semestre/poo/src/aula02/README.md index de42a52..5fdc3f6 100755 --- a/1ano/2semestre/poo/src/aula02/README.md +++ b/1ano/2semestre/poo/src/aula02/README.md @@ -3,7 +3,8 @@ ### Tópico principal da aula: Flux Control * [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula02.pdf) -* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_ControloFluxo.pdf) +* [Slides 1](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_ControloFluxo.pdf) +* [Slides 2](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_Modularidade_MetodosEstaticos.pdf) ### Exercise List | Exercise Number | File Name | Exercise Number | File Name | diff --git a/1ano/2semestre/poo/src/aula03/HiLo.java b/1ano/2semestre/poo/src/aula03/HiLo.java new file mode 100644 index 0000000..9c1f5ab --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/HiLo.java @@ -0,0 +1,37 @@ +package aula03; + +import utils.UserInput; +import java.util.Random; +import java.util.Scanner; + +public class HiLo { + private static final Scanner sin = new Scanner(System.in); + private static final Random rand = new Random(); + + public static void main(String[] args) { + String toContinue; + do { + game(); + System.out.print("Pretende continuar? Prima (S)im "); + toContinue = sin.next(); + } while (toContinue.equals("S") || toContinue.equals("Sim")); + } + + private static void game() { + int secret = rand.nextInt(1, 100); + int i = 1; + int n; + while (true) { + System.out.printf("Tentativa %d: ", i); + n = (int) UserInput.getNumberBetween(sin, 1, 100); + if (n > secret) + System.out.println("Tentativa demasiado alta"); + else if (n < secret) + System.out.println("Tentativa demasiado baixa"); + else + break; + i++; + } + System.out.printf("Acertou, o número era %d, foram precisas %d tentativas.\n", secret, i); + } +} diff --git a/1ano/2semestre/poo/src/aula03/Investment.java b/1ano/2semestre/poo/src/aula03/Investment.java new file mode 100644 index 0000000..3a0910f --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/Investment.java @@ -0,0 +1,27 @@ +package aula03; + +import utils.UserInput; + +import java.util.Scanner; + +public class Investment { + public static void main(String[] args) { + Scanner sin = new Scanner(System.in); + + int investment; + do { + System.out.print("Introduza o investimento inicial (múltiplo de 1000): "); + investment = sin.nextInt(); + } while (investment <= 0 || investment % 1000 != 0); + + System.out.print("Introduza a taxa a aplicar (entre 0% e 5%): "); + double tax = UserInput.getNumberBetween(sin, 0, 5); + + for (int i = 1; i <= 12; i++) { + investment *= 1+tax/100; + System.out.printf("Investimento em %d %s: %d\n", i, i==1?"mês":"meses", investment); + } + + sin.close(); + } +} diff --git a/1ano/2semestre/poo/src/aula03/PrimesSum.java b/1ano/2semestre/poo/src/aula03/PrimesSum.java new file mode 100644 index 0000000..21ec79c --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/PrimesSum.java @@ -0,0 +1,33 @@ +package aula03; + +import utils.UserInput; +import java.util.Scanner; + +// Solução do exercício 1 + +public class PrimesSum { + public static void main(String[] args) { + Scanner sin = new Scanner(System.in); + + System.out.println("Introduza um número:"); + int n = (int) UserInput.getPositiveNumber(sin); + + int sum = 0; + for (int i = 0; i <= n; i++) + if (isNumPrime(i)) + sum += i; + + System.out.printf("A soma dos números primos até %d é %d", n, sum); + + sin.close(); + } + + private static boolean isNumPrime(int n) { + if (n == 1) + return false; + for (int i = 2; i < n; i++) + if (n % i == 0) + return false; + return true; + } +} diff --git a/1ano/2semestre/poo/src/aula03/README.md b/1ano/2semestre/poo/src/aula03/README.md index f323f26..2faa8cb 100755 --- a/1ano/2semestre/poo/src/aula03/README.md +++ b/1ano/2semestre/poo/src/aula03/README.md @@ -2,12 +2,15 @@ ## Aula 03 ### Tópico principal da aula: Classes -* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula02.pdf) -* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_ControloFluxo.pdf) +* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula03.pdf) +* [Slides](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_03_Classes.pdf) ### Exercise List -| Exercise Number | File Name | -|-----------------|----------------------------------------------------------------------------------------------------------------------------------------| -| 1 | [KmToMiles.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/KmToMiles.java) | +| Exercise Number | File Name | +|-----------------|----------------------------------------------------------------------------------------------------------------------| +| 1 | [PrimesSum.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula03/PrimesSum.java) | +| 2 | [Investment.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula03/Investment.java) | +| 3 | [HiLo.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula03/HiLo.java) | + --- *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)