POO: aula03 ex1-3 added

This commit is contained in:
TiagoRG 2023-02-25 12:18:54 +00:00
parent e4496c6c97
commit a5a96a4740
7 changed files with 115 additions and 6 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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 |

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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) |
|-----------------|----------------------------------------------------------------------------------------------------------------------|
| 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)