Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the Code in Java? The code to start is below. /**************************************************************** * These are the jUnit tests to test the class GpsPosition. *

What is the Code in Java? The code to start is below.

image text in transcribed

image 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(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 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 GpsPosition 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 GpsPosition - latitude double longitude: double uble eva + GpsPosition ( latitude: double, longitude: double, elevation double) +getLatitudedouble +getLongitude : doubie + geevaton () : double +toString ): 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 TllegalArgumentException("Invalid latitude 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 Lotitude, 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: 39.573645, -109.546389 1227.1) Use the jUnit tests to test and troubleshoot Gps Position before moving on to the next class

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

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions