diff --git a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/CheckIfLetter.java b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/CheckIfLetter.java new file mode 100644 index 0000000..24575c0 --- /dev/null +++ b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/CheckIfLetter.java @@ -0,0 +1,25 @@ +package uwstout.courses.cs144.examples; + +import java.util.Scanner; + +public class CheckIfLetter { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + String input = scan.nextLine(); + + char firstChar = input.charAt(0); + + if ((firstChar >= 'a' && firstChar <= 'z') || (firstChar >= 'A' && firstChar <= 'Z')) { + System.out.printf("%s is a letter!", firstChar); + } else if (firstChar >= '0' && firstChar <= '9') { + System.out.printf("%s is a number!", firstChar); + } else { + System.out.printf("%s is of unknown type!", firstChar); + } + + scan.close(); + } + +} diff --git a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Distance.java b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Distance.java index 65550a3..de58fd5 100644 --- a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Distance.java +++ b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Distance.java @@ -1,5 +1,6 @@ package uwstout.courses.cs144.examples; +import java.text.DecimalFormat; import java.util.Scanner; /** @@ -30,6 +31,7 @@ public class Distance { */ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); + DecimalFormat df = new DecimalFormat("#,##0.00"); // Read in route name System.out.print("Enter route name: "); @@ -43,10 +45,14 @@ public class Distance { // Calculate /// Starting at 0,0 and going to the two stops in sequence - int distanceTraveled = ORIGIN_STOP.distanceTo(stop1) + stop1.distanceTo(stop2); + int blockDistanceTraveled = ORIGIN_STOP.getBlockDistance(stop1) + stop1.getBlockDistance(stop2); + double directDistanceTraveled = ORIGIN_STOP.getDistance(stop1) + stop1.getDistance(stop2); // Output - System.out.printf("Total distance travelled (in city blocks) for route %s: %d\n", routeName, distanceTraveled); + System.out.println("Stop 1: " + stop1); + System.out.println("Stop 2: " + stop2); + System.out.printf("Total distance travelled (in city blocks) for route %s: %d\n", routeName, blockDistanceTraveled); + System.out.printf("The direct distance is: %s\n", df.format(directDistanceTraveled)); scnr.close(); } diff --git a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Formatter.java b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Formatter.java new file mode 100644 index 0000000..609e4cf --- /dev/null +++ b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Formatter.java @@ -0,0 +1,23 @@ +package uwstout.courses.cs144.examples; + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Scanner; + +public class Formatter { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + System.out.print("Enter a value: "); + double val = scan.nextDouble(); + + System.out.println(val); + System.out.println(NumberFormat.getCurrencyInstance().format(val)); + System.out.println(NumberFormat.getPercentInstance().format(val)); + System.out.println(new DecimalFormat("#,##0.000,0").format(val)); + + scan.close(); + } + +} diff --git a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/SphereVolume.java b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/SphereVolume.java new file mode 100644 index 0000000..2911f8d --- /dev/null +++ b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/SphereVolume.java @@ -0,0 +1,33 @@ +package uwstout.courses.cs144.examples; + +import java.util.Scanner; + +public class SphereVolume { + + /** + * Calculates the volume of a sphere given the radius. + * + * TESTS: + * + * TEST1IN: 1; TEST1OUT: 4.189 + * + * TEST2IN: -2, TEST2OUT: -33.510 + * + * TEST3IN: 0, TEST3OUT: 0.000 + * + * TEST4IN: 0.5, TEST4OUT: 0.524 + * + * @param args Command line arguments - not used + */ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.print("Enter the radius of the sphere: "); + + double radius = Math.abs(scan.nextDouble()); + double volume = (4.0 / 3.0) * Math.pow(radius, 3) * Math.PI; + System.out.printf("A sphere with radius %.2f has a volume of %.2f", radius, volume); + + scan.close(); + } + +} diff --git a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Stop.java b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Stop.java index e719f5f..94c343b 100644 --- a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Stop.java +++ b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Stop.java @@ -99,8 +99,12 @@ public class Stop { * @param otherStop The stop we are attempting to measure the distance to * @return The distance (in city blocks) between this object and otherPoint */ - public int distanceTo(Stop otherStop) { + public int getBlockDistance(Stop otherStop) { /// No Diagonals, city blocks return Math.abs(otherStop.getX() - this.x) + Math.abs(otherStop.getY() - this.y); } + + public double getDistance(Stop otherStop) { + return Math.sqrt(Math.pow(otherStop.getX() - this.x, 2) + Math.pow(otherStop.getY() - this.y, 2)); + } } \ No newline at end of file diff --git a/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/StringSplit.java b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/StringSplit.java new file mode 100644 index 0000000..1788f17 --- /dev/null +++ b/CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/StringSplit.java @@ -0,0 +1,22 @@ +package uwstout.courses.cs144.examples; + +import java.util.Scanner; + +public class StringSplit { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + String inputString = scan.nextLine().toLowerCase().strip(); + + int splitAt = inputString.length() / 2; + + String stringHalf1 = inputString.substring(0, splitAt); + String stringHalf2 = inputString.substring(splitAt, inputString.length()); + + System.out.println(stringHalf1); + System.out.println(stringHalf2); + + scan.close(); + } + +} diff --git a/LAB-4/.classpath b/LAB-4/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/LAB-4/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/LAB-4/.project b/LAB-4/.project new file mode 100644 index 0000000..ca5df5b --- /dev/null +++ b/LAB-4/.project @@ -0,0 +1,17 @@ + + + LAB-4 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/LAB-4/src/module-info.java b/LAB-4/src/module-info.java new file mode 100644 index 0000000..05e31f2 --- /dev/null +++ b/LAB-4/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * @author brett + * + */ +module uwstout.courses.cs144 { +} \ No newline at end of file diff --git a/LAB-4/src/uwstout/courses/cs144/labs/lab4/ExpFormat.java b/LAB-4/src/uwstout/courses/cs144/labs/lab4/ExpFormat.java new file mode 100644 index 0000000..3c28156 --- /dev/null +++ b/LAB-4/src/uwstout/courses/cs144/labs/lab4/ExpFormat.java @@ -0,0 +1,70 @@ +package uwstout.courses.cs144.labs.lab4; + +import java.text.DecimalFormat; + +/** + * An expression formatter + * + * @author Brett Bender + * @version 2022.10.10 + * + */ +public class ExpFormat { + + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.##"); + + private int x; + private int y; + private double result; + + /** + * An expression formatter + * + * @param ix The X value + * @param iy The Y value + * @param iresult The result + */ + public ExpFormat(int ix, int iy, double iresult) { + x = ix; + y = iy; + result = iresult; + } + + /** + * Get the X value of the expression + * + * @return The X value + */ + public int getX() { + return x; + } + + /** + * Get the Y value of the expression + * + * @return The Y value + */ + public int getY() { + return y; + } + + /** + * Get the result of the expression + * + * @return The result + */ + public double getResult() { + return result; + } + + @Override + public String toString() { + String reString = ""; + reString += x + " * " + y + " + 2\n"; + reString += "----------\n"; + reString += x + "^2 + 1\n"; + reString += "= " + DECIMAL_FORMAT.format(result); + + return reString; + } +} diff --git a/LAB-4/src/uwstout/courses/cs144/labs/lab4/Expression.java b/LAB-4/src/uwstout/courses/cs144/labs/lab4/Expression.java new file mode 100644 index 0000000..6f0b9fa --- /dev/null +++ b/LAB-4/src/uwstout/courses/cs144/labs/lab4/Expression.java @@ -0,0 +1,51 @@ +package uwstout.courses.cs144.labs.lab4; + +import java.util.Scanner; + +/** + * Calculates (xy + 2)/(x^2 + 1) with the values entered by the end user. + * + * @author Brett Bender + * @version 2022.10.10 + */ +public class Expression { + + /** + * Calculates (xy + 2)/(x^2 + 1) with the values entered by the end user. It + * then prints out a formatted version of the expression and result + * + * TESTS + * + * IN1: [1, 1] OUT1: [1.5] + * + * IN2: [1, 0] OUT2: [1] + * + * IN3: [0, 1] OUT3: [2] + * + * IN4: [-1, -1] OUT4: [1.5] + * + * @param args Command line arguments, not used + */ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // Read Inputs + /// Read X + System.out.print("Enter the x value: "); + int x = scan.nextInt(); + + /// Read Y + System.out.print("Enter the y value: "); + int y = scan.nextInt(); + + // Calculate: (xy + 2)/(x^2 + 1) + double result = ((x * y) + 2) / (Math.pow(x, 2) + 1); + + // Format & Print + ExpFormat ef = new ExpFormat(x, y, result); + System.out.println("\n" + ef); + + scan.close(); + } + +} diff --git a/LAB-4/src/uwstout/courses/cs144/labs/lab4/FabricationCost.java b/LAB-4/src/uwstout/courses/cs144/labs/lab4/FabricationCost.java new file mode 100644 index 0000000..12f2d12 --- /dev/null +++ b/LAB-4/src/uwstout/courses/cs144/labs/lab4/FabricationCost.java @@ -0,0 +1,68 @@ +package uwstout.courses.cs144.labs.lab4; + +import java.text.NumberFormat; +import java.util.Scanner; + +/** + * Calculates the fabrication cost for a number of units, the cost per unit, and + * a mark-up. This automatically includes a constant setup cost + * + * @author Brett Bender + * @version 2022.10.10 + * + */ +public class FabricationCost { + + private static final double SETUP_COST = 3575.95; + + /** + * Calculates the fabrication cost for a number of units, the cost per unit, and + * a mark-up. This automatically includes a constant setup cost + * + * + * TESTS + * + * IN1: [5, 5.00, 0.5] OUT1: [5, $5.00, 50%, $3,613.45] + * + * IN2: [-1, -1.0, 0.0] OUT2: [1, $1.00, 0%, $3,756.95] + * + * IN3: [0, 0.0, 0.0] OUT3: [0, 0.00, 0%, $3,575.95] + * + * IN4: [10, 2.75, -0.15]: [10, $2.75, -15%, $3,599.32] + * + * @param args Command line arguments (not used) + */ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // Read Units + System.out.print("Enter the number of units: "); + int units = Math.abs(scan.nextInt()); + + // Read Cost Per Unit + System.out.print("Enter the cost per unit: $"); + double costPerUnit = Math.abs(scan.nextDouble()); + + // Red Percent Mark-up + System.out.print("Enter the percent markup: "); + double percentMarkup = scan.nextDouble(); + + // Calculate + /// Initial calculation of units x cost per unit + double totalCost = units * costPerUnit; + /// Add the mark-up + totalCost += totalCost * percentMarkup; + /// Add the setup cost + totalCost += SETUP_COST; + + // Output Units, Cost per unit, percent mark-up, and computed value + System.out.println("Units: " + units); + System.out.println("Cost Per: " + NumberFormat.getCurrencyInstance().format(costPerUnit)); + System.out.println("Markup: " + NumberFormat.getPercentInstance().format(percentMarkup)); + System.out.println("Total Cost: " + NumberFormat.getCurrencyInstance().format(totalCost)); + + scan.close(); + + } + +} diff --git a/LAB-5/.classpath b/LAB-5/.classpath new file mode 100644 index 0000000..80d258b --- /dev/null +++ b/LAB-5/.classpath @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/LAB-5/.project b/LAB-5/.project new file mode 100644 index 0000000..ea4c466 --- /dev/null +++ b/LAB-5/.project @@ -0,0 +1,17 @@ + + + LAB-5 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/LAB-5/src/module-info.java b/LAB-5/src/module-info.java new file mode 100644 index 0000000..f92a3fd --- /dev/null +++ b/LAB-5/src/module-info.java @@ -0,0 +1,10 @@ +/** + * + */ +/** + * @author brett + * + */ +module uwstout.courses.cs144 { + requires junit; +} \ No newline at end of file diff --git a/LAB-5/src/uwstout/courses/cs144/labs/lab5/Projectile.java b/LAB-5/src/uwstout/courses/cs144/labs/lab5/Projectile.java new file mode 100644 index 0000000..e1d95aa --- /dev/null +++ b/LAB-5/src/uwstout/courses/cs144/labs/lab5/Projectile.java @@ -0,0 +1,109 @@ +package uwstout.courses.cs144.labs.lab5; + +/** + * Represents a projectile being launched into the air at a specific velocity, + * and at a specific angle. Allows the user to calculate the distance traveled + * by the projectile at a particular time, as well as the maximum distance. + * + * @author Brett Bender + * @version 2022.10.26 + */ +public class Projectile { + + /** + * The acceleration due to gravity + */ + private static final double G = 9.8; + private static final int DEFAULT_ANGLE = 45; + + private double velocity; + private int angle; + + /** + * Constructs a new Projectile with a given velocity and angle. + * + * @param nvelocity The velocity of the projectile + * @param nangle The angle of the projectile + */ + public Projectile(double nvelocity, int nangle) { + velocity = nvelocity; + angle = nangle; + } + + /** + * Constructs a new Projectile with a given velocity. Defaults angle to + * 45deg. + * + * @param nvelocity The velocity of the projectile + */ + public Projectile(double nvelocity) { + velocity = nvelocity; + angle = DEFAULT_ANGLE; + } + + /** + * Constructs a new Projectile that is a copy of the specified Projectile. + * + * @param other The projectile that is being copied + */ + public Projectile(Projectile other) { + velocity = other.velocity; + angle = other.angle; + } + + /** + * Calculates the distance traveled at a specified time. + * + * @param time The time (in seconds) + * @return The distance traveled + */ + public double getDistance(double time) { + return velocity * time * Math.cos(Math.toRadians(angle)); + } + + /** + * Calculates the maximum distance the projectile may travel. + * + * @return The maximum distance + */ + public double getMaximumDistance() { + return (velocity * velocity / G) * Math.sin(2 * Math.toRadians(angle)); + } + + /** + * Get the velocity of the projectile. + * + * @return The velocity of the projectile + */ + public double getVelocity() { + return velocity; + } + + /** + * Get the angle of the projectile. + * + * @return The angle of the projectile + */ + public int getAngle() { + return angle; + } + + /** + * Set the velocity of the projectile + * + * @param nvelocity The new velocity + */ + public void setVelocity(double nvelocity) { + velocity = nvelocity; + } + + /** + * Set the angle of the projectile + * + * @param nangle The new angle + */ + public void setAngle(int nangle) { + angle = nangle; + } + +} diff --git a/LAB-5/src/uwstout/courses/cs144/labs/lab5/ProjectileTest.java b/LAB-5/src/uwstout/courses/cs144/labs/lab5/ProjectileTest.java new file mode 100644 index 0000000..0ff9ab7 --- /dev/null +++ b/LAB-5/src/uwstout/courses/cs144/labs/lab5/ProjectileTest.java @@ -0,0 +1,106 @@ +package uwstout.courses.cs144.labs.lab5; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the Projectile class + * + * @author Brett Bender + * @version 2022.10.26 + */ +public class ProjectileTest { + + private static final double DELTA = 0.01; + + private Projectile p1; + private Projectile p2; + private Projectile p3; + + /** + * Sets the class up for testing + * + * @throws Exception + */ + @Before + public void setUp() throws Exception { + p1 = new Projectile(50.0, 31); + p2 = new Projectile(35.0); + p3 = new Projectile(p1); + } + + /** + * Tears down the class from testing. + * + * @throws Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Tests the Projectile(double, int) constructor + */ + @Test + public void testProjectileDoubleInt() { + Projectile t1 = new Projectile(4.0, 20); + assertEquals(4.0, t1.getVelocity(), DELTA); + assertEquals(20, t1.getAngle()); + + Projectile t2 = new Projectile(0.0, 0); + assertEquals(0.0, t2.getVelocity(), DELTA); + assertEquals(0, t2.getAngle()); + } + + /** + * Tests the Projectile(double) constructor + */ + @Test + public void testProjectileDouble() { + Projectile t1 = new Projectile(1.0); + assertEquals(1.0, t1.getVelocity(), DELTA); + assertEquals(45, t1.getAngle()); + + Projectile t2 = new Projectile(0.0); + assertEquals(0.0, t2.getVelocity(), DELTA); + assertEquals(45, t2.getAngle()); + } + + /** + * Tests the Projectile(Projectile) constructor + */ + @Test + public void testProjectileProjectile() { + Projectile t1 = new Projectile(p1); + assertEquals(50.0, t1.getVelocity(), DELTA); + assertEquals(31, t1.getAngle()); + + Projectile t2 = new Projectile(p2); + assertEquals(35.0, t2.getVelocity(), DELTA); + assertEquals(45, t2.getAngle()); + } + + /** + * Tests the {@link Projectile#getDistance(double)} method + */ + @Test + public void testGetDistance() { + assertEquals(214.29, p1.getDistance(5), DELTA); + assertEquals(0, p2.getDistance(0), DELTA); + assertEquals(214.29, p3.getDistance(5), DELTA); + } + + /** + * Tests the {@link Projectile#getMaximumDistance()} method + */ + @Test + public void testGetMaximumDistance() { + assertEquals(225.24, p1.getMaximumDistance(), DELTA); + assertEquals(125, p2.getMaximumDistance(), DELTA); + assertEquals(225.24, p3.getMaximumDistance(), DELTA); + } + +} diff --git a/OCT-6-Debugging/.classpath b/OCT-6-Debugging/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/OCT-6-Debugging/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/OCT-6-Debugging/.project b/OCT-6-Debugging/.project new file mode 100644 index 0000000..c423d6f --- /dev/null +++ b/OCT-6-Debugging/.project @@ -0,0 +1,17 @@ + + + OCT-6-Debugging + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/OCT-6-Debugging/src/module-info.java b/OCT-6-Debugging/src/module-info.java new file mode 100644 index 0000000..68013c7 --- /dev/null +++ b/OCT-6-Debugging/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * @author brett + * + */ +module oct2_debug { +} \ No newline at end of file diff --git a/OCT-6-Debugging/src/uwstout/courses/cs144/examples/debug/Slope.java b/OCT-6-Debugging/src/uwstout/courses/cs144/examples/debug/Slope.java new file mode 100644 index 0000000..e92ee53 --- /dev/null +++ b/OCT-6-Debugging/src/uwstout/courses/cs144/examples/debug/Slope.java @@ -0,0 +1,72 @@ +package uwstout.courses.cs144.examples.debug; + +/** + * Calculates the slope of a line through two points. + * + * @author Turner + * @version 1.0 + */ +public class Slope { + + private double slope; + + /** + * Calculates the slope using two points. + * + * @param x1 First x coordinate + * @param y1 First y coordinate + * @param x2 Second x coordinate + * @param y2 Second y coordinate + */ + public void setSlope(int x1, int y1, int x2, int y2) { + int rise; + int run; + + rise = y2 - y1; + run = x2 - x1; + + slope = (double) rise / run; + } + + /** + * Gets the slope + * + * @return The slope + */ + public double getSlope() { + return slope; + } + + /** + * Tests the Slope class + * + * @param args Command line - not used + */ + public static void main(String[] args) { + Slope slope = new Slope(); + + // tests + // Input: 0, 0, 1, 1 -> slope: 1 + slope.setSlope(0, 0, 1, 1); + System.out.println("Slope: " + slope.getSlope()); + + // Input: 0, 0, 1, -1 -> slope: -1 + slope.setSlope(0, 0, 1, -1); + System.out.println("Slope: " + slope.getSlope()); + + // Input: 0, 0, 1, 0 -> slope: 0 + slope.setSlope(0, 0, 1, 0); + System.out.println("Slope: " + slope.getSlope()); + + // Input: 0, 0, 0, 1 -> slope: UND + slope.setSlope(0, 0, 0, 1); + System.out.println("Slope: " + slope.getSlope()); + + // Input: 0, 0, 1, 2 -> slope: + slope.setSlope(0, 0, 3, 2); + System.out.println("Slope: " + slope.getSlope()); + + // More? + } + +} diff --git a/PROJECT-1/.classpath b/PROJECT-1/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/PROJECT-1/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/PROJECT-1/.project b/PROJECT-1/.project new file mode 100644 index 0000000..afbd1a0 --- /dev/null +++ b/PROJECT-1/.project @@ -0,0 +1,17 @@ + + + PROJECT-1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/PROJECT-1/src/module-info.java b/PROJECT-1/src/module-info.java new file mode 100644 index 0000000..05e31f2 --- /dev/null +++ b/PROJECT-1/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * @author brett + * + */ +module uwstout.courses.cs144 { +} \ No newline at end of file diff --git a/PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/FreeFall.java b/PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/FreeFall.java new file mode 100644 index 0000000..0a2ef69 --- /dev/null +++ b/PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/FreeFall.java @@ -0,0 +1,151 @@ +package uwstout.courses.cs144.projects.project1.freefall; + +import java.util.Scanner; + +/* ALGORITHM: + * + * # Get input from the user + * - Print "Enter object name: " + * - Read name + * - Print "Enter object mass: " + * - Read mass # in kg + * - Print "Enter initial velocity: " + * - Read V_0 # in m/s + * - Print "Enter time: " + * - Read t # in s + * + * # Constants + * G <- 9.8 # Gravity in m/s^2 + * + * # Start the calculations + * V_f <- G * t + V_0 # in m/s + * distance <- [(V_0 + V_f) / 2] * t # in m + * momentum <- [mass * V_f] # in kg m/s + * force <- G * mass # in N + * kinetic <- [(mass * V_f ^ 2) / 2] # in J + * + * Print name + * Print "Mass: " + mass + "kg" + * Print "Initial Velocity: " + V_0 + "m/s" + * Print "Time in Free Fall: " + t + "s" + * Print "Final Velocity: " + V_f + "m/s" + * Print "Distance Traveled: " + distance + "m" + * Print "Momentum: " + momentum + "kg m/s" + * Print "Force: " + force + "N" + * Print "Kinetic Energy " + kinetic + "J" + */ + +/* TESTS: + * + * Input1: + * Name: Baseball + * Mass: 0.145 + * V_0: 40.2 + * Time: 10 + * + * Output1: + * Baseball + * Mass: 0.15kg + * Initial Velocity: 40.20m/s + * Time in Free Fall: 10.00s + * Final Velocity: 138.20m/s + * Distance Traveled: 892.00m + * Momentum: 20.04kg m/s + * Force: 1.42N + * Kinetic Energy: 1384.69J + * + * Test2: + * Name: Gus + * Mass: 68.1 + * V_0: 0 + * Time: 30 + * + * Output2: + * Gus + * Mass: 68.10kg + * Initial Velocity: 0.00m/s + * Time in Free Fall: 30.00s + * Final Velocity: 294.00m/s + * Distance Traveled: 4410.00m + * Momentum: 20021.40kg m/s + * Force: 667.38N + * Kinetic Energy: 2943145.80J + * + */ + +/** + * Calculates several values relating to a free-falling object in a vacuum. + * Specifically, it will calculate the final velocity, distance traveled, the + * momentum, the force acting on the object, and the kinetic energy of the + * object after a given time passes. + * + * @author Brett Bender + * @version 2022.10.19 + */ +public class FreeFall { + + private static final double G = 9.8; // m/s^2 + + /** + * Calculates several values relating to a free-falling object in a vacuum. + * Specifically, it will calculate the final velocity, distance traveled, + * the momentum, the force acting on the object, and the kinetic energy of + * the object after a given time passes. + * + * @param args Command line arguments (not used) + */ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.print("Enter the name of the object: "); + String objName = sc.nextLine(); + + System.out.print("Enter the mass (in kg) of " + objName + ": "); + Value objMass = new Value(sc.nextDouble(), "Mass", "kg"); + + System.out.print("Enter the initial velocity (in m/s): "); + Value initialVelocity = new Value(sc.nextDouble(), "Initial_Velocity", + "m/s"); + + System.out.print("Enter the time (in seconds): "); + Value timeValue = new Value(sc.nextDouble(), "Time_in_Free_Fall", "s"); + + sc.close(); + + // final velocity (V_f = Gt + V_0) m/s + Value finalVelocity = new Value( + G * timeValue.getValue() + initialVelocity.getValue(), + "Final_Velocity", "m/s"); + + // distance traveled (d = {[V_0 + V_f]/2}t ) m + Value distanceTraveled = new Value( + ((initialVelocity.getValue() + finalVelocity.getValue()) / 2.0) + * timeValue.getValue(), + "Distance_Traveled", "m"); + + // momentum (p = mV_f) kg m/s + Value momentum = new Value( + objMass.getValue() * finalVelocity.getValue(), "Momentum", + "kg m/s"); + + // force (F = Gm) N + Value force = new Value(G * objMass.getValue(), "Force", "N"); + + // kinetic energy (E_k = {[mV_f^2]/2}) J + Value kineticEnergy = new Value( + (objMass.getValue() * Math.pow(finalVelocity.getValue(), 2)) + / 2, + "Kinetic_Energy", "J"); + + System.out.println(objName); + System.out.println(objMass); + System.out.println(initialVelocity); + System.out.println(timeValue); + System.out.println(finalVelocity); + System.out.println(distanceTraveled); + System.out.println(momentum); + System.out.println(force); + System.out.println(kineticEnergy); + } + +} diff --git a/PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/Value.java b/PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/Value.java new file mode 100644 index 0000000..f44bd27 --- /dev/null +++ b/PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/Value.java @@ -0,0 +1,120 @@ +package uwstout.courses.cs144.projects.project1.freefall; + +import java.text.DecimalFormat; + +/** + * A value that can be easily formatted with {@link #toString()}. + * + * @author Brett Bender + * @version 2022.10.19 + */ +public class Value { + + // Since all decimals will always be the same format, can just share the + // instance between Value instances. + private static final DecimalFormat DF = new DecimalFormat("0.00"); + + private static final int DEFAULT_LABEL_WIDTH = 20; + private static final int DEFAULT_VALUE_WIDTH = 16; + + private static final int MIN_LABEL_WIDTH = 12; + private static final int MIN_VALUE_WIDTH = 8; + + private double value; + private String label; + private String units; + private int labelWidth; + private int valueWidth; + + /** + * Constructs a new value that can be formatted with {@link #toString()}. + * + * @param nValue The value of this Value + * @param nLabel The label of this Value + * @param nUnits The units of this Value + */ + public Value(double nValue, String nLabel, String nUnits) { + value = nValue; + label = nLabel.replace('_', ' '); + units = nUnits; + + labelWidth = DEFAULT_LABEL_WIDTH; + valueWidth = DEFAULT_VALUE_WIDTH; + } + + /** + * Get the value stored by this Value + * + * @return The value + */ + public double getValue() { + return value; + } + + /** + * Get the label of this Value + * + * @return The label + */ + public String getLabel() { + return label; + } + + /** + * Get the units of this value + * + * @return The units + */ + public String getUnits() { + return units; + } + + /** + * Get the width of this Value's label + * + * @return The label width + */ + public int getLabelWidth() { + return labelWidth; + } + + /** + * Get the width of this Value's value + * + * @return The value width + */ + public int getValueWidth() { + return valueWidth; + } + + /** + * Set the width of this Value's label. Will be automatically enforced to be + * above the {@link #MIN_LABEL_WIDTH}. + * + * @param nLabelWidth The new label width + */ + public void setLabelWidth(int nLabelWidth) { + labelWidth = Math.max(MIN_LABEL_WIDTH, nLabelWidth); + } + + /** + * Set the width of this Value's value. Will be automatically enforced to be + * above the {@link #MIN_VALUE_WIDTH} + * + * @param nValueWidth The new value width + */ + public void setValueWidth(int nValueWidth) { + valueWidth = Math.max(MIN_VALUE_WIDTH, nValueWidth); + } + + @Override + public String toString() { + String retString = label + " ".repeat(labelWidth - label.length()); + String valString = DF.format(value); + retString += " ".repeat(valueWidth - valString.length()) + valString; + retString += units; + + return retString; + } + +}