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