POO aula03 finished

This commit is contained in:
TiagoRG 2023-02-25 15:26:39 +00:00
parent e8ad075602
commit 9abee3f273
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
6 changed files with 134 additions and 2 deletions

View File

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

View File

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

View File

@ -4,6 +4,8 @@ import utils.UserInput;
import java.util.Random; import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
// Solução do exercício 3
public class HiLo { public class HiLo {
private static final Scanner sin = new Scanner(System.in); private static final Scanner sin = new Scanner(System.in);
private static final Random rand = new Random(); private static final Random rand = new Random();

View File

@ -1,9 +1,10 @@
package aula03; package aula03;
import utils.UserInput; import utils.UserInput;
import java.util.Scanner; import java.util.Scanner;
// Solução do exercício 2
public class Investment { public class Investment {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sin = new Scanner(System.in); Scanner sin = new Scanner(System.in);

View File

@ -17,7 +17,7 @@ public class PrimesSum {
if (isNumPrime(i)) if (isNumPrime(i))
sum += 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(); sin.close();
} }

View File

@ -11,6 +11,8 @@
| 1 | [PrimesSum.java](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/2semestre/poo/src/aula03/PrimesSum.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) | | 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) | | 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) *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)