Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Last active August 27, 2018 04:24
Show Gist options
  • Save rr-codes/0a4f8ef38498fc92c8c321bf13c2a3ac to your computer and use it in GitHub Desktop.
Save rr-codes/0a4f8ef38498fc92c8c321bf13c2a3ac to your computer and use it in GitHub Desktop.
EECS 1021 Lab Test 2 Practice
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
final int SAFE_DIST = 5; // declares main variable
Scanner fileName = new Scanner(System.in); // reads filename
System.out.print("Please enter the name of the input file: "); // output
Scanner inFile = new Scanner(new File(fileName.nextLine())); // reads file from filename
fileName.close(); // close filename scanner
ArrayList<Float> list = new ArrayList<>(); // create list
while (inFile.hasNextLine()) {
String[] inputLine = inFile.nextLine().split(","); // split each line at comma
Float distance = Float.parseFloat(inputLine[1]); // convert distance to float
list.add(distance); // add distance to list
}
inFile.close(); // close file scanner
Collections.sort(list); // sort list
float minDiff = Float.MAX_VALUE; // set init minimum diff
for (int i = 1; i < list.size(); i++) {
float diff = list.get(i) - list.get(i - 1); // difference between each two elements
minDiff = Math.min(minDiff, diff); // find the lesser value of thw two vars
}
if (minDiff < SAFE_DIST) {
System.out.println("They are not in safe orbits.");
} else {
System.out.println("They are in safe orbits.");
}
System.out.println("Minimum distance is " + minDiff + " km.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment