Question

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;...

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

Solutions

Expert Solution


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");

}

}

}


Related Solutions

In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
JAVA - Complete the directions in the 5 numbered comments import java.util.Scanner; /** * This program...
JAVA - Complete the directions in the 5 numbered comments import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count =...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Need to able to solve this method WITHOUT using ANY java libraries. ------------------------------ import java.util.Scanner; public...
Need to able to solve this method WITHOUT using ANY java libraries. ------------------------------ import java.util.Scanner; public class nthroot { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read n (for taking the nth root) int num = Integer.parseInt(sc.nextLine());    // Read number to take the nth root of int number = Integer.parseInt(sc.nextLine());    // Read the desired precision int precision = Integer.parseInt(sc.nextLine());    // Print the answer System.out.println(findnthRoot(number, num, precision)); }    private static String...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry {...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry { private String month; private int day; private int year; //a four digit number. public void writeOutput() { System.out.println(month + " " + day + ", " + year); } public void readInput() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter month, day, and year."); System.out.println("Do not use a comma."); month = keyboard.next(); day = keyboard.nextInt(); year = keyboard.nextInt(); } public int getDay() { return day;...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT