POO: aula02 added (except for extra)

This commit is contained in:
TiagoRG 2023-02-17 23:33:57 +00:00
parent d66bf7f25f
commit 322b091371
23 changed files with 204 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,32 @@
import java.util.Scanner;
// Solução do exercício 5
public class AverageSpeed {
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
System.out.println("v1? ");
double v1;
do {
v1 = sin.nextDouble();
} while (v1 <= 0);
System.out.println("d1? ");
double d1;
do {
d1 = sin.nextDouble();
} while (d1 <= 0);
System.out.println("v2? ");
double v2;
do {
v2 = sin.nextDouble();
} while (v2 <= 0);
System.out.println("d2? ");
double d2;
do {
d2 = sin.nextDouble();
} while (d2 <= 0);
double vm = (d1 + d2) / ((d1 / v1) + (d2 / v2));
System.out.printf("Velocidade final da viagem: %fkm/h", vm);
}
}

View File

@ -0,0 +1,13 @@
import java.util.Scanner;
// Solução do exercício 2
public class CelciusToFahrenheit {
public static void main(String[] args) {
System.out.print("ºC? ");
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);
}
}

View File

@ -0,0 +1,15 @@
import java.util.Scanner;
// Solução do exercício 9
public class Countdown {
public static void main(String[] args){
Scanner sin = new Scanner(System.in);
System.out.print("N? ");
int n = sin.nextInt();
for (int i = n; i >= 0; i--) {
System.out.print(i%10 == 0 ? i + "\n" : i + ",");
}
}
}

View File

@ -0,0 +1,20 @@
import java.util.Scanner;
// Solução do exercício 7
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(",");
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);
}
}

View File

@ -0,0 +1,17 @@
import java.util.Scanner;
// Solução do exercício 3
public class EnergyToHeatWater {
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
System.out.print("Kg de água? ");
double kgOfWater = sin.nextDouble();
System.out.print("Temperatura inicial da água (ºC)? ");
double initialTemperature = sin.nextDouble();
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);
}
}

View File

@ -0,0 +1,15 @@
import java.util.Scanner;
// Solução do exercício 4
public class Investment {
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
System.out.print("Saldo inicial (Euros)? ");
double initialWallet = sin.nextDouble();
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);
}
}

View File

@ -0,0 +1,13 @@
import java.util.Scanner;
// Solução do exercício 1
public class KmToMiles {
public static void main(String[] args) {
System.out.print("Km? ");
Scanner sin = new Scanner(System.in);
double km = sin.nextDouble();
double miles = km / 1.609;
System.out.printf("%fkm = %f miles", km, miles);
}
}

View File

@ -0,0 +1,28 @@
import java.util.Scanner;
// Solução do exercício 8
public class PythagoreanTheorem {
public static void main(String[] args){
Scanner sin = new Scanner(System.in);
System.out.println("Cateto A:");
double a;
do {
a = sin.nextDouble();
} while (a <= 0);
System.out.println("Cateto B:");
double b;
do {
b = sin.nextDouble();
} while (b <= 0);
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
double cossin = a / c;
double angRad = Math.acos(cossin);
double angDeg = angRad * 180 / Math.PI;
System.out.printf("O comprimento da hipotenusa é %f e o valor do angulo entre o cateto A e a hipotenusa é %f°", (float) (Math.round(c * 100) / 100), (float) (Math.round(angDeg * 100) / 100));
}
}

View File

@ -0,0 +1,31 @@
import java.util.Scanner;
// Solução do exercício 10
public class RealNumbers {
public static void main(String[] args){
Scanner sin = new Scanner(System.in);
int readNumbers = 1;
System.out.println("Introduza os diversos números.");
double first = sin.nextDouble();
double max = first;
double min = first;
double sum = first;
while (true) {
double n = sin.nextDouble();
if (n == first)
break;
if (n > max)
max = n;
if (n < min)
min = n;
sum += n;
++readNumbers;
}
System.out.printf("Valor máximo: %f\nValor mínimo: %f\nMédia: %f\nTotal: %f", max, min, (float) sum/readNumbers, sum);
}
}

View File

@ -0,0 +1,20 @@
import java.util.Scanner;
// Solução do exercício 6
public class SecsToHMS {
public static void main(String[] args){
Scanner sin = new Scanner(System.in);
System.out.println("Introduza os segundos totais: ");
int totalSecs;
do {
totalSecs = sin.nextInt();
} while (totalSecs <= 0);
int secs = totalSecs % 60;
int mins = Math.round((float) (totalSecs / 60));
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);
}
}

Binary file not shown.

Binary file not shown.