In: Computer Science
Java Programming
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Scanner;
import javax.swing.JFrame;
import org.math.plot.Plot2DPanel;
import java.awt.Container;
import java.awt.BorderLayout;
public class Heart {
public static LinkedHashMap<String,double[]> readData(final Scanner fsc) {
final LinkedHashMap<String, double[]> result = new LinkedHashMap<String, double[]>();
fsc.nextLine();
String State;
String line;
String[] parts;
double[] values;
while (fsc.hasNextLine()) {
line = fsc.nextLine();
parts = line.split("\t");
State = parts[0];
values = new double[parts.length - 1];
for (int i = 1; i < parts.length; i++) {
values[i - 1] = Double.parseDouble(parts[i]);
}
result.put(State, values);
}
return result;
}
public static double[] getDays(final int howMany) {
final double[] result = new double[howMany];
for (int i = 0; i < howMany; i++) {
result[i] = i;
}
return result;
}
public static void setUpAndShowPlot(final Plot2DPanel plot) {
final JFrame frm = new JFrame();
frm.setBounds(100, 100, 500, 500);
frm.setTitle("Investment Curves");
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final Container c = frm.getContentPane();
c.setLayout(new BorderLayout());
plot.addLegend("SOUTH");
plot.setAxisLabels("Day", "Deaths");
c.add(plot, BorderLayout.CENTER);
frm.setVisible(true);
}
public static void main(final String[] args) throws Exception {
LinkedHashMap<String, double[]> accounts;
String inputtedStates;
String[] names;
final Scanner sc = new Scanner(System.in);
double[] DeathValues;
try {
final Scanner fsc = new Scanner(new File("States.txt"));
accounts = readData(fsc);
} catch (final Exception ex) {
accounts = null;
}
if (accounts == null) {
System.out.println("Couldn't read the file.");
} else {
do {
System.out.print("Enter name of States separated by commas: ");
inputtedStates = sc.nextLine();
if (!inputtedStates.equalsIgnoreCase("exit")) {
final Plot2DPanel plot = new Plot2DPanel();
names = inputtedStates.split(",");
for (String name : names) {
name = name.trim();
if (!accounts.containsKey(name)) {
System.out.printf("%s is not in the data.\n", name);
} else {
DeathValues = accounts.get(name);
plot.addLinePlot(name, getDays(DeathValues.length), DeathValues);
}
}
// configure and show the frame that houses the plot
setUpAndShowPlot(plot);
}
} while (!inputtedStates.equalsIgnoreCase("exit"));
System.out.println("Thank you");
}
}
}
This code uses a tab delimited text file to graph
cumulative Heart attack deaths per state. below is an example of
the text files format. Instead of having the name if the file built
in ("States.txt") change the code so it askes the user to input a
name of a text file
Also the Data is cumulative in the text file. This Data must be
turned in daily totals to be graphed. for example Ohio day 1 would
have 5 deaths ohio day 2 would have 5 deaths and ohio day 7 would
have 11 deaths. Please make the proper changes to the code
Note: jmathio and jmathplot jar files are required
State 0 1 2
3 4 5
Ohio 5 10 17
21 32 50
Texas 2 7 9
45 51 56
Florida 5 8 12
18 27 51
Utah 1 3 7
9 18 21
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Scanner;
import javax.swing.JFrame;
import org.math.plot.Plot2DPanel;
import java.awt.Container;
import java.awt.BorderLayout;
public class Heart {
// function to read data from file and put into
map
public static LinkedHashMap<String,double[]> readData(final
Scanner fsc) {
final LinkedHashMap<String, double[]> result = new LinkedHashMap<String, double[]>();
fsc.nextLine();
String State;
String line;
String[] parts;
double[] values;
while (fsc.hasNextLine()) {
line = fsc.nextLine();
parts = line.split("\\s+");//splits on multiple
spaces
State = parts[0];
double temp=0;
values = new double[parts.length - 1];
for (int i = 1; i < parts.length; i++) {
values[i - 1] = Double.parseDouble(parts[i])-temp;//
store daily deaths in values array
temp=Double.parseDouble(parts[i]);// stores previous day
value
}
result.put(State, values);
}
return result;
}
public static double[] getDays(final int howMany) {
final double[] result = new double[howMany];
for (int i = 0; i < howMany; i++) {
result[i] = i;
}
return result;
}
// function to set up and show plot
public static void setUpAndShowPlot(final Plot2DPanel plot) {
final JFrame frm = new JFrame();
frm.setBounds(100, 100, 500, 500);
frm.setTitle("Investment Curves");
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final Container c = frm.getContentPane();
c.setLayout(new BorderLayout());
plot.addLegend("SOUTH");
plot.setAxisLabels("Day", "Deaths");
c.add(plot, BorderLayout.CENTER);
frm.setVisible(true);
}
// driver code
public static void main(final String[] args) throws Exception {
LinkedHashMap<String, double[]> accounts;
String inputtedStates;
String[] names;
final Scanner sc = new Scanner(System.in);
double[] DeathValues;
try {
System.out.print("Enter the input file:
");
String file=sc.next();// user enters input file
name
sc.nextLine();
final Scanner fsc = new Scanner(new File(file));
accounts = readData(fsc);
} catch (final Exception ex) {
accounts = null;
}
if (accounts == null) {
System.out.println("Couldn't read the file.");
} else {
do {
System.out.print("Enter name of States separated by commas: ");
inputtedStates = sc.nextLine();
if (!inputtedStates.equalsIgnoreCase("exit")) {
final Plot2DPanel plot = new Plot2DPanel();
names = inputtedStates.split(",");
for (String name : names) {
name = name.trim();
if (!accounts.containsKey(name)) {
System.out.printf("%s is not in the data.\n", name);
} else {
DeathValues = accounts.get(name);
plot.addLinePlot(name, getDays(DeathValues.length), DeathValues);
}
}
// configure and show the frame that houses the plot
setUpAndShowPlot(plot);
}
} while (!inputtedStates.equalsIgnoreCase("exit"));
System.out.println("Thank you");
}
}
}