From 256d6928c668fce2ccb2eced26a281929058ce1e Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Mon, 18 Mar 2024 17:59:42 +0000 Subject: [PATCH] Feat: added CPU performance calc and comments to help the code be more readable --- .../lesson7/MohamedObjects/Main/CPU.java | 33 +++++++++++++++---- .../lesson7/MohamedObjects/Main/GPU.java | 17 ++++++++-- .../lesson7/MohamedObjects/Main/Part.java | 15 +++++++++ .../main/ComputerPartsTest.java | 21 ++++++++---- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java index 33c7ba80..7378951b 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java @@ -1,13 +1,18 @@ package com.codedifferently.lesson7.mohamedobjects.main; +// CPU is a child class of Part this means that CPU inherits name, releaseYear, and brand public class CPU extends Part { - double clockSpeed; - int cores; + // initialization of member variables for CPU + private double clockSpeed; + private int cores; + + // A constructor that takes in brand. public CPU(cpuBrand brand) { this.brand = brand.toString(); } + // Constructor for GPU that needs name, releaseYear, brand, and clockSpeed public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed) { this.name = name; checkValidYear(releaseYear); @@ -16,6 +21,7 @@ public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed) { this.clockSpeed = clockSpeed; } + // Constructor for GPU that needs name, releaseYear, brand, clockSpeed, and cores public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed, int cores) { this.name = name; checkValidYear(releaseYear); @@ -25,19 +31,34 @@ public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed, int this.cores = cores; } + // Fuctions that set the value for all the member variables in CPU public void setClockSpeed(double clockSpeed) { this.clockSpeed = clockSpeed; } - public double getClockSpeed() { - return clockSpeed; - } - public void setCores(int cores) { this.cores = cores; } + // Functions that retreve the member variables. + public double getClockSpeed() { + return clockSpeed; + } + public int getCores() { return cores; } + + // Calculates the performance for a CPU by clockSpeed to the power of the core count + public double calcPerformance() { + if (cores == 0 || clockSpeed == 0) { + return 0; + } else { + double performance = clockSpeed; + for (int i = 1; i < cores; i++) { + performance *= clockSpeed; + } + return Math.round(performance); + } + } } diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java index 9288c2f5..b113e97e 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java @@ -3,16 +3,18 @@ import java.util.HashSet; import java.util.Set; +// GPU is a child class of Part this means that GPU inherits name, releaseYear, and brand public class GPU extends Part { + // initialization of member variables for GPU private Set ports = new HashSet(); - private int vRam; - private boolean hasRayTracing; + // empty constructor for GPU public GPU() {} + // Constructor for GPU that needs name, releaseYear, brand, and vRam public GPU(String name, int releaseYear, String brand, int vRam) { this.vRam = vRam; this.name = name; @@ -21,14 +23,17 @@ public GPU(String name, int releaseYear, String brand, int vRam) { this.brand = brand; } + // Constructor for GPU that needs name, releaseYear, brand, vRam, and hasRayTracing public GPU(String name, int releaseYear, String brand, int vRam, boolean hasRayTracing) { this.vRam = vRam; this.name = name; checkValidYear(releaseYear); this.releaseYear = releaseYear; this.brand = brand; + this.hasRayTracing = hasRayTracing; } + // Fuctions that set the value for all the member variables in GPU public void setvRam(int vRam) { this.vRam = vRam; } @@ -37,20 +42,26 @@ public void setHasRayTracing(boolean hasRayTracing) { this.hasRayTracing = hasRayTracing; } + // Functions that retreve the member variables. public int getvRam() { return vRam; } + public boolean getHasRayTracing() { + return hasRayTracing; + } + public Set getPorts() { return ports; } + // Adds ports to a set that contains ports public void addPorts(portType port) { this.ports.add(port); } + // A function that returns number of ports public int calculateNumPorts() { - // this was set to a loop to meet project reqirements return ports.size(); } } diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java index 52df260f..202aec2b 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java @@ -1,12 +1,15 @@ package com.codedifferently.lesson7.mohamedobjects.main; public class Part { + // initialization of member variables for part String name; int releaseYear; String brand; + // empty constructor for Part public Part() {} + // constructor for Part that requires name, realeaseYear, and brand public Part(String name, int releaseYear, String brand) { this.name = name; checkValidYear(releaseYear); @@ -14,6 +17,7 @@ public Part(String name, int releaseYear, String brand) { this.brand = brand; } + // Fuctions that set the value for all the member variables in Part public void setBrand(String brand) { this.brand = brand; } @@ -27,6 +31,7 @@ public void setReleaseYear(int releaseYear) { this.releaseYear = releaseYear; } + // Functions that retreve the member variables. public String getBrand() { return brand; } @@ -39,6 +44,16 @@ public int getReleaseYear() { return releaseYear; } + // A function that checks if the releaseYear is valid + public boolean checkValidYear() { + if (releaseYear <= 2024 && releaseYear >= 2000) { + return true; + } else { + throw new IllegalYearExeption("Year is out of expected range. 2000 - 2024"); + } + } + + // a function to check if a year was to be entered, is it valid public boolean checkValidYear(int year) { if (year <= 2024 && year >= 2000) { return true; diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java index 0dfc92f7..5dc70c9c 100644 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java @@ -8,28 +8,24 @@ import org.junit.jupiter.api.Test; class ComputerPartsTest { - CPU cpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32); - GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); Part apart = new Part(); @Test public void testCpuConstruction() { + CPU cpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32); assertEquals(23.32, cpu.getClockSpeed()); - assertNotEquals(8, cpu.getCores()); cpu.setCores(8); assertEquals(8, cpu.getCores()); - assertEquals(2020, cpu.getReleaseYear()); - assertNotEquals("M2", cpu.getName()); } @Test void testGpuConstruction() { - + GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); Set porty = Set.of(portType.DP, portType.HDMI, portType.VGA); bGpu.addPorts(portType.DP); bGpu.addPorts(portType.HDMI); @@ -41,6 +37,8 @@ void testGpuConstruction() { @Test void testYearValadation() { + GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); + assertThrows( IllegalYearExeption.class, () -> { @@ -55,11 +53,22 @@ void testYearValadation() { @Test void testPartConstruction() { + Part apart = new Part(); + apart.setBrand("A Brand"); apart.setName("A Part"); apart.setReleaseYear(2011); + assertEquals("A Brand", apart.getBrand()); assertEquals("A Part", apart.getName()); assertEquals(2011, apart.getReleaseYear()); } + + @Test + void testCpuPerformance() { + CPU acpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32); + assertEquals(0, acpu.calcPerformance()); + acpu.setCores(8); + assertEquals(Math.round(87463805373.55), acpu.calcPerformance()); + } }