Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/**************************************************************** * These are the jUnit tests to test the class GpsPosition. * Feel free to write your own jUnit tests in a separate class.

image text in transcribedimage text in transcribedimage text in transcribed

/****************************************************************

* These are the jUnit tests to test the class GpsPosition.

* Feel free to write your own jUnit tests in a separate class.

* However, there tests here should NOT be modified.

* They need to be used unchanged for the submission.

****************************************************************/

package gps;

import static org.junit.Assert.*;

import org.junit.Test;

public class GpsPositionTest {

private final double delta = 0.000001; // specifies the required

precision

private final GpsPosition slc = new GpsPosition(40.760671, -

111.891122, 1299.8 );

@Test

public void testGpsPosition() {

GpsPosition moab = new GpsPosition(38.573645, -

109.546389, 1227.1);

assertEquals("38.573645, -109.546389 (1227.1)",

moab.toString());

}

@Test(expected = IllegalArgumentException.class)

public void testGpsPositionInvalidLatitude() {

new GpsPosition(91, -104.9847132, 1606.296);

}

@Test(expected = IllegalArgumentException.class)

public void testGpsPositionInvalidLongitude() {

new GpsPosition(39.7392541, -180.01, 1606.296);

}

@Test

public void testGetLatitude() {

assertEquals(40.760671, slc.getLatitude(), delta);

}

@Test

public void testGetLongitude() {

assertEquals(-111.891122, slc.getLongitude(), delta);

}

@Test

public void testGetElevation() {

assertEquals(1299.8, slc.getElevation(), delta);

}

@Test

public void testToString() {

assertEquals("40.760671, -111.891122 (1299.8)",

slc.toString());

}

@Test

public void testToStringRounding() {

GpsPosition denver = new GpsPosition(39.7392541, -

104.9847129, 1606.296);

assertEquals("39.739254, -104.984713 (1606.3)",

denver.toString());

}

--------------------------------------------------------------------------------------------------------------------------------------------------------

/****************************************************************

* These are the jUnit tests to test the class Gps.

* Feel free to write your own jUnit tests in a separate class.

* However, there tests here should NOT be modified.

* They need to be used unchanged for the submission.

****************************************************************/

package gps;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Before;

import org.junit.Test;

public class GpsTest {

private final double deltaDistance = 0.1; // deviations up to 100m

are acceptable

private final GpsPosition slc = new GpsPosition(40.760671, -

111.891122, 1299.8 );

private final GpsPosition moab = new GpsPosition(38.573645, -

109.546389, 1227.1);

private final GpsPosition denver = new GpsPosition(39.7392541, -

104.9847129, 1606.296);

private final GpsPosition sf = new GpsPosition(37.808715, -

122.409821, 5);

private Gps gps;

@Before

public void setUp() throws Exception {

gps = new Gps(slc);

}

@Test

public void testGps() {

Gps gpsSF = new Gps(sf);

assertEquals(sf, gpsSF.position());

assertEquals(0d, gpsSF.distanceTraveled(),

deltaDistance);

}

@Test

public void testGetRoute() {

ArrayList expectedRoute = new ArrayList();

expectedRoute.add(slc);

assertTrue(expectedRoute.equals(gps.getRoute()));

}

@Test

public void testUpdate() {

gps.update(moab);

assertEquals(moab, gps.position());

}

@Test

public void testUpdateMultipleTimes() {

gps.update(moab);

gps.update(denver);

gps.update(sf);

assertEquals(sf, gps.position());

}

@Test

public void testRandomUpdate() {

// update Salt Lake City 100 times and ensure that each

time

// the updated position is within the required range

for (int i = 0; i

gps = new Gps(slc);

gps.randomUpdate();

GpsPosition newPosition = gps.position();

assertTrue(newPosition.getLatitude()

41.260671 &&

newPosition.getLatitude() >=

40.2606711 &&

newPosition.getLongitude()

newPosition.getLongitude() >= -112.391122 &&

newPosition.getElevation()

== 1299.8 );

}

}

@Test

public void testPosition() {

assertEquals(slc, gps.position());

}

@Test

public void testPositionAfterUpdate() {

gps.update(denver);

gps.update(sf);

assertEquals(sf, gps.position());

}

@Test

public void testDistanceTraveledNoTravel() {

double expected = 0d; // no distance has been traveled

yet

assertEquals(moab, gps.position());

}

@Test

public void testUpdateMultipleTimes() {

gps.update(moab);

gps.update(denver);

gps.update(sf);

assertEquals(sf, gps.position());

}

@Test

public void testRandomUpdate() {

// update Salt Lake City 100 times and ensure that each

time

// the updated position is within the required range

for (int i = 0; i

gps = new Gps(slc);

gps.randomUpdate();

GpsPosition newPosition = gps.position();

assertTrue(newPosition.getLatitude()

41.260671 &&

newPosition.getLatitude() >=

40.2606711 &&

newPosition.getLongitude()

newPosition.getLongitude() >= -112.391122 &&

newPosition.getElevation()

== 1299.8 );

}

}

@Test

public void testPosition() {

assertEquals(slc, gps.position());

}

@Test

public void testPositionAfterUpdate() {

gps.update(denver);

gps.update(sf);

assertEquals(sf, gps.position());

}

@Test

public void testDistanceTraveledNoTravel() {

double expected = 0d; // no distance has been traveled

yet

assertEquals(expected, gps.distanceTraveled(),

deltaDistance);

}

@Test

public void testDistanceTraveledSlcToMoab() {

gps.update(moab);

double expected = 315.279;

assertEquals(expected, gps.distanceTraveled(),

deltaDistance);

}

@Test

public void testDistanceTraveledMoabToDenver() {

Gps gpsMoab = new Gps(moab);

gpsMoab.update(denver);

double expected = 414.0689;

assertEquals(expected, gpsMoab.distanceTraveled(),

deltaDistance);

}

@Test

public void testDistanceTraveledSlcToDenver() {

gps.update(moab);

gps.update(denver);

double expected = 729.348;

assertEquals(expected, gps.distanceTraveled(),

deltaDistance);

}

@Test

public void testReset() {

gps.reset();

ArrayList expectedRoute = new ArrayList();

expectedRoute.add(slc);

assertTrue(expectedRoute.equals(gps.getRoute()));

}

@Test

public void testResetAfterUpdate() {

gps.update(sf);

gps.update(denver);

gps.reset();

ArrayList expectedRoute = new ArrayList();

expectedRoute.add(denver);

assertTrue(expectedRoute.equals(gps.getRoute()));

}

}

Assignment GPS including jUnit Tests CSIS-1410 Learning Objectives: Practice Composition Review declaring classes based on UML class diagrams Use jUnit tests to test and troubleshoot your code Description: Create a new Java project called "1410 Gps". It includes a package named gps. e Inside the package gps implement the classes Gps and GpsPosition as described below Include doc comments for each of the public methods, for the constructors, and for the classes themselves Use the provided jUnit tests to test and troubleshoot both classes Feel free to add a main method at the end of the classes Gps and GpsPosition to demonstrate the use of the class members. For grading purposes the main methods will be ignored Class GpsPosition Class GpsPosition represents a position based on a given longitude, latitude, and elevation (in meters) Declare class Gps Position based on the UML class diagram below. Notice that there are no setters. The class is immutable. This means the state of a GpsPosition object can no longer be changed once it has been created Do not add or remove any public methods or constructors (except for main) There should also be NO print statements in class GpsPosition atitude: double longitude: double +GpsPosition (latitude: double, longitude: double, elevation: double) +getLatitude (): double + getLongitude (): double +getElevaton 0: double + toString0 String The getters expose the field values as usual Constructor: The constructor should implement input validation for If the constructor receives an invalid argument, an IllegalArgumentException should be thrown. Like this: throw new IilegalArgumentException"Invalid 1atitude and/or longitude" Method toString The method toString should return a String that includes the latitude followed by a comma, a space, the longitude, a space, and then the elevation in parenthesis like this except with the numeric values Latitude, Longitude (elevation) Both latitude and longitude should display 6 digits after the decimal point while the elevation should display only one digit after the decimal point. Example: 38.573645, 109.546389 (1227.1) Use the jUnit tests to test and troubleshoot GpsPosition before moving on to the next class Class Gps: Declare class Gps based on the class diagram below. No public methods or constructors should be added (except for main). There should be NO print statements in this class + Gps (position: GpsPosition) +getRoute ArrayList + update (position GpsPosition) + randomUpdate () + position () GpsPosition + distanceTraveled () double distance (from GpsPosition, to: GpsPosition): double + reset The getter exposes the field value as usual Constructor: Use the parameter postition to initialize the field route. Notice that the argument and the field have different types. When a new Gps is created, the ArrayList should include exactly one element: the start position. You can think of it as the position where the gps is located at that given time Method update The method update simulates that the GPS position has changed. The user provides the new position. When the method update is called, the new position needs to be added to the route Method randomUpdate: Method randomUpdate also updates the position. This time no value is provided by the user. Instead, we simulate the movement of the GPS by calculating a random position within a limited range of the current position Calculate the latitude by adding a random numbers from the range [-0.5, 0.5) to the latitude of the current position. Then calculate the longitude by adding another random number form the same range to the longitude of the current position. Assume the elevation remains unchanged When the method randomUpdate is called, the newly position needs to be added to the route Method position: This method returns the current position. The current position is the last element of the route Method distanceTraveled: This method calculates the distance that has been traveled by calculating the sum of all the distances between the positions that are listed in the route from the original start position all the way to the current position. The distance should be measured in km. In order to calculate the distance between two gps positions call the method distance. stance: n the disabett sthat tn inthi al gaiam This method should return the distance between two gps positions in km. Notice that minus in the UML class diagram. It indicates that this method should be declared private Calculate the distance by using the Haversine formula as shown in this algorithm, You'll need to make two important adaptations though: the different parameter list (as specified in the UML diagram) and the unit (km instead of m) Caveat: remember that the elevation is measured in m Method reset This method resets the route to the current position, which is the last element of the list route. After a reset, the route should include only one element: the current position Use the jUnit tests to test and troubleshoot class Gps before moving on to the next class Video: Create a screen recording (about 2-3 min) similar to the one you did for the first assignment Start with your name. Have the editor pane wide open. Show the code of both classes by calmly scrolling down the code from top to bottom. Point out any known problems with the code. When done compile and run the unit tests. Turning in: Submit both the video AND a jar file that includes the source code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions

Question

LO 182 Are there diff erent kinds of memory?

Answered: 1 week ago