Moved isNumPrime to MathTools and made Student static

This commit is contained in:
TiagoRG 2023-02-25 17:53:42 +00:00
parent f804a55cdc
commit cf85b02b69
3 changed files with 27 additions and 19 deletions

View File

@ -31,16 +31,16 @@ public class Grades {
System.out.printf("%5.1f %5.1f %5d\n", student.notaT, student.notaP, student.notaFinal); System.out.printf("%5.1f %5.1f %5d\n", student.notaT, student.notaP, student.notaFinal);
} }
} }
}
class Student { private static class Student {
public double notaT; public double notaT;
public double notaP; public double notaP;
public int notaFinal; public int notaFinal;
public Student(double notaT, double notaP) { public Student(double notaT, double notaP) {
this.notaT = notaT; this.notaT = notaT;
this.notaP = notaP; this.notaP = notaP;
this.notaFinal = (notaT < 7 || notaP < 7) ? 66 : (int) Math.round(0.4 * notaT + 0.6 * notaP); this.notaFinal = (notaT < 7 || notaP < 7) ? 66 : (int) Math.round(0.4 * notaT + 0.6 * notaP);
}
} }
} }

View File

@ -1,5 +1,6 @@
package aula03; package aula03;
import utils.MathTools;
import utils.UserInput; import utils.UserInput;
import java.util.Scanner; import java.util.Scanner;
@ -14,20 +15,11 @@ public class PrimesSum {
int sum = 0; int sum = 0;
for (int i = 0; i <= n; i++) for (int i = 0; i <= n; i++)
if (isNumPrime(i)) if (MathTools.isNumPrime(i))
sum += i; sum += i;
System.out.printf("A soma dos números primos até %d é %d\n", n, sum); System.out.printf("A soma dos números primos até %d é %d\n", n, sum);
sin.close(); sin.close();
} }
private static boolean isNumPrime(int n) {
if (n == 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
} }

View File

@ -1,7 +1,23 @@
package utils; package utils;
import java.util.Random;
public class MathTools { public class MathTools {
public static double round(double n, int places) { public static double round(double n, int places) {
return (Math.round(n * Math.pow(10, places))) / Math.pow(10, places); return (Math.round(n * Math.pow(10, places))) / Math.pow(10, places);
} }
public static double random(double minLimit, double maxLimit) {
Random rand = new Random();
return rand.nextDouble(minLimit, maxLimit);
}
public static boolean isNumPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
} }