Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Best Route Rewrite the Best Route utility program ( BestRoute ) using classes. First, define a class named Point2D . Its internal state is defined

Best Route

Rewrite the Best Route utility program (BestRoute) 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 BestRoute-- it contains the main methodthat uses Path3 and ultimately Point2D objects to accomplish its purpose.

Using these classes, write a program named BestRoute that reads in the coordinates of four points on a two-dimensional surface (a plane). Each point has an x-coordinate and a y-coordinate. Each point has a one-token label (which is read in, as part of the input), for example:

A 5.0 5.0 Q 10.0 10.0 R 7.0 7.0 ?Z 6.0 6.0

The program computes the total distance involved in traveling from the first point (in this example 5.0,5.0) to the last point (6.0,6.0) via the second point (10.0,10.0 in this example) and the total distance via the third point (7.0,7.0 in this case). In other words, it computes the path length of A-Q-Z and the path length of A-R-Z. It then displays the shorter path by printing the labels of the points in the shorter path:

Best Route: A-R-Z

(The other possibility would have been "A Q Z" but the program should have determined that A-Q-Z was longer than A-R-Z.)

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions