From cf85b02b694208c72c9984eb0ca10ed253d5692f Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 25 Feb 2023 17:53:42 +0000 Subject: [PATCH] Moved isNumPrime to MathTools and made Student static --- 1ano/2semestre/poo/src/aula03/Grades.java | 18 +++++++++--------- 1ano/2semestre/poo/src/aula03/PrimesSum.java | 12 ++---------- 1ano/2semestre/poo/src/utils/MathTools.java | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/1ano/2semestre/poo/src/aula03/Grades.java b/1ano/2semestre/poo/src/aula03/Grades.java index 0407fbb..4af54d7 100644 --- a/1ano/2semestre/poo/src/aula03/Grades.java +++ b/1ano/2semestre/poo/src/aula03/Grades.java @@ -31,16 +31,16 @@ public class Grades { 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; + private static 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); + 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/PrimesSum.java b/1ano/2semestre/poo/src/aula03/PrimesSum.java index 13425ad..b93a897 100644 --- a/1ano/2semestre/poo/src/aula03/PrimesSum.java +++ b/1ano/2semestre/poo/src/aula03/PrimesSum.java @@ -1,5 +1,6 @@ package aula03; +import utils.MathTools; import utils.UserInput; import java.util.Scanner; @@ -14,20 +15,11 @@ public class PrimesSum { int sum = 0; for (int i = 0; i <= n; i++) - if (isNumPrime(i)) + if (MathTools.isNumPrime(i)) sum += i; System.out.printf("A soma dos números primos até %d é %d\n", n, sum); 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; - } } diff --git a/1ano/2semestre/poo/src/utils/MathTools.java b/1ano/2semestre/poo/src/utils/MathTools.java index fa7c389..8744e9b 100644 --- a/1ano/2semestre/poo/src/utils/MathTools.java +++ b/1ano/2semestre/poo/src/utils/MathTools.java @@ -1,7 +1,23 @@ package utils; +import java.util.Random; + public class MathTools { public static double round(double n, int 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; + } }