Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Not reading fileimport java.io . File; import java.io . FileNotFoundException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; class Earthquake { private Date dateTime; private

Not reading fileimport java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
class Earthquake {
private Date dateTime;
private double latitude;
private double longitude;
private double magnitude;
private String location;
public Earthquake(String dateTime, double latitude, double longitude, double magnitude, String location)
throws ParseException {
this.dateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(dateTime);
this.latitude = latitude;
this.longitude = longitude;
this.magnitude = magnitude;
this.location = location;
}
public boolean isInRegion(double minLat, double maxLat, double minLon, double maxLon){
return latitude >= minLat && latitude <= maxLat && longitude >= minLon && longitude <= maxLon;
}
public boolean isInRange(Date minDate, Date maxDate){
return dateTime.compareTo(minDate)>=0 && dateTime.compareTo(maxDate)<=0;
}
public boolean isMagnitudeGreaterOrEqual(double minMag){
return magnitude >= minMag;
}
public String formattedOutput(){
SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMM yy HHmm'Z'");
String formattedDateTime = outputFormat.format(dateTime);
return String.format("%s,(%.2f,%.2f), Mag: %.1f,%s", formattedDateTime, latitude, Math.abs(longitude),
magnitude, location);
}
}
public class quakeFinder {
private static final int MAX_LENGTH =125000;
private Earthquake[] earthquakes = new Earthquake[MAX_LENGTH];
private int numQuakes =0;
public static void main(String[] args){
quakeFinder quakeFinder = new quakeFinder();
quakeFinder.readDataFromFile("quakes.txt");
quakeFinder.runQueryInterface();
}
public void readDataFromFile(String filename){
try (Scanner scanner = new Scanner(new File("quakes.txt"))){
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] quakeData = line.split("\\|");
if (quakeData.length ==5){
try {
earthquakes[numQuakes]= new Earthquake(quakeData[0], Double.parseDouble(quakeData[1]),
Double.parseDouble(quakeData[2]), Double.parseDouble(quakeData[3]), quakeData[4]);
numQuakes++;
} catch (ParseException | NumberFormatException e){
System.out.println("Error parsing earthquake data: "+ e.getMessage());
}
} else {
System.out.println("Invalid earthquake data format: "+ line);
}
}
} catch (FileNotFoundException e){
System.out.println("File not found: "+ "quakes.txt");
}
}
public void runQueryInterface(){
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("Enter query (R, D, M) or 'Q' to quit:");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("Q")){
break;
}
if (isValidQuery(input)){
processQuery(input);
} else {
System.out.println("Invalid query format. Please try again.");
}
}
scanner.close();
}
private boolean isValidQuery(String input){
// Validate the query format (e.g.,"R 3040-120-110","D 2020-01-01T00:00:00Z 2022-01-01T23:59:59Z",
//"M 5.0")
String[] tokens = input.split("\\s+");
if (tokens.length <2|| tokens.length >4){
return false;
}
if (!tokens[0].equalsIgnoreCase("R") && !tokens[0].equalsIgnoreCase("D") && !tokens[0].equalsIgnoreCase("M")){
return false;
}
switch (tokens[0]){
case "R":
return tokens.length ==4 && isDouble(tokens[1]) && isDouble(tokens[2]) && isDouble(tokens[3]);
case "D":
return tokens.length ==3 && isValidDate(tokens[1]) && isValidDate(tokens[2]);
case "M":
return tokens.length ==2 && isDouble(tokens[1]);
default:
return false;
}
}
private void processQuery(String input){
String[] tokens = input.split("\\s+");
switch (tokens[0]){
case "R":
double minLat = Double.parseDouble(tokens[1]);
double maxLat = Double.parseDouble(tokens[2]);
double minLon = Double.parseDouble(tokens[3]);
double maxLon = Double.parseDouble(tokens[4]);
printQuakesInRegion(minLat, maxLat, minLon, maxLon);
break;
case "D":
try {

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

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students explore these related Databases questions