Merge pull request #27 from TiagoRG/dev-tiagorg

This commit is contained in:
Tiago Garcia 2023-02-27 17:42:25 +00:00 committed by GitHub
commit fc86e395c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 163 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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