Skip to content

Instantly share code, notes, and snippets.

@sahat
Created September 9, 2011 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahat/1205458 to your computer and use it in GitHub Desktop.
Save sahat/1205458 to your computer and use it in GitHub Desktop.
homework 1 for cs221
// Name: Sahat Yalkabov
// Date: September 15, 2011
// Class: CS22100
// Homework 1: Radiation Data Log
import java.util.ArrayList;
import java.util.Collections; // to get minimum and maximum
import javax.swing.JOptionPane;;
class hw1 {
public static void main(String[] args) {
ArrayList<Double> data = new ArrayList<Double>();
String value = ""; // temporary string before it's added to ArrayList
String historical = ""; // value that's compared against with average data
double min = 0;
double max = 0;
double mean = 0;
double total = 0;
try {
historical = JOptionPane.showInputDialog("Enter historical background measurement: ");
while (historical.equals("")) {
historical = JOptionPane.showInputDialog("---THE INPUT CANNOT BE BLANK---\n" +
"Enter historical background measurement: ");
}
// runs until encounters if-statement that checks if 'q' is entered.
// didn't use while(!value.equals("q")) to break from the loop because
// it didn't get out of the loop right away, thus proceeding to the
// data.add(Double.parseDouble(value)); which tries to parse "q" into double
// causing exception error.
while (true) {
value = JOptionPane.showInputDialog("Enter more background measurements: ");
while (value.equals("")) {
value = JOptionPane.showInputDialog("---THE INPUT CANNOT BE BLANK---\n" +
"Enter historical background measurement: ");
}
if (value.equals("q")) {
break;
}
data.add(Double.parseDouble(value));
}
// if-statement to prevent exceptions caused by the user not inputing any values at all,
// typing "q" to exit the loop.
if (data.size() > 0) {
for (int i=0; i<data.size(); i++) {
total += data.get(i);
}
mean = total / data.size();
min = Collections.min(data);
max = Collections.max(data);
}
JOptionPane.showMessageDialog(null, "Minimum: " + min +
"\nMaximum: " + max + "\nMean: " + mean);
if (mean > (Double.parseDouble(historical)*0.1 + Double.parseDouble(historical)))
JOptionPane.showMessageDialog(null, "Warning: Radiation levels are unusually high!",
"WARNING", JOptionPane.WARNING_MESSAGE);
}
// If at any point user clicks Cancel or presses ESC, exception will be caught
// with a message dialogue and then terminating.
catch (NullPointerException n) {
JOptionPane.showMessageDialog(null, "Goodbye!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment