Java need help. my code is below with comment, have finished most of them. // th
ID: 3746652 • Letter: J
Question
Java need help. my code is below with comment, have finished most of them.
// this is Location
//
// This class implements a place on a map. Each location
// includes a textual name and a pair of Cartesian
// coordinates. Note that textual names are assumed to be
// unique; two locations are considered the same if they
// have the same name.
//
//
import java.io.*;
import java.util.*;
public class Location {
public String name = "";
public double longitude = 0.0;
public double latitude = 0.0;
// Default constructor ...
public Location() {
this.name = "";
this.longitude = 0.0;
this.latitude = 0.0;
}
// Constructor with location name specified ...
public Location(String name) {
this();
this.name = name;
}
// Constructor with location name and coordinates specified ...
public Location(String name, double longitude, double latitude) {
this(name);
this.longitude = longitude;
this.latitude = latitude;
}
// equals -- Return true if and only if this location has the same name
// as the argument location.
public boolean equals(Location loc) {
return (loc.name.equals(this.name));
}
// read -- Read a location description from the given stream into this
// object. At minimum, a name must be read from the stream. Optionally,
// coordinates may also be specified as a pair of double precision
// floating point numbers. Return true if at least a name was read and
// false otherwise.
public boolean read(BufferedReader str) {
try {
String thisLine = str.readLine();
if (thisLine == null)
// No more input, at all ...
return (false);
Scanner inScanner = new Scanner(thisLine);
inScanner.useDelimiter("\s+");
if (inScanner.hasNext()) {
// There is something to read ...
name = inScanner.next();
if (inScanner.hasNextDouble()) {
// There is a longitude to read ...
longitude = inScanner.nextDouble();
if (inScanner.hasNextDouble()) {
// There is a latitude to read ...
latitude = inScanner.nextDouble();
}
}
inScanner.close();
// At least a name was successfully read ...
return (true);
} else {
inScanner.close();
// Did not even read a name ...
return (false);
}
} catch (IOException e) {
// Something went wrong ...
return (false);
}
}
// write -- Write the name of this location to the given stream. If the
// "showCoords" argument is true, then also output the Cartesian
// coordinates of this location, separated by blanks, on the same line.
public void write(OutputStream str, boolean showCoords) {
PrintWriter out = new PrintWriter(str, true);
out.printf("%s", name);
if (showCoords) {
out.printf(" %f %f", longitude, latitude);
}
}
}
//
// Ezero
//
// This class provides a "main" method that acts as a driver program
// for evaluating bounding box implementations. A file of locations,
// consisting of names and coordinate pairs, is read. If a location
// name is duplicated in the list, the first location is recorded
// and the duplicate discarded. The name of the duplicate location
// is reported. The bounding box that surrounds all read locations
// is then output, and this is followed by the output of all of
// the locations, with no duplicates listed. Program output is
// written to the standard output stream.
//
//
import java.io.*;
public class Ezero {
public static void main(String[] args) {
MapBox map = new MapBox();
String locationFilename = "locations.dat";
System.out.println("BOUNDING BOX TEST");
System.out.flush();
// Read the locations into the allocated MapBox object ...
try {
// Open file of locations ...
File locFile = new File(locationFilename);
if (locFile.exists() && locFile.canRead()) {
// Convert File to a BufferedReader ...
FileInputStream locFileIn = new FileInputStream(locFile);
InputStreamReader locISReader = new InputStreamReader(locFileIn);
BufferedReader locBufferedReader = new BufferedReader(locISReader);
// Read all of the locations in the file into the
// allocted MapBox object. First, allocate storage
// for the first location to be read ...
Location loc = new Location();
// Read one location at a time ...
while (loc.read(locBufferedReader)) {
// Record location in the MapBox object ...
if (!(map.recordLocation(loc))) {
// A false return value from "recordLocation"
// means that the location is a duplicate.
System.out.printf("Duplicate Location: %s ", loc.name);
} else {
// Allocate storage for the next location ...
loc = new Location();
}
}
// Output results ...
// Output size of bounding box ...
System.out.println("The Bounding Box:");
System.out.printf(" West Edge = %f ", map.Westmost());
System.out.printf(" East Edge = %f ", map.Eastmost());
System.out.printf(" North Edge = %f ", map.Northmost());
System.out.printf(" South Edge = %f ", map.Southmost());
System.out.println("The Locations:");
for (Location outloc : map.locations) {
outloc.write(System.out, true);
System.out.println("");
}
} else {
System.err.println("ERROR: Could not open file for reading.");
}
} catch (IOException e) {
// Something went wrong ...
System.err.println("ERROR: IO exception thrown.");
}
// Done ...
System.out.println("BOUNDING BOX TEST COMPLETE");
}
}
More specifically, you are to provide a class called MapBox in a source code file named "MapBox. java". Your class must have the following features. a default constructor that allocates any needed storage a public member variable called "locations" that holds a list of objects of the type Location four public accessor member functions that return (as type double) the coordinates of the bounding box: Westmost_-returns the minimum x-axis coordinate - Eastmost-returns the maximum x-axis coordinate - Southmost returns the minimum y-axis coordinate - Northmostretums the maximum y-axis coordinate . a public member function called recordlocation that takes a single Location object as an argument and adds that object to the locations list, but only if the given Location object does not have the same name as any previously recorded location In general, your classes should allow the main method in the provided Ezero class to output correct solutions, including the coordinates of the bounding box and a complete list of locations, without duplicates. Note that this means that the member functions of the class that you implement should write no output to the standard output stream, as this will clutter the output produced by the Ezero class main method. If you include any statements that write output in your code (perhaps as tools for debugging) these should be removed prior to submitting your source code file for evaluation. You will not receive credit for the exercise if your solution produces extraneous output. The Java™ utility classes that you are required to use are provided in a ZIP archive file called "EXO.zip which is available in the "Assignments" section of the class CatCourses site, under "Exercise #0". These utilities incl ude: LocationThis object encodes a location on the map. It includes a location name, a longitude (x-axis coordinate), and a latitude (y-axis coordinate. Ezero This object provides a top-level driver that tests your solution. Your code must allow Ezero to produce correct output. The contents of these JavaM utility files will be discussed during a course laboratory session, and inline comments in these files should assist in your understanding of the provided code. Questions are welcome, however, and should be directed to the teaching team.Explanation / Answer
MapBox.java
import java.util.ArrayList;
/**
* Provides the coordinates of bounding box
* Maintains list of locations without duplicates
*/
class MapBox {
ArrayList<Location> locations; //list of object of type Location
/**
* Default constructor
*/
public MapBox() {
locations=new ArrayList<Location>();
}
/**
* Add locations to location list after checking for duplication
* @param loc location to be added to list
* @return true if location is added, false other wise
*/
boolean recordLocation(Location loc) {
for(int i=0;i<locations.size();i++)
{
if(locations.get(i).equals(loc))
return false;
}
locations.add(loc);
return true;
}
/**
* Return the coordinates of bounding box
* @return the minimum xaxis coordinate
*/
double Westmost() {
double minx=locations.get(0).longitude;
for (Location theLoc : locations)
{
if(theLoc.longitude<minx)
minx=theLoc.longitude;
}
return minx;
}
/**
* Return the coordinates of bounding box
* @return the maximum xaxis coordinate
*/
double Eastmost() {
double maxx=locations.get(0).longitude;
for (Location theLoc : locations)
{
if(theLoc.longitude>maxx)
maxx=theLoc.longitude;
}
return maxx;
}
/**
* Return the coordinates of bounding box
* @return the maximum yaxis coordinate
*/
double Northmost() {
double maxy=locations.get(0).longitude;
for (Location theLoc : locations)
{
if(theLoc.latitude<maxy)
maxy=theLoc.latitude;
}
return maxy;
}
/**
* Return the coordinates of bounding box
* @return the minimum yaxis coordinate
*/
double Southmost() {
double miny=locations.get(0).longitude;
for (Location theLoc : locations)
{
if(theLoc.latitude<miny)
miny=theLoc.latitude;
}
return miny;
}
}
sample input file : locations.dat
abc 23.44 23.55
def 45.8 23.9
ghi 34.6 78.9
abc 23.44 23.55
jkl 34.56 90.8
mno 34.67 23.56
pqr 23.4 12.8
xyz 23.5 67.8
sample output using the above input
BOUNDING BOX TEST
Duplicate Location: abc
The Bounding Box:
West Edge = 23.400000
East Edge = 45.800000
North Edge = 12.800000
South Edge = 12.800000
The Locations:
abc 23.440000 23.550000
def 45.800000 23.900000
ghi 34.600000 78.900000
jkl 34.560000 90.800000
mno 34.670000 23.560000
pqr 23.400000 12.800000
xyz 23.500000 67.800000
BOUNDING BOX TEST COMPLETE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.