diff --git a/1ano/2semestre/poo/out/production/poo/aula02/AverageSpeed.class b/1ano/2semestre/poo/out/production/poo/aula02/AverageSpeed.class index b8aff85..c4eab1f 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/AverageSpeed.class and b/1ano/2semestre/poo/out/production/poo/aula02/AverageSpeed.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/CelciusToFahrenheit.class b/1ano/2semestre/poo/out/production/poo/aula02/CelciusToFahrenheit.class index b4898f0..fd19361 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/CelciusToFahrenheit.class and b/1ano/2semestre/poo/out/production/poo/aula02/CelciusToFahrenheit.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/Countdown.class b/1ano/2semestre/poo/out/production/poo/aula02/Countdown.class index 7f1b31f..1233309 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/Countdown.class and b/1ano/2semestre/poo/out/production/poo/aula02/Countdown.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/DistanceBetweenPoints.class b/1ano/2semestre/poo/out/production/poo/aula02/DistanceBetweenPoints.class index 234bebc..122f743 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/DistanceBetweenPoints.class and b/1ano/2semestre/poo/out/production/poo/aula02/DistanceBetweenPoints.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/EnergyToHeatWater.class b/1ano/2semestre/poo/out/production/poo/aula02/EnergyToHeatWater.class index 469b999..858177d 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/EnergyToHeatWater.class and b/1ano/2semestre/poo/out/production/poo/aula02/EnergyToHeatWater.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/Investment.class b/1ano/2semestre/poo/out/production/poo/aula02/Investment.class index ac34fb2..1826b61 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/Investment.class and b/1ano/2semestre/poo/out/production/poo/aula02/Investment.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/KmToMiles.class b/1ano/2semestre/poo/out/production/poo/aula02/KmToMiles.class index 0bdc5de..0d2e8cc 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/KmToMiles.class and b/1ano/2semestre/poo/out/production/poo/aula02/KmToMiles.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/PythagoreanTheorem.class b/1ano/2semestre/poo/out/production/poo/aula02/PythagoreanTheorem.class index 619c224..bf8e1f6 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/PythagoreanTheorem.class and b/1ano/2semestre/poo/out/production/poo/aula02/PythagoreanTheorem.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/README.md b/1ano/2semestre/poo/out/production/poo/aula02/README.md index 4a3635a..de42a52 100755 --- a/1ano/2semestre/poo/out/production/poo/aula02/README.md +++ b/1ano/2semestre/poo/out/production/poo/aula02/README.md @@ -5,5 +5,14 @@ * [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) +### 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) diff --git a/1ano/2semestre/poo/out/production/poo/aula02/RealNumbers.class b/1ano/2semestre/poo/out/production/poo/aula02/RealNumbers.class index 6460fb1..e2a5591 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/RealNumbers.class and b/1ano/2semestre/poo/out/production/poo/aula02/RealNumbers.class differ diff --git a/1ano/2semestre/poo/out/production/poo/aula02/SecsToHMS.class b/1ano/2semestre/poo/out/production/poo/aula02/SecsToHMS.class index d09e420..e78b87b 100644 Binary files a/1ano/2semestre/poo/out/production/poo/aula02/SecsToHMS.class and b/1ano/2semestre/poo/out/production/poo/aula02/SecsToHMS.class differ diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/BankInterest.class b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/BankInterest.class new file mode 100644 index 0000000..0651117 Binary files /dev/null and b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/BankInterest.class differ diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/Table.class b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/Table.class new file mode 100644 index 0000000..15ff93c Binary files /dev/null and b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/Table.class differ diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/Temperature.class b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/Temperature.class new file mode 100644 index 0000000..9c46d70 Binary files /dev/null and b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp01/Temperature.class differ diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Array1.md b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Array1.md new file mode 100644 index 0000000..82c4075 --- /dev/null +++ b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Array1.md @@ -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; +} +``` + diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/NumberOfDays.class b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/NumberOfDays.class new file mode 100644 index 0000000..dbb0162 Binary files /dev/null and b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/NumberOfDays.class differ diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Numbers.class b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Numbers.class new file mode 100644 index 0000000..8323d22 Binary files /dev/null and b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Numbers.class differ diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Strings1.md b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Strings1.md new file mode 100644 index 0000000..ea8d514 --- /dev/null +++ b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Strings1.md @@ -0,0 +1,9 @@ +# Tabela Exercício 1 de Strings + +| Question | Answer | +|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------| +| What is the length of the string below?
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?

String str = "Harry";
int n = str.length();
String mystery = str.substring(0, 1) + str.substring(n - 1, n);
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.

Scanner in = new Scanner(System.in);
String first = in.next();
String last = in.next();
System.out.println(last + ", " + first); | Q., John | \ No newline at end of file diff --git a/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Words.class b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Words.class new file mode 100644 index 0000000..aa87939 Binary files /dev/null and b/1ano/2semestre/poo/out/production/poo/tp_codecheck/tp02/Words.class differ diff --git a/1ano/2semestre/poo/out/production/poo/utils/UserInput.class b/1ano/2semestre/poo/out/production/poo/utils/UserInput.class index 4ea1b41..9748a8d 100644 Binary files a/1ano/2semestre/poo/out/production/poo/utils/UserInput.class and b/1ano/2semestre/poo/out/production/poo/utils/UserInput.class differ diff --git a/1ano/2semestre/poo/src/aula02/AverageSpeed.java b/1ano/2semestre/poo/src/aula02/AverageSpeed.java index 532aebb..e5fbe7a 100644 --- a/1ano/2semestre/poo/src/aula02/AverageSpeed.java +++ b/1ano/2semestre/poo/src/aula02/AverageSpeed.java @@ -18,6 +18,8 @@ public class AverageSpeed { double d2 = UserInput.getPositiveNumber(sin); 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(); } } diff --git a/1ano/2semestre/poo/src/aula02/CelciusToFahrenheit.java b/1ano/2semestre/poo/src/aula02/CelciusToFahrenheit.java index fe12735..8d4d260 100644 --- a/1ano/2semestre/poo/src/aula02/CelciusToFahrenheit.java +++ b/1ano/2semestre/poo/src/aula02/CelciusToFahrenheit.java @@ -10,6 +10,8 @@ public class CelciusToFahrenheit { Scanner sin = new Scanner(System.in); double celcius = sin.nextDouble(); 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(); } } diff --git a/1ano/2semestre/poo/src/aula02/Countdown.java b/1ano/2semestre/poo/src/aula02/Countdown.java index a3d0919..d04bffb 100644 --- a/1ano/2semestre/poo/src/aula02/Countdown.java +++ b/1ano/2semestre/poo/src/aula02/Countdown.java @@ -11,7 +11,11 @@ public class Countdown { System.out.print("N? "); int n = sin.nextInt(); 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 + ","); } + + sin.close(); } } \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula02/DistanceBetweenPoints.java b/1ano/2semestre/poo/src/aula02/DistanceBetweenPoints.java index b1c5554..af1c152 100644 --- a/1ano/2semestre/poo/src/aula02/DistanceBetweenPoints.java +++ b/1ano/2semestre/poo/src/aula02/DistanceBetweenPoints.java @@ -1,5 +1,7 @@ package aula02; +import utils.UserInput; + import java.util.Scanner; // Solução do exercício 7 @@ -8,15 +10,15 @@ public class DistanceBetweenPoints { public static void main(String[] args){ Scanner sin = new Scanner(System.in); - System.out.print("Coordenadas do ponto 1 (separadas por ','): "); - String[] p1 = sin.next().split(","); - System.out.print("Coordenadas do ponto 2 (separadas por ','): "); - String[] p2 = sin.next().split(","); + String[] p1 = UserInput.input(sin, "Coordenadas do ponto 1 (separadas por ','): ").split(","); + String[] p2 = UserInput.input(sin, "Coordenadas do ponto 2 (separadas por ','): ").split(","); double distance = Math.sqrt( Math.pow(Double.parseDouble(p1[0]) - Double.parseDouble(p2[0]), 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(); } } \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula02/EnergyToHeatWater.java b/1ano/2semestre/poo/src/aula02/EnergyToHeatWater.java index dcdbef5..1892a6f 100644 --- a/1ano/2semestre/poo/src/aula02/EnergyToHeatWater.java +++ b/1ano/2semestre/poo/src/aula02/EnergyToHeatWater.java @@ -16,6 +16,8 @@ public class EnergyToHeatWater { System.out.print("Temperatura final da água (ºC)? "); double finalTemperature = sin.nextDouble(); 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(); } } diff --git a/1ano/2semestre/poo/src/aula02/Investment.java b/1ano/2semestre/poo/src/aula02/Investment.java index 53e9844..d1957b2 100644 --- a/1ano/2semestre/poo/src/aula02/Investment.java +++ b/1ano/2semestre/poo/src/aula02/Investment.java @@ -14,6 +14,8 @@ public class Investment { System.out.print("Taxa de juro mensal (%)? "); double tax = sin.nextDouble(); 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(); } } diff --git a/1ano/2semestre/poo/src/aula02/KmToMiles.java b/1ano/2semestre/poo/src/aula02/KmToMiles.java index bd8b965..2bd5b8a 100644 --- a/1ano/2semestre/poo/src/aula02/KmToMiles.java +++ b/1ano/2semestre/poo/src/aula02/KmToMiles.java @@ -12,6 +12,8 @@ public class KmToMiles { Scanner sin = new Scanner(System.in); double km = UserInput.getPositiveNumber(sin); double miles = km / 1.609; - System.out.printf("%fkm = %f miles", km, miles); + System.out.printf("%.3fkm = %.3f miles", km, miles); + + sin.close(); } } \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula02/PythagoreanTheorem.java b/1ano/2semestre/poo/src/aula02/PythagoreanTheorem.java index c912169..4487e52 100644 --- a/1ano/2semestre/poo/src/aula02/PythagoreanTheorem.java +++ b/1ano/2semestre/poo/src/aula02/PythagoreanTheorem.java @@ -17,11 +17,10 @@ public class PythagoreanTheorem { double b = UserInput.getPositiveNumber(sin); 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; - double angRad = Math.acos(cossin); - double angDeg = angRad * 180 / Math.PI; + System.out.printf("O comprimento da hipotenusa é %.2f e o valor do angulo entre o cateto A e a hipotenusa é %.2f°", c, angDeg); - 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(); } } \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula02/RealNumbers.java b/1ano/2semestre/poo/src/aula02/RealNumbers.java index 3875fb7..56d5c96 100644 --- a/1ano/2semestre/poo/src/aula02/RealNumbers.java +++ b/1ano/2semestre/poo/src/aula02/RealNumbers.java @@ -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); + + sin.close(); } } \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula02/SecsToHMS.java b/1ano/2semestre/poo/src/aula02/SecsToHMS.java index 1393d99..194127c 100644 --- a/1ano/2semestre/poo/src/aula02/SecsToHMS.java +++ b/1ano/2semestre/poo/src/aula02/SecsToHMS.java @@ -17,5 +17,7 @@ public class SecsToHMS { int hours = Math.round((float) (mins / 60)); mins = mins % 60; System.out.printf("%d segundos no formato hh:mm:ss : %d:%d:%d", totalSecs, hours, mins, secs); + + sin.close(); } } \ No newline at end of file diff --git a/1ano/2semestre/poo/src/tp_codecheck/README.md b/1ano/2semestre/poo/src/tp_codecheck/README.md new file mode 100755 index 0000000..f49dbb0 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/README.md @@ -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) diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp01/BankInterest.java b/1ano/2semestre/poo/src/tp_codecheck/tp01/BankInterest.java new file mode 100644 index 0000000..6397ad3 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp01/BankInterest.java @@ -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); + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp01/Table.java b/1ano/2semestre/poo/src/tp_codecheck/tp01/Table.java new file mode 100644 index 0000000..07fc547 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp01/Table.java @@ -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; + } +} diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp01/Temperature.java b/1ano/2semestre/poo/src/tp_codecheck/tp01/Temperature.java new file mode 100644 index 0000000..abb7a6c --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp01/Temperature.java @@ -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 \ No newline at end of file diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp02/Array1.md b/1ano/2semestre/poo/src/tp_codecheck/tp02/Array1.md new file mode 100644 index 0000000..82c4075 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp02/Array1.md @@ -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; +} +``` + diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp02/NumberOfDays.java b/1ano/2semestre/poo/src/tp_codecheck/tp02/NumberOfDays.java new file mode 100644 index 0000000..401c3c1 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp02/NumberOfDays.java @@ -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); + } +} diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp02/Numbers.java b/1ano/2semestre/poo/src/tp_codecheck/tp02/Numbers.java new file mode 100644 index 0000000..ebaa91c --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp02/Numbers.java @@ -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]); + } + } +} diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp02/Strings1.md b/1ano/2semestre/poo/src/tp_codecheck/tp02/Strings1.md new file mode 100644 index 0000000..ea8d514 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp02/Strings1.md @@ -0,0 +1,9 @@ +# Tabela Exercício 1 de Strings + +| Question | Answer | +|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------| +| What is the length of the string below?
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?

String str = "Harry";
int n = str.length();
String mystery = str.substring(0, 1) + str.substring(n - 1, n);
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.

Scanner in = new Scanner(System.in);
String first = in.next();
String last = in.next();
System.out.println(last + ", " + first); | Q., John | \ No newline at end of file diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp02/Words.java b/1ano/2semestre/poo/src/tp_codecheck/tp02/Words.java new file mode 100644 index 0000000..d2fdf36 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp02/Words.java @@ -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); + } +}