Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Shortest path utility program: Rewrite the shortest path utility program ( ShortestPath ) using classes. First, define a class named Point2D . Its internal state

Shortest path utility program: Rewrite the shortest path utility program (ShortestPath) using classes.

First, define a class named Point2D. Its internal state is defined by an x and a y coordinate (doubles of course) and a label (a String). The label is allowed to be an empty string. The class provides three instance methods:

distance, which takes another Point2D as its parameter and returns the distance between the point and its parameter;

getLabel, which returns the value of the label instance variable; and

toString, which represents the point by the label concatenated with the standard parenthetical representation of a 2-dimensional point: for example, if the label was "P" and the point was located at x==2 and y==3 then toString would yield: "P(2.0,3.0)".

In addition, the Point2D class provides four constructors:

One that has no parameters and sets the position of the point to the origin (0,0) and makes the label the empty string.

A second that has one string parameter that is used to initialize the label followed by two double parameters that are used to initialize the coordinates x and y).

A third that has a single Scanner parameter. This constructor uses the Scanner to read in a label, followed by the x and y values of the point.

Finally, a fourth constructor, that has one string parameter that is used to initialize the label followed by a single Scanner parameter that is used to read in just the x and y values of the point.

Next, define a class named Path3. It represents a labeled path consisting of 3 points. So its internal state is defined by a String label and three Point2D objects: the starting point of the path, the interior point of the path, and the end point of the path. The class provides three instance methods:

toString, which represents the path by concatenating the labels of the 3 points, separating them with dashes. So no coordinates are printed out. If the three Point2D instance variables happen to have the labels NY KC LA then toString returns "NY-KC-LA".

length, which returns the length of the path, that is the distance between the first two points in the path plus the distance of the last two points in the path.

getLabel, which returns the value of the label of the path (not the label of any of the points in the path).

In addition, the Path3 class provides two constructors:

One that has four parameters: the label, the first point in the path, the second point in the path, and the third point in the path.

A second that just has one parameter, a Scanner. It uses the Scanner to read in the label, the first point, the second point, and the third.

The program class (or application class) for this exercise is ShortestPath-- it contains the main method that usesPath3 and ultimately Point2D objects to accomplish its purpose.

Using these classes, the program named ShortestPath compares two paths. Here are the details: The program reads two lines of input like the following:

lake_route 1.4 2.9 4.3 8.7 9.0 10.0 ?meadow_route 1.1 4.7 5.3 8.9 10.0 9.0

Each line consists of a string identifying (labeling) the path followed by three pairs of doubles. Each pair of doublesrepresents the (x,y) coordinates of a point. Each line's sequence of three pairs represents a path of three points. The program creates two Path3 objects, one for each line of input. The program then prints out 3 lines:

the label and length of the first path

the label and length of the second path

the path name of the shortest path

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_2

Step: 3

blur-text-image_3

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

Compare and contrast cultural preferences for privacy

Answered: 1 week ago