From 2417cc1152072643e63e07351772a1904b71922f Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sun, 26 Feb 2023 14:03:12 +0000 Subject: [PATCH 1/4] Converted the switch case to an array --- 1ano/2semestre/poo/src/aula03/Calendar.java | 23 ++------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/1ano/2semestre/poo/src/aula03/Calendar.java b/1ano/2semestre/poo/src/aula03/Calendar.java index 6b2cdef..f1770ca 100644 --- a/1ano/2semestre/poo/src/aula03/Calendar.java +++ b/1ano/2semestre/poo/src/aula03/Calendar.java @@ -45,7 +45,8 @@ public class Calendar { } private static void printCalendar(int[] data, int monthDays) { - System.out.printf("\n%15s %d\n", monthName(data[0]), data[1]); + String[] monthNames = {"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"}; + System.out.printf("\n%15s %d\n", monthNames[data[0]-1], data[1]); System.out.println("Dom Seg Ter Qua Qui Sex Sab"); if (data[2] != 7) @@ -58,24 +59,4 @@ public class Calendar { System.out.println(); } } - - private static String monthName(int month) { - /* Note that this does not work in codecheck. - In codecheck use the usual switch case. - */ - return switch (month) { - case 1 -> "Janeiro"; - case 2 -> "Fevereiro"; - case 3 -> "Março"; - case 4 -> "Abril"; - case 5 -> "Maio"; - case 6 -> "Junho"; - case 7 -> "Julho"; - case 8 -> "Agosto"; - case 9 -> "Setembro"; - case 10 -> "Outubro"; - case 11 -> "Novembro"; - default -> "Dezembro"; - }; - } } From c94006cba85ea96390bed428594c03a559501a11 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sun, 26 Feb 2023 16:49:59 +0000 Subject: [PATCH 2/4] Refactored to utils method --- 1ano/2semestre/poo/src/aula03/Investment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1ano/2semestre/poo/src/aula03/Investment.java b/1ano/2semestre/poo/src/aula03/Investment.java index 3730a5a..92cb2cd 100644 --- a/1ano/2semestre/poo/src/aula03/Investment.java +++ b/1ano/2semestre/poo/src/aula03/Investment.java @@ -12,8 +12,8 @@ public class Investment { double investment; do { System.out.print("Introduza o investimento inicial (múltiplo de 1000): "); - investment = sin.nextInt(); - } while (investment <= 0 || investment % 1000 != 0); + investment = UserInput.getPositiveNumber(sin); + } while (investment % 1000 != 0); System.out.print("Introduza a taxa a aplicar (entre 0% e 5%): "); double tax = UserInput.getNumberBetween(sin, 0, 5); From 4ad0baa51c4244d3578522aceb182363acafbb13 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:08:45 +0000 Subject: [PATCH 3/4] POO TP3 added --- .../src/tp_codecheck/tp03/TrafficLight1.java | 66 ++++++++++++++++++ .../src/tp_codecheck/tp03/TrafficLight2.java | 68 +++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight1.java create mode 100644 1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight2.java diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight1.java b/1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight1.java new file mode 100644 index 0000000..16a2a86 --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight1.java @@ -0,0 +1,66 @@ +package tp_codecheck.tp03; + +/** + A simulated traffic light. +*/ +public class TrafficLight1 +{ + private String color; + private int reds; + + /** + Constructs a green traffic light. + */ + public TrafficLight1() + { + this.color = "green"; + this.reds = 0; + } + + /** + Constructs a traffic light. + @param initialColor the initial color "green", "yellow", or "red" + */ + public TrafficLight1(String initialColor) + { + this.color = initialColor; + this.reds = initialColor == "red" ? 1 : 0; + } + + /** + Moves this traffic light to the next color. + */ + public void next() + { + switch (this.color) { + case "red": + this.color = "green"; + break; + case "green": + this.color = "yellow"; + break; + case "yellow": + this.color = "red"; + this.reds += 1; + break; + } + } + + /** + Returns the current color of this traffic light. + @return the current color + */ + public String getColor() + { + return this.color; + } + + /** + Counts how often this traffic light has been red. + @return the number of times this traffic light has been red + */ + public int getReds() + { + return this.reds; + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight2.java b/1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight2.java new file mode 100644 index 0000000..154113d --- /dev/null +++ b/1ano/2semestre/poo/src/tp_codecheck/tp03/TrafficLight2.java @@ -0,0 +1,68 @@ +package tp_codecheck.tp03; + +/** + A simulated traffic light. +*/ +public class TrafficLight2 +{ + private int steps; + + /** + Constructs a green traffic light. + */ + public TrafficLight2() + { + this.steps = 0; + } + + /** + Constructs a traffic light. + @param initialColor the initial color "green", "yellow", or "red" + */ + public TrafficLight2(String initialColor) + { + switch (initialColor) { + case "green": + this.steps = 0; + break; + case "yellow": + this.steps = 1; + break; + case "red": + this.steps = 2; + break; + } + } + + /** + Moves this traffic light to the next color. + */ + public void next() + { + steps++; + } + + /** + Returns the current color of this traffic light. + @return the current color + */ + public String getColor() + { + int rem = (this.steps + 1) % 3; + if (rem == 0) + return "red"; + else if (rem == 1) + return "green"; + else + return "yellow"; + } + + /** + Counts how often this traffic light has been red. + @return the number of times this traffic light has been red + */ + public int getReds() + { + return (this.steps + 1) / 3; + } +} \ No newline at end of file From 0805aa366a5a3a73b562dba2939f398f2751503a Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 27 Feb 2023 17:41:01 +0000 Subject: [PATCH 4/4] Created a new version for grades to be random --- 1ano/2semestre/poo/src/aula03/Grades.java | 2 +- .../poo/src/aula03/GradesRandom.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 1ano/2semestre/poo/src/aula03/GradesRandom.java diff --git a/1ano/2semestre/poo/src/aula03/Grades.java b/1ano/2semestre/poo/src/aula03/Grades.java index 4af54d7..c3426a4 100644 --- a/1ano/2semestre/poo/src/aula03/Grades.java +++ b/1ano/2semestre/poo/src/aula03/Grades.java @@ -3,7 +3,7 @@ package aula03; import java.util.Scanner; import utils.UserInput; -// Solução do exercício 4 +// Solução do exercício 4 com o utilizador a introduzir as notas individualmente public class Grades { private static final Scanner sin = new Scanner(System.in); diff --git a/1ano/2semestre/poo/src/aula03/GradesRandom.java b/1ano/2semestre/poo/src/aula03/GradesRandom.java new file mode 100644 index 0000000..3ee3ad8 --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/GradesRandom.java @@ -0,0 +1,24 @@ +package aula03; + + +// Solução do exercício 4 com notas aleatórias + +import java.util.Random; +import java.util.Scanner; + +public class GradesRandom { + private static final Scanner sin = new Scanner(System.in); + private static final Random rand = new Random(); + + public static void main(String[] args) { + System.out.print("Quantos alunos tem a turma? "); + int studentCount = sin.nextInt(); + + System.out.println("NotaT NotaP Pauta"); + for (int i = 0; i < studentCount; i++) { + double notaT = rand.nextDouble(0, 20); + double notaP = rand.nextDouble(0, 20); + System.out.printf("%5.1f %5.1f %5d\n", notaT, notaP, (notaT < 7 || notaP < 7) ? 66 : (int) Math.round(0.4 * notaT + 0.6 * notaP)); + } + } +}