Skip to content

Commit

Permalink
Feat: added CPU performance calc and comments to help the code be mor…
Browse files Browse the repository at this point in the history
…e readable
  • Loading branch information
Mohamed committed Mar 18, 2024
1 parent 14e49db commit 256d692
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<portType> ports = new HashSet<portType>();

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;
Expand All @@ -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;
}
Expand All @@ -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<portType> 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();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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);
this.releaseYear = releaseYear;
this.brand = brand;
}

// Fuctions that set the value for all the member variables in Part
public void setBrand(String brand) {
this.brand = brand;
}
Expand All @@ -27,6 +31,7 @@ public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}

// Functions that retreve the member variables.
public String getBrand() {
return brand;
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<portType> porty = Set.of(portType.DP, portType.HDMI, portType.VGA);
bGpu.addPorts(portType.DP);
bGpu.addPorts(portType.HDMI);
Expand All @@ -41,6 +37,8 @@ void testGpuConstruction() {

@Test
void testYearValadation() {
GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8);

assertThrows(
IllegalYearExeption.class,
() -> {
Expand All @@ -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());
}
}

0 comments on commit 256d692

Please sign in to comment.