From c34367d155823c679e68a457f6539e53520d565f Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Mon, 10 Feb 2025 20:28:32 -0800 Subject: [PATCH] add lab05 --- src/ca/bcit/comp1510/lab05/Cone.java | 60 ++++++ src/ca/bcit/comp1510/lab05/Cube.java | 80 ++++++++ .../bcit/comp1510/lab05/GeometryDriver.java | 64 ++++++ src/ca/bcit/comp1510/lab05/Mathematics.java | 77 +++++++ .../bcit/comp1510/lab05/MathematicsTest.java | 189 ++++++++++++++++++ src/ca/bcit/comp1510/lab05/Name.java | 67 +++++++ src/ca/bcit/comp1510/lab05/NameDriver.java | 27 +++ src/ca/bcit/comp1510/lab05/Sphere.java | 57 ++++++ 8 files changed, 621 insertions(+) create mode 100644 src/ca/bcit/comp1510/lab05/Cone.java create mode 100644 src/ca/bcit/comp1510/lab05/Cube.java create mode 100644 src/ca/bcit/comp1510/lab05/GeometryDriver.java create mode 100644 src/ca/bcit/comp1510/lab05/Mathematics.java create mode 100644 src/ca/bcit/comp1510/lab05/MathematicsTest.java create mode 100644 src/ca/bcit/comp1510/lab05/Name.java create mode 100644 src/ca/bcit/comp1510/lab05/NameDriver.java create mode 100644 src/ca/bcit/comp1510/lab05/Sphere.java diff --git a/src/ca/bcit/comp1510/lab05/Cone.java b/src/ca/bcit/comp1510/lab05/Cone.java new file mode 100644 index 0000000..756dae4 --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/Cone.java @@ -0,0 +1,60 @@ +package ca.bcit.comp1510.lab05; + +import java.text.DecimalFormat; + +/** + * Cone. + * @author Braeden Sowinski + * @version 1.0.0 + * + * @param r radius + * @param h height + */ +public record Cone(int r, int h) { + + /** + * volume returns the calculated volume. + * @return double volume + */ + public double volume() { + DecimalFormat format = new DecimalFormat("#.###"); + + final double magic = 3; + double v = (Math.PI * (r * r) * h) / magic; + + return Double.parseDouble(format.format(v)); + } + + /** + * slantHeight returns the calculated slant height. + * @return double slantHeight + */ + public double slantHeight() { + DecimalFormat format = new DecimalFormat("#.###"); + + double v = Math.sqrt((r * r) + (h * h)); + + return Double.parseDouble(format.format(v)); + } + + /** + * surfaceArea returns the calculated surface area. + * @return double surfaceArea + */ + public double surfaceArea() { + DecimalFormat format = new DecimalFormat("#.###"); + + double v = Math.PI * (r * r) + + Math.PI * r * Math.sqrt((r * r) + (h * h)); + + return Double.parseDouble(format.format(v)); + } + + /** + * toString returns cone data as a string. + * @return string data + */ + public String toString() { + return r + " " + h; + } +} diff --git a/src/ca/bcit/comp1510/lab05/Cube.java b/src/ca/bcit/comp1510/lab05/Cube.java new file mode 100644 index 0000000..9b652c7 --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/Cube.java @@ -0,0 +1,80 @@ +package ca.bcit.comp1510.lab05; + +import java.text.DecimalFormat; + +/** + * Cube. + * @author Braeden Sowinski + * @version 1.0.0 + * + * @param x coord. + * @param y coord. + * @param z coord. + * @param len side length. + */ +public record Cube(int x, int y, int z, int len) { + + /** + * toString returns cube data as string. + * @return string + */ + public String toString() { + return x + " " + + y + " " + + z + " " + + len; + } + + /** + * surfaceArea returns the calculated surface + * area. + * @return int surfaceArea + */ + public int surfaceArea() { + DecimalFormat format = new DecimalFormat("#.###"); + + final int sides = 6; + int a = sides * (int) Math.pow(len, 2); + + return Integer.parseInt(format.format(a)); + } + + /** volume returns the calculated volume. + * @return int volume + */ + public int volume() { + DecimalFormat format = new DecimalFormat("#.###"); + + final int power = 3; + int v = (int) Math.pow(len, power); + + return Integer.parseInt(format.format(v)); + } + + /** + * faceDiagonal returns the face calculated + * diagonal. + * @return double faceDiagonal + */ + public double faceDiagonal() { + DecimalFormat format = new DecimalFormat("#.###"); + + double d = Math.sqrt(2) * len; + + return Double.parseDouble(format.format(d)); + } + + /** + * spaceDiagonal returns the calculated + * space diagonal. + * @return double spaceDiagonal + */ + public double spaceDiagonal() { + DecimalFormat format = new DecimalFormat("#.###"); + + final int magic = 3; + double d = Math.sqrt(magic) * len; + + return Double.parseDouble(format.format(d)); + } +} diff --git a/src/ca/bcit/comp1510/lab05/GeometryDriver.java b/src/ca/bcit/comp1510/lab05/GeometryDriver.java new file mode 100644 index 0000000..e127f32 --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/GeometryDriver.java @@ -0,0 +1,64 @@ +package ca.bcit.comp1510.lab05; + +import java.util.Scanner; + +/** + * GeometryDriver drives geo. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class GeometryDriver { + + /** + * main program entry. + * @param args unused + */ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + System.out.print("Enter a radius: "); + int r = scan.nextInt(); + System.out.print("Enter x coord: "); + int x = scan.nextInt(); + System.out.print("Enter y coord: "); + int y = scan.nextInt(); + System.out.print("Enter z coord: "); + int z = scan.nextInt(); + + Sphere sphere = new Sphere(x, y, z, r); + System.out.println("Created Sphere at " + sphere.toString() + "\n" + + "Sphere Surface Area: " + sphere.surfaceArea() + "\n" + + "Sphere Volume: " + sphere.volume() + "\n" + ); + + System.out.print("Enter side length: "); + int l = scan.nextInt(); + System.out.print("Enter x coord: "); + x = scan.nextInt(); + System.out.print("Enter y coord: "); + y = scan.nextInt(); + System.out.print("Enter z coord: "); + z = scan.nextInt(); + + Cube cube = new Cube(x, y, z, l); + System.out.println("Created Cube at " + cube.toString() + "\n" + + "Cube Volume: " + cube.volume() + "\n" + + "Cube Surface Area: " + cube.surfaceArea() + "\n" + + "Cube Face Diagonal: " + cube.faceDiagonal() + "\n" + + "Cube Space Diagonal: " + cube.spaceDiagonal() + "\n" + ); + + System.out.print("Enter a height: "); + int h = scan.nextInt(); + System.out.print("Enter a radius: "); + r = scan.nextInt(); + + Cone cone = new Cone(r, h); + System.out.println("Created Cone at " + cone.toString() + "\n" + + "Cone Volume: " + cone.volume() + "\n" + + "Cone Slant Height: " + cone.slantHeight() + "\n" + + "Cone Surface Area: " + cone.surfaceArea()); + + scan.close(); + } +} diff --git a/src/ca/bcit/comp1510/lab05/Mathematics.java b/src/ca/bcit/comp1510/lab05/Mathematics.java new file mode 100644 index 0000000..08cfcc8 --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/Mathematics.java @@ -0,0 +1,77 @@ +package ca.bcit.comp1510.lab05; + +/** + * Mathematics impliments math. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class Mathematics { + + /** PI is a math constant. */ + public static final double PI = 3.141592654; + + /** + * getCircleArea returns area of a circle. + * @param r radius + * @return double area + */ + public double getCircleArea(double r) { + return PI * r * r; + } + + /** + * sumOfInts adds up all ints counting up to int i. + * @param n max number + * @return int sum + */ + public int sumOfInts(int n) { + int sum = 0; + for (int i = 0; i <= n; i++) { + sum += i; + } + return sum; + } + + /** + * isPositive checks if given number is positive. + * @param n int to check if positive + * @return boolean is positive + */ + public boolean isPositive(int n) { + return n > 0; + } + + /** + * isEven check if a given number is even. + * @param n int to check if even + * @return boolean is even + */ + public boolean isEven(int n) { + return (n % 2) == 0; + } + + /** + * sumOfEvens sums even integers up to max i. + * @param n int max + * @return int sum + */ + public int sumOfEvens(int n) { + int sum = 0; + + if (this.isPositive(n)) { + for (int i = 0; i <= n; i++) { + if (this.isEven(i)) { + sum += i; + } + } + return sum; + } else { + for (int i = 0; i >= n; i--) { + if (this.isEven(i)) { + sum += i; + } + } + return sum; + } + } +} diff --git a/src/ca/bcit/comp1510/lab05/MathematicsTest.java b/src/ca/bcit/comp1510/lab05/MathematicsTest.java new file mode 100644 index 0000000..9f3b3e4 --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/MathematicsTest.java @@ -0,0 +1,189 @@ +package ca.bcit.comp1510.lab05; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + +/** + * MathematicsTest. + * + * @author BCIT + * @version 1.0 + */ +public class MathematicsTest { + + /** + * The Mathematics object to test. + */ + private Mathematics math; + + /** + * Creates a new Mathematics objects before each test is executed. + * + * @throws Exception + * if a Mathematics object cannot be created. + */ + @BeforeEach + public void setUp() throws Exception { + math = new Mathematics(); + } + + /** + * Test method for the value of PI. + */ + @Test + public void testPIValue() { + assertEquals(3.14159, Mathematics.PI, 0.00001); + } + + /** + * Tests getCircleArea method. + */ + @Test + public void testGetCircleArea1() { + double area = math.getCircleArea(1.0); + assertEquals(3.14159, area, 0.00001); + } + + /** + * Tests getCircleArea method. + */ + @Test + public void testGetCircleArea2() { + double area = math.getCircleArea(15.0); + assertEquals(706.858347058, area, 0.00001); + } + + /** + * Tests the method that sums the positive integers + * between 0 and the specified value. + */ + @Test + public void testSumOfInts() { + int sumOfInts = math.sumOfInts(0); + assertEquals(0, sumOfInts); + } + + /** + * Tests the method that sums the positive integers + * between 0 and the specified value. + */ + @Test + public void testSumOfInts2() { + int sumOfInts = math.sumOfInts(10); + assertEquals(55, sumOfInts); + } + + /** + * Tests the method that sums the positive integers + * between 0 and the specified value. + */ + @Test + public void testSumOfInts3() { + int sumOfInts = math.sumOfInts(49); + assertEquals(1225, sumOfInts); + } + + /** + * Tests the method that sums the positive integers + * between 0 and the specified value. + */ + @Test + public void testSumOfInts4() { + int sumOfInts = math.sumOfInts(-49); + assertEquals(0, sumOfInts); + } + + /** + * Tests the method that returns true if a specified + * number is positive, else false. + */ + @Test + public void testIsPositive() { + boolean isPositive = math.isPositive(2); + assertEquals(true, isPositive); + } + + /** + * Tests the method that returns true if a specified + * number is positive, else false. + */ + @Test + public void testIsPositive2() { + boolean isPositive = math.isPositive(-2); + assertEquals(false, isPositive); + } + + /** + * Tests the method that returns true if a specified + * number is positive, else false. + */ + @Test + public void testIsPositive3() { + boolean isPositive = math.isPositive(0); + assertEquals(false, isPositive); + } + + /** + * Tests the method that returns true if a specified + * number is even, else false. + */ + @Test + public void testIsEven() { + boolean isEven = math.isEven(0); + assertEquals(true, isEven); + } + + /** + * Tests the method that returns true if a specified + * number is even, else false. + */ + @Test + public void testIsEven2() { + boolean isEven = math.isEven(1); + assertEquals(false, isEven); + } + + /** + * Tests the method that returns true if a specified + * number is even, else false. + */ + @Test + public void testIsEven3() { + boolean isEven = math.isEven(2); + assertEquals(true, isEven); + } + + /** + * Tests the method that returns the sum of the even + * numbers between 0 and the specified value inclusive. + */ + @Test + public void testSumOfEvens() { + int sumOfEvens = math.sumOfEvens(0); + assertEquals(0, sumOfEvens); + } + + /** + * Tests the method that returns the sum of the even + * numbers between 0 and the specified value inclusive. + */ + @Test + public void testSumOfEvens2() { + int sumOfEvens = math.sumOfEvens(10); + assertEquals(30, sumOfEvens); + } + + /** + * Tests the method that returns the sum of the even + * numbers between 0 and the specified value inclusive. + */ + @Test + public void testSumOfEvens3() { + int sumOfEvens = math.sumOfEvens(-10); + assertEquals(-30, sumOfEvens); + } + +} diff --git a/src/ca/bcit/comp1510/lab05/Name.java b/src/ca/bcit/comp1510/lab05/Name.java new file mode 100644 index 0000000..abf8f55 --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/Name.java @@ -0,0 +1,67 @@ +package ca.bcit.comp1510.lab05; + +/** + * Name contains name information. + * + * @author Braeden Sowinski + * @version 1.0.0 + * + * @param first name string + * @param middle name string + * @param last name string + */ +public record Name(String first, String middle, String last) { + + /** + * toString compiles all names. + * @return full name as String. + */ + public String toString() { + return first + " " + middle + " " + last; + } + + /** + * length returns the sum of all name lengths. + * @return int length + */ + public int length() { + return first.length() + middle.length() + last.length(); + } + + /** + * initials returns initials of all names uppercased. + * @return string initials + */ + public String initials() { + return "" + first.toUpperCase().charAt(0) + + middle.toUpperCase().charAt(0) + + last.toUpperCase().charAt(0); + } + + /** + * charAt returns character from full string name. + * @param i index + * @return char from full name string. + */ + public char charAt(int i) { + return this.toString().charAt(i); + } + + /** + * toStringFormat compiles all names in a format. + * @return full name as String formatted. + */ + public String toStringFormat() { + return last + ", " + first + " " + middle; + } + + /** + * equalsFirst checks if input equals first name. + * @param s string to compare to first name + * @return boolean if it matches + */ + public boolean equalsFirst(String s) { + return first.toLowerCase().equals(s.toLowerCase()); + } +} + diff --git a/src/ca/bcit/comp1510/lab05/NameDriver.java b/src/ca/bcit/comp1510/lab05/NameDriver.java new file mode 100644 index 0000000..1179dbd --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/NameDriver.java @@ -0,0 +1,27 @@ +package ca.bcit.comp1510.lab05; + +/** + * NameDriver tests Name record. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class NameDriver { + + /** + * main program entry. + * @param args unused + */ + public static void main(String[] args) { + Name name = new Name("Braeden", "M", "Sowinski"); + + final int idx4 = 4; + System.out.println("Full name: " + name.toString()); + System.out.println("Length: " + name.length()); + System.out.println("Initials: " + name.initials()); + System.out.println("4th character: " + name.charAt(idx4)); + System.out.println("Name format: " + name.toStringFormat()); + System.out.println("'John' == first: " + name.equalsFirst("john")); + System.out.println("'Braeden' == first: " + + name.equalsFirst("Braeden")); + } +} diff --git a/src/ca/bcit/comp1510/lab05/Sphere.java b/src/ca/bcit/comp1510/lab05/Sphere.java new file mode 100644 index 0000000..0ee91fa --- /dev/null +++ b/src/ca/bcit/comp1510/lab05/Sphere.java @@ -0,0 +1,57 @@ +package ca.bcit.comp1510.lab05; + +import java.text.DecimalFormat; + +/** + * Sphere record. + * @author Braeden Sowinski + * @version 1.0.0 + * + * @param x coord. + * @param y coord. + * @param z coord. + * @param r radius. + */ +public record Sphere(int x, int y, int z, int r) { + + /** + * surfaceArea calculates the surface area + * of the given sphere. + * @return double + */ + public double surfaceArea() { + DecimalFormat format = new DecimalFormat("#.###"); + + final int magic = 4; + double a = magic * Math.PI * (r * r); + + return Double.parseDouble(format.format(a)); + } + + /** + * volume calculates the volume of the + * given sphere. + * @return double + */ + public double volume() { + DecimalFormat format = new DecimalFormat("#.###"); + + final double magic = 0.75; + final int power = 3; + double v = magic * Math.PI * Math.pow(r, power); + + return Double.parseDouble(format.format(v)); + } + + /** + * toString returns the sphere data as + * a string. + * @return string + */ + public String toString() { + return x + " " + + y + " " + + z + " " + + r; + } +}