I don't know what changes I've made lol
This commit is contained in:
10
PROJECT-1/.classpath
Normal file
10
PROJECT-1/.classpath
Normal 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
PROJECT-1/.project
Normal file
17
PROJECT-1/.project
Normal 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
PROJECT-1/src/module-info.java
Normal file
9
PROJECT-1/src/module-info.java
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* @author brett
|
||||
*
|
||||
*/
|
||||
module uwstout.courses.cs144 {
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user