From befd8f5b71434c400fd3155592d13547230fa981 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 25 Feb 2023 15:26:39 +0000 Subject: [PATCH] POO aula03 finished --- 1ano/2semestre/poo/src/aula03/Calendar.java | 81 +++++++++++++++++++ 1ano/2semestre/poo/src/aula03/Grades.java | 46 +++++++++++ 1ano/2semestre/poo/src/aula03/HiLo.java | 2 + 1ano/2semestre/poo/src/aula03/Investment.java | 3 +- 1ano/2semestre/poo/src/aula03/PrimesSum.java | 2 +- 1ano/2semestre/poo/src/aula03/README.md | 2 + 6 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula03/Calendar.java create mode 100644 1ano/2semestre/poo/src/aula03/Grades.java diff --git a/1ano/2semestre/poo/src/aula03/Calendar.java b/1ano/2semestre/poo/src/aula03/Calendar.java new file mode 100644 index 0000000..ba3711c --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/Calendar.java @@ -0,0 +1,81 @@ +package aula03; + +import utils.UserInput; +import java.util.Scanner; + +// Solução do exercício 5 + +public class Calendar { + public static void main(String[] args) { + // Data contains: {month, year, firstDay} + int[] data = getValues(); + int monthDays = monthDays(data[0], data[1]); + printCalendar(data, monthDays); + System.out.println(); + } + + private static int[] getValues() { + Scanner sin = new Scanner(System.in); + + System.out.print("Introduza o mês/ano no formato mm/yyyy: "); + String[] monthYear = sin.next().split("/"); + System.out.print("1 = Segunda\n2 = Terça\n3 = Quarta\n4 = Quinta\n5 = Sexta\n6 = Sábado\n7 = Domingo\nIntroduza o dia da semana em que começa o mês (1 a 7): "); + int firstDay = (int) UserInput.getNumberBetween(sin, 1, 7); + + sin.close(); + + return new int[]{Integer.parseInt(monthYear[0]), Integer.parseInt(monthYear[1]), firstDay}; + } + + private static int monthDays(int month, int year) { + switch (month) { + case 2: + if (year % 100 == 0 ? year % 400 == 0 : year % 4 == 0) + return 29; + else + return 28; + case 4: + case 6: + case 9: + case 11: + return 30; + default: + return 31; + } + } + + private static void printCalendar(int[] data, int monthDays) { + System.out.printf("\n%13s %d\n", monthName(data[0]), data[1]); + System.out.println("Dom Seg Ter Qua Qui Sex Sab"); + + if (data[2] != 7) + for (int i = 0; i < data[2]; i++) + System.out.print(" "); + + for (int monthDay = 1; monthDay <= monthDays; monthDay++) { + System.out.printf("%3d ", monthDay); + if ((monthDay + data[2]) % 7 == 0) + 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"; + }; + } +} diff --git a/1ano/2semestre/poo/src/aula03/Grades.java b/1ano/2semestre/poo/src/aula03/Grades.java new file mode 100644 index 0000000..0407fbb --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/Grades.java @@ -0,0 +1,46 @@ +package aula03; + +import java.util.Scanner; +import utils.UserInput; + +// Solução do exercício 4 + +public class Grades { + private static final Scanner sin = new Scanner(System.in); + + public static void main(String[] args) { + System.out.print("Quantos alunos tem a turma? "); + int studentCount = sin.nextInt(); + Student[] students = new Student[studentCount]; + + for (int i = 0; i < studentCount; i++) { + System.out.printf("Nota teórica do aluno %d: ", i+1); + double notaT = UserInput.getNumberBetween(sin, 0, 20); + System.out.printf("Nota prática do aluno %d: ", i+1); + double notaP = UserInput.getNumberBetween(sin, 0, 20); + students[i] = new Student(notaT, notaP); + } + + printGrades(students); + System.out.println(); + } + + private static void printGrades(Student[] students) { + System.out.println("NotaT NotaP Pauta"); + for (Student student : students) { + System.out.printf("%5.1f %5.1f %5d\n", student.notaT, student.notaP, student.notaFinal); + } + } +} + +class Student { + public double notaT; + public double notaP; + public int notaFinal; + + public Student(double notaT, double notaP) { + this.notaT = notaT; + this.notaP = notaP; + this.notaFinal = (notaT < 7 || notaP < 7) ? 66 : (int) Math.round(0.4 * notaT + 0.6 * notaP); + } +} diff --git a/1ano/2semestre/poo/src/aula03/HiLo.java b/1ano/2semestre/poo/src/aula03/HiLo.java index 9c1f5ab..b95fbb5 100644 --- a/1ano/2semestre/poo/src/aula03/HiLo.java +++ b/1ano/2semestre/poo/src/aula03/HiLo.java @@ -4,6 +4,8 @@ import utils.UserInput; import java.util.Random; import java.util.Scanner; +// Solução do exercício 3 + public class HiLo { private static final Scanner sin = new Scanner(System.in); private static final Random rand = new Random(); diff --git a/1ano/2semestre/poo/src/aula03/Investment.java b/1ano/2semestre/poo/src/aula03/Investment.java index 3a0910f..fac08a3 100644 --- a/1ano/2semestre/poo/src/aula03/Investment.java +++ b/1ano/2semestre/poo/src/aula03/Investment.java @@ -1,9 +1,10 @@ package aula03; import utils.UserInput; - import java.util.Scanner; +// Solução do exercício 2 + public class Investment { public static void main(String[] args) { Scanner sin = new Scanner(System.in); diff --git a/1ano/2semestre/poo/src/aula03/PrimesSum.java b/1ano/2semestre/poo/src/aula03/PrimesSum.java index 21ec79c..13425ad 100644 --- a/1ano/2semestre/poo/src/aula03/PrimesSum.java +++ b/1ano/2semestre/poo/src/aula03/PrimesSum.java @@ -17,7 +17,7 @@ public class PrimesSum { if (isNumPrime(i)) sum += i; - System.out.printf("A soma dos números primos até %d é %d", n, sum); + System.out.printf("A soma dos números primos até %d é %d\n", n, sum); sin.close(); } diff --git a/1ano/2semestre/poo/src/aula03/README.md b/1ano/2semestre/poo/src/aula03/README.md index 2faa8cb..530859c 100755 --- a/1ano/2semestre/poo/src/aula03/README.md +++ b/1ano/2semestre/poo/src/aula03/README.md @@ -11,6 +11,8 @@ | 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) | +| 4 | [Grades.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula03/Grades.java) | +| 5 | [Calendar.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula03/Calendar.java) | --- *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)