Browse Source

I don't know what changes I've made lol

master
Brett Bender 3 years ago
parent
commit
9a1bc9cfb5
26 changed files with 1013 additions and 3 deletions
  1. +25
    -0
      CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/CheckIfLetter.java
  2. +8
    -2
      CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Distance.java
  3. +23
    -0
      CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Formatter.java
  4. +33
    -0
      CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/SphereVolume.java
  5. +5
    -1
      CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Stop.java
  6. +22
    -0
      CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/StringSplit.java
  7. +10
    -0
      LAB-4/.classpath
  8. +17
    -0
      LAB-4/.project
  9. +9
    -0
      LAB-4/src/module-info.java
  10. +70
    -0
      LAB-4/src/uwstout/courses/cs144/labs/lab4/ExpFormat.java
  11. +51
    -0
      LAB-4/src/uwstout/courses/cs144/labs/lab4/Expression.java
  12. +68
    -0
      LAB-4/src/uwstout/courses/cs144/labs/lab4/FabricationCost.java
  13. +15
    -0
      LAB-5/.classpath
  14. +17
    -0
      LAB-5/.project
  15. +10
    -0
      LAB-5/src/module-info.java
  16. +109
    -0
      LAB-5/src/uwstout/courses/cs144/labs/lab5/Projectile.java
  17. +106
    -0
      LAB-5/src/uwstout/courses/cs144/labs/lab5/ProjectileTest.java
  18. +10
    -0
      OCT-6-Debugging/.classpath
  19. +17
    -0
      OCT-6-Debugging/.project
  20. +9
    -0
      OCT-6-Debugging/src/module-info.java
  21. +72
    -0
      OCT-6-Debugging/src/uwstout/courses/cs144/examples/debug/Slope.java
  22. +10
    -0
      PROJECT-1/.classpath
  23. +17
    -0
      PROJECT-1/.project
  24. +9
    -0
      PROJECT-1/src/module-info.java
  25. +151
    -0
      PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/FreeFall.java
  26. +120
    -0
      PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/Value.java

+ 25
- 0
CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/CheckIfLetter.java View File

@ -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();
}
}

+ 8
- 2
CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Distance.java View File

@ -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();
}


+ 23
- 0
CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Formatter.java View File

@ -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();
}
}

+ 33
- 0
CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/SphereVolume.java View File

@ -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();
}
}

+ 5
- 1
CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/Stop.java View File

@ -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));
}
}

+ 22
- 0
CS144-007-EXAMPLE/src/uwstout/courses/cs144/examples/StringSplit.java View File

@ -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();
}
}

+ 10
- 0
LAB-4/.classpath View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
LAB-4/.project View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LAB-4</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 9
- 0
LAB-4/src/module-info.java View File

@ -0,0 +1,9 @@
/**
*
*/
/**
* @author brett
*
*/
module uwstout.courses.cs144 {
}

+ 70
- 0
LAB-4/src/uwstout/courses/cs144/labs/lab4/ExpFormat.java View File

@ -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;
}
}

+ 51
- 0
LAB-4/src/uwstout/courses/cs144/labs/lab4/Expression.java View File

@ -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();
}
}

+ 68
- 0
LAB-4/src/uwstout/courses/cs144/labs/lab4/FabricationCost.java View File

@ -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();
}
}

+ 15
- 0
LAB-5/.classpath View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
LAB-5/.project View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LAB-5</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 10
- 0
LAB-5/src/module-info.java View File

@ -0,0 +1,10 @@
/**
*
*/
/**
* @author brett
*
*/
module uwstout.courses.cs144 {
requires junit;
}

+ 109
- 0
LAB-5/src/uwstout/courses/cs144/labs/lab5/Projectile.java View File

@ -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;
}
}

+ 106
- 0
LAB-5/src/uwstout/courses/cs144/labs/lab5/ProjectileTest.java View File

@ -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);
}
}

+ 10
- 0
OCT-6-Debugging/.classpath View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
OCT-6-Debugging/.project View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>OCT-6-Debugging</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 9
- 0
OCT-6-Debugging/src/module-info.java View File

@ -0,0 +1,9 @@
/**
*
*/
/**
* @author brett
*
*/
module oct2_debug {
}

+ 72
- 0
OCT-6-Debugging/src/uwstout/courses/cs144/examples/debug/Slope.java View File

@ -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?
}
}

+ 10
- 0
PROJECT-1/.classpath View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
PROJECT-1/.project View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PROJECT-1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 9
- 0
PROJECT-1/src/module-info.java View File

@ -0,0 +1,9 @@
/**
*
*/
/**
* @author brett
*
*/
module uwstout.courses.cs144 {
}

+ 151
- 0
PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/FreeFall.java View File

@ -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);
}
}

+ 120
- 0
PROJECT-1/src/uwstout/courses/cs144/projects/project1/freefall/Value.java View File

@ -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;
}
}

Loading…
Cancel
Save