Merge pull request #16 from TiagoRG/dev-tiagorg

This commit is contained in:
Tiago Garcia 2023-02-20 13:49:27 +00:00 committed by GitHub
commit ef7d937807
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
39 changed files with 284 additions and 14 deletions

View File

@ -5,5 +5,14 @@
* [Guião](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/guides/POO-2021-aula02.pdf) * [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](https://github.com/TiagoRG/uaveiro-leci/tree/master/1ano/2semestre/poo/slides/POO_02_ControloFluxo.pdf)
### Exercise List
| Exercise Number | File Name | Exercise Number | File Name |
|-----------------|----------------------------------------------------------------------------------------------------------------------------------------|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| 1 | [KmToMiles.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/KmToMiles.java) | 6 | [SecsToHMS.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/SecsToHMS.java) |
| 2 | [CelciusToFahrenheit.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/CelciusToFahrenheit.java) | 7 | [DistanceBetweenPoints.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/DistanceBetweenPoints.java) |
| 3 | [EnergyToHeatWater.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/EnergyToHeatWater.java) | 8 | [PythagoreanTheorem.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/PythagoreanTheorem.java) |
| 4 | [Investment.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/Investment.java) | 9 | [Countdown.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/Countdown.java) |
| 5 | [AverageSpeed.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/AverageSpeed.java) | 10 | [RealNumbers.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula02/RealNumbers.java) |
--- ---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

View File

@ -0,0 +1,23 @@
# Exercícios de Arrays
## Ex1
```java
final int LENGHT = 100;
int[] a = new int[LENGHT];
// Code for filling a ommited
for (int = 99; i >= 0; i--)
{
System.out.print(a[i]);
if (i > 0) { System.out.print(', '); }
}
```
## Ex2
```java
int[] numbers = new int[100];
for (int k = 0; k < numbers.length; k++)
{
numbers[k] = k + 1;
}
```

View File

@ -0,0 +1,9 @@
# Tabela Exercício 1 de Strings
| Question | Answer |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|
| What is the length of the string below?<br>String str = "Java Program" | 12 |
| With str as defined above, give a call to the substring method that returns the substring "gram". | str.substring(8) |
| Use the string concatenation operator to change the string variable str to contain the string "Java Programming". | str += "ming" |
| What does the following statement sequence print?<br><br>String str = "Harry";<br>int n = str.length();<br>String mystery = str.substring(0, 1) + str.substring(n - 1, n);<br>System.out.println(mystery); | Hy |
| Consider the following statement sequence. If the user provides the input John Q. Public, what is printed? If an error occurs, type error.<br><br>Scanner in = new Scanner(System.in);<br>String first = in.next();<br>String last = in.next();<br>System.out.println(last + ", " + first); | Q., John |

View File

@ -18,6 +18,8 @@ public class AverageSpeed {
double d2 = UserInput.getPositiveNumber(sin); double d2 = UserInput.getPositiveNumber(sin);
double vm = (d1 + d2) / ((d1 / v1) + (d2 / v2)); double vm = (d1 + d2) / ((d1 / v1) + (d2 / v2));
System.out.printf("Velocidade final da viagem: %fkm/h", vm); System.out.printf("Velocidade final da viagem: %.2fkm/h", vm);
sin.close();
} }
} }

View File

@ -10,6 +10,8 @@ public class CelciusToFahrenheit {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);
double celcius = sin.nextDouble(); double celcius = sin.nextDouble();
double fahrenheit = 1.8*celcius+32; double fahrenheit = 1.8*celcius+32;
System.out.printf("%fºC = %fºF", celcius, fahrenheit); System.out.printf("%.2fºC = %.2fºF", celcius, fahrenheit);
sin.close();
} }
} }

View File

@ -11,7 +11,11 @@ public class Countdown {
System.out.print("N? "); System.out.print("N? ");
int n = sin.nextInt(); int n = sin.nextInt();
for (int i = n; i >= 0; i--) { for (int i = n; i >= 0; i--) {
// If the statement before '?' is true then the expression before the ':' is used, else the expression after the ':' is used.
// In python: i + "\n" if i%10 == 0 else i + ","
System.out.print(i%10 == 0 ? i + "\n" : i + ","); System.out.print(i%10 == 0 ? i + "\n" : i + ",");
} }
sin.close();
} }
} }

View File

@ -1,5 +1,7 @@
package aula02; package aula02;
import utils.UserInput;
import java.util.Scanner; import java.util.Scanner;
// Solução do exercício 7 // Solução do exercício 7
@ -8,15 +10,15 @@ public class DistanceBetweenPoints {
public static void main(String[] args){ public static void main(String[] args){
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);
System.out.print("Coordenadas do ponto 1 (separadas por ','): "); String[] p1 = UserInput.input(sin, "Coordenadas do ponto 1 (separadas por ','): ").split(",");
String[] p1 = sin.next().split(","); String[] p2 = UserInput.input(sin, "Coordenadas do ponto 2 (separadas por ','): ").split(",");
System.out.print("Coordenadas do ponto 2 (separadas por ','): ");
String[] p2 = sin.next().split(",");
double distance = Math.sqrt( double distance = Math.sqrt(
Math.pow(Double.parseDouble(p1[0]) - Double.parseDouble(p2[0]), 2) + Math.pow(Double.parseDouble(p1[0]) - Double.parseDouble(p2[0]), 2) +
Math.pow(Double.parseDouble(p1[1]) - Double.parseDouble(p2[1]), 2)); Math.pow(Double.parseDouble(p1[1]) - Double.parseDouble(p2[1]), 2));
System.out.printf("A distância entre os dois pontos é %f", distance); System.out.printf("A distância entre os dois pontos é %.3f", distance);
sin.close();
} }
} }

View File

@ -16,6 +16,8 @@ public class EnergyToHeatWater {
System.out.print("Temperatura final da água (ºC)? "); System.out.print("Temperatura final da água (ºC)? ");
double finalTemperature = sin.nextDouble(); double finalTemperature = sin.nextDouble();
double energy = kgOfWater * (finalTemperature - initialTemperature) * 4184; double energy = kgOfWater * (finalTemperature - initialTemperature) * 4184;
System.out.printf("Para aquecer %fkg de água de %fºC para %fºC, serão necessários %fJ de energia.", kgOfWater, initialTemperature, finalTemperature, energy); System.out.printf("Para aquecer %.3fkg de água de %.2fºC para %.2fºC, serão necessários %.3fJ de energia.", kgOfWater, initialTemperature, finalTemperature, energy);
sin.close();
} }
} }

View File

@ -14,6 +14,8 @@ public class Investment {
System.out.print("Taxa de juro mensal (%)? "); System.out.print("Taxa de juro mensal (%)? ");
double tax = sin.nextDouble(); double tax = sin.nextDouble();
double finalWallet = initialWallet * Math.pow(1 + tax/100, 3); double finalWallet = initialWallet * Math.pow(1 + tax/100, 3);
System.out.printf("O saldo final será de %f euros", finalWallet); System.out.printf("O saldo final será de %.2f euros", finalWallet);
sin.close();
} }
} }

View File

@ -12,6 +12,8 @@ public class KmToMiles {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);
double km = UserInput.getPositiveNumber(sin); double km = UserInput.getPositiveNumber(sin);
double miles = km / 1.609; double miles = km / 1.609;
System.out.printf("%fkm = %f miles", km, miles); System.out.printf("%.3fkm = %.3f miles", km, miles);
sin.close();
} }
} }

View File

@ -17,11 +17,10 @@ public class PythagoreanTheorem {
double b = UserInput.getPositiveNumber(sin); double b = UserInput.getPositiveNumber(sin);
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
double angDeg = Math.acos(a / c) * 180 / Math.PI;
double cossin = a / c; System.out.printf("O comprimento da hipotenusa é %.2f e o valor do angulo entre o cateto A e a hipotenusa é %.2f°", c, angDeg);
double angRad = Math.acos(cossin);
double angDeg = angRad * 180 / Math.PI;
System.out.printf("O comprimento da hipotenusa é %f e o valor do angulo entre o cateto A e a hipotenusa é %f°", MathTools.round(c, 2), MathTools.round(angDeg, 2)); sin.close();
} }
} }

View File

@ -29,5 +29,7 @@ public class RealNumbers {
} }
System.out.printf("Valor máximo: %f\nValor mínimo: %f\nMédia: %f\nTotal: %f", max, min, (float) sum/readNumbers, sum); System.out.printf("Valor máximo: %f\nValor mínimo: %f\nMédia: %f\nTotal: %f", max, min, (float) sum/readNumbers, sum);
sin.close();
} }
} }

View File

@ -17,5 +17,7 @@ public class SecsToHMS {
int hours = Math.round((float) (mins / 60)); int hours = Math.round((float) (mins / 60));
mins = mins % 60; mins = mins % 60;
System.out.printf("%d segundos no formato hh:mm:ss : %d:%d:%d", totalSecs, hours, mins, secs); System.out.printf("%d segundos no formato hh:mm:ss : %d:%d:%d", totalSecs, hours, mins, secs);
sin.close();
} }
} }

View File

@ -0,0 +1,6 @@
# Programação Orientada a Objetos
## Exercícios TP
### Resoluções para exercícios sugeridos nas aulas Teórico-Práticas
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

View File

@ -0,0 +1,59 @@
package tp_codecheck.tp01;
// Solução do exercício 2
import java.util.Scanner;
/**
This program updates an account balance, according to the table below:
Balance Interest Rate Charge
> $100,000.00 2.75 % $ 0.00
> $25,000.00 2.00 % $ 0.00
> $10,000.00 1.00 % $ 0.00
>= $0.00 0.00 % $ 0.00
< $0.00 0.00 % $ 25.00
and prints out the new balance.
*/
public class BankInterest
{
public static void main(String[] args)
{
// Define constants
final double HI_RATE = 2.75;
final double MD_RATE = 2.00;
final double LO_RATE = 1.00;
final double ZERO_RATE = 0.00;
final double DEB_CHG = -25.00;
final double HI_LIMIT = 100000.00;
final double MD_LIMIT = 25000.00;
final double LO_LIMIT = 10000.00;
final double ZERO_LIMIT = 0.00;
// Print prompt to enter a current balence
System.out.print("Enter current balance: ");
// Read balance
Scanner in = new Scanner(System.in);
double balance = in.nextDouble();
// Determine interest rate (or charge) based on current balance
// to compute new balance
// Your work here
double newBalance;
if (balance > HI_LIMIT)
newBalance = balance*(1+HI_RATE/100);
else if (balance > MD_LIMIT)
newBalance = balance*(1+MD_RATE/100);
else if (balance > LO_LIMIT)
newBalance = balance*(1+LO_RATE/100);
else if (balance < ZERO_LIMIT)
newBalance = balance+DEB_CHG;
else
newBalance = balance;
System.out.printf("%.2f\n", newBalance);
}
}

View File

@ -0,0 +1,28 @@
package tp_codecheck.tp01;
// Solução do exercício 3
public class Table {
public static void main(String[] args) {
System.out.printf("%s | %s | %s | %s\n", "n", "Hn", "log n", "Hn - log n");
int n = 1;
while (n <= 1000000) {
double f1 = harmonic(n);
double f2 = Math.log((double)n);
System.out.printf("%d | %.3f | %.3f | %.9f\n", n, f1, f2, f1-f2);
n *= 2;
}
}
/**
* Computes the Harmonic number Hn = 1 + 1/2 + 1/3 + ... + 1/n.
*/
private static double harmonic(int n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1/(float)i;
}
return sum;
}
}

View File

@ -0,0 +1,29 @@
package tp_codecheck.tp01;
// Solução do exercício 1
/*
This is a short Java program to convert
a temperature from Celsius to Fahrenheit.
*/
// You may want to import stuff here...
import java.util.Scanner;
public class Temperature {
// Create a Scanner to parse standard input:
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Put your code here
System.out.print("Celsius? ");
double celcius = sc.nextDouble();
double fahrenheit = 1.8*celcius+32;
System.out.printf("%f C = %f F\n", celcius, fahrenheit);
System.out.println("THE END");
}
}
// JMR 2023

View File

@ -0,0 +1,23 @@
# Exercícios de Arrays
## Ex1
```java
final int LENGHT = 100;
int[] a = new int[LENGHT];
// Code for filling a ommited
for (int = 99; i >= 0; i--)
{
System.out.print(a[i]);
if (i > 0) { System.out.print(', '); }
}
```
## Ex2
```java
int[] numbers = new int[100];
for (int k = 0; k < numbers.length; k++)
{
numbers[k] = k + 1;
}
```

View File

@ -0,0 +1,27 @@
package tp_codecheck.tp02;
import java.util.Scanner;
public class NumberOfDays {
public static void main(String[] args)
{
// Declare and initialize daysOfMonth
int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Scanner in = new Scanner(System.in);
System.out.print("Month (1 - 12): ");
int month = in.nextInt();
System.out.print("Year: ");
int year = in.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
// It's a leap year. Adjust the entry for February
daysOfMonth[1]+=1;
}
// Get the number of days in the given month
int days = daysOfMonth[month-1];
System.out.println("Number of days: " + days);
}
}

View File

@ -0,0 +1,17 @@
package tp_codecheck.tp02;
public class Numbers {
public static void main(String[] args) {
// Different arrays will be substituted here.
int[] values = { 3, 1, 4, 1, 5, 9 };
int[] newValues = new int[values.length/2];
for (int x = 0; x < values.length; x+=2) {
newValues[x/2] = values[x];
}
for (int i = 0; i < newValues.length; i++) {
System.out.println(newValues[i]);
}
}
}

View File

@ -0,0 +1,9 @@
# Tabela Exercício 1 de Strings
| Question | Answer |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|
| What is the length of the string below?<br>String str = "Java Program" | 12 |
| With str as defined above, give a call to the substring method that returns the substring "gram". | str.substring(8) |
| Use the string concatenation operator to change the string variable str to contain the string "Java Programming". | str += "ming" |
| What does the following statement sequence print?<br><br>String str = "Harry";<br>int n = str.length();<br>String mystery = str.substring(0, 1) + str.substring(n - 1, n);<br>System.out.println(mystery); | Hy |
| Consider the following statement sequence. If the user provides the input John Q. Public, what is printed? If an error occurs, type error.<br><br>Scanner in = new Scanner(System.in);<br>String first = in.next();<br>String last = in.next();<br>System.out.println(last + ", " + first); | Q., John |

View File

@ -0,0 +1,12 @@
package tp_codecheck.tp02;
import java.util.Scanner;
public class Words {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String word = in.next();
word = word.charAt(word.length()-1) + word.substring(1, word.length()-1) + word.charAt(0);
System.out.println(word);
}
}