Question

In: Computer Science

JAVA use the example and write program Choose five international source currencies to monitor. Each currency...

JAVA

use the example and write program

Choose five international source currencies to monitor. Each currency is referenced with a three letter ISO 4217 currency code. For example, the code for the U.S. Dollar currency is USD. Search online for these abbreviations with a search string such as "ISO 4217 Currency Codes." Place these currency codes in a text file.

The following URL is a link to a CSV file that contains the exchange rate for a given source and target currency. For example, if the source currency is EUR and the target currency is USD, the URL is

http://download.finance.yahoo.com/d/quotes.csv?s=EURUSD=X&f=sl1d1t1ba&e=.csv

Read the five source currencies in the text file that you created in Step 1, dynamically create URLs that are passed to Java Scanner objects to obtain the exchange rates (to USD) for five source currencies from Part 1.

Plot the five exchange rates that you found in Step 2 in a JFreeChart bar chart.

BarChart Example the jfreechart example file.

jfreechart Examples, methods.txt File

=======================================================

// BarChart Example
// Source code file LineChart.java
// Create a bar chart using JFreeChart library.
//
// JAR files jcommon-1.0.23.jar and jfreechart-1.0.19.jar
// must be added to project.

import java.io.*;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
public class BarChart {
    public static void main(String[] args) {
        try {
              
            // Define data for line chart.
            DefaultCategoryDataset barChartDataset =
                new DefaultCategoryDataset();
            barChartDataset.addValue(1435, "total", "East");
            barChartDataset.addValue(978, "total", "North");
            barChartDataset.addValue(775, "total", "South");               
            barChartDataset.addValue(1659, "total", "West");               
               
            // Define JFreeChart object that creates line chart.
            JFreeChart barChartObject = ChartFactory.createBarChart(
                "Sales ($1000)", "Region", "Sales", barChartDataset,
                PlotOrientation.VERTICAL,
                false, // Include legend.
                false, // Include tooltips.
                false); // Include URLs.              
                         
             // Write line chart to a file.              
             int imageWidth = 640;
             int imageHeight = 480;               
             File barChart = new File("sales.png");             
             ChartUtilities.saveChartAsPNG(
                 barChart, barChartObject, imageWidth, imageHeight);
        }
     
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}

=======================================================

// LineChart Example
// Source code file LineChart.java
// Create a line chart using JFreeChart library.
//
// JAR files jcommon-1.0.23.jar and jfreechart-1.0.19.jar
// must be added to project.

import java.io.*;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
public class LineChart {
    public static void main(String[] args) {
        try {
              
            // Define data for line chart.
            DefaultCategoryDataset lineChartDataset =
                new DefaultCategoryDataset();
            lineChartDataset.addValue(15, "schools", "1970");
            lineChartDataset.addValue(30, "schools", "1980");
            lineChartDataset.addValue(60, "schools", "1990");
            lineChartDataset.addValue(120, "schools", "2000");
            lineChartDataset.addValue(240, "schools", "2010");               
               
            // Define JFreeChart object that creates line chart.
            JFreeChart lineChartObject = ChartFactory.createLineChart(
                "Schools Vs Years", "Year", "Schools Count", lineChartDataset,
                PlotOrientation.VERTICAL,
                true,   // Include legend.
                true,   // Include tooltips.
                false); // Don't include URLs.              
                         
             // Write line chart to a file.              
             int imageWidth = 640;
             int imageHeight = 480;               
             File lineChart = new File("line-chart.png");             
             ChartUtilities.saveChartAsPNG(
                 lineChart, lineChartObject, imageWidth, imageHeight);
        }
     
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}

input-output Examples, input-output.txt File
=======================================================

// TestAmtToWords Example
// Source code file TestAmtToWords.java
// Input a double amount < 100.00 and convert it to
// words suitable for using as the amount on a check.
public class TestAmtToWords {

    public static void main(String[] args) {
        
        System.out.println(amtToWords(63.45));
        System.out.println(amtToWords(18.06));
        System.out.println(amtToWords(0.97));
    }
    
    public static String amtToWords(double amt) {
        
        // Extract pieces of input.
        int dollars = (int) amt;
        int tens = dollars / 10;
        int ones = dollars % 10;
        int cents = (int) Math.round(100 * (amt - dollars));
        
        // Definitions of words for digits.
        String[ ] onesWords = {"zero", "one", "two", "three", "four", 
                   "five", "six", "seven", "eight", "nine"};
        String[ ] teensWords = {"", "eleven", "twelve", "thirteen",
                   "fourteen", "fifteen", "sixteen", "seventeen",
                   "eighteen", "nineteen"};
        String[ ] tensWords = {"", "ten", "twenty", "thirty", "forty",
                   "fifty", "sixty", "seventy", "eighty", "ninety"};
        
        // Declare output variable.
        String output = "";
        
        if (dollars <= 9) {
            output = onesWords[ones]; 
        }
        else if (dollars <= 19) {
            output = teensWords[ones];
        }
        else if (dollars <= 99) {
            output = tensWords[tens] + " " + onesWords[ones];
        }
        else {
            output = "Out of range";
        }
        return output + " and " + cents + "/100";
    }
}

=======================================================

// TestScanner Example
// Source code file TestScanner.java
// Test a Scanner object that reads from a string.
// Also include code that uses the String method 
// split to do the same thing.
import java.util.Scanner;
public class TestScanner {

    public static void main(String[] args) {
        Scanner scnr = new Scanner("This is a test.");
        scnr.useDelimiter(" ");
        while (scnr.hasNext( )) {
            String word = scnr.next( );
            System.out.println(word);
        }
        scnr.close( );
        
        System.out.println( );
        
        String s = "This is a test.";
        String[ ] fields = s.split(" ");
        for(String word : fields) {
            System.out.println(word);
        }    
    }
}

=======================================================

// ReadFromWeb Example
// Source code file ReadFromWeb.java
// Read raw HTML lines from input file and
// print them to stdout.
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class ReadFromWeb {

    public static void main(String[] args) 
        throws MalformedURLException, IOException {

        Scanner in = new Scanner((new URL(
            "http://facweb.cdm.depaul.edu/sjost/it313/test.htm")).
            openStream( ));
        
        while (in.hasNextLine( )) {
            String line = in.nextLine( );
            System.out.println(line);
        }
        
        in.close( );
    }
}

=======================================================

// WriteGreetings Example
// Source code file WriteGreetings.java
// Read names of persons from input file, then write
// greetings to each of them, each in their own
// output file.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class WriteGreetings {

    public static void main(String[] args) throws FileNotFoundException {

        // Declare PrintWriter reference variable.
        PrintWriter pw;
        
        // Write greeting to Larry in greeting.txt output file.
        pw = new PrintWriter("greeting.txt");
        pw.println("Hello, Larry, how are you?");
        pw.close( );
        
        // Write greetings to persons, each in their own
        // output file.
        Scanner in = new Scanner(new File("names.txt"));
        while (in.hasNextLine( )) {
            String name = in.nextLine( );
            String greeting = "Hello " + name + ", how are you?";
            String fileName = name + ".txt";
            pw = new PrintWriter(fileName);
            pw.println(greeting);
            pw.close( );
        }
        in.close( );
    }
}

=======================================================

// UseFileChooser Example
// Source code file UseFileChooser
// Open an output file with a FileChooser dialog.
// Read names and write greetings as in the WriteGreetings Example
import java.io.*;
import java.util.Scanner;

import javax.swing.JFileChooser;

    public class UseFileChooser {

        public static void main(String[] args) throws FileNotFoundException {

            // Open file containing names with FileChooser dialog
            JFileChooser chooser = new JFileChooser( );
            chooser.showOpenDialog(null);
            File fileObj = chooser.getSelectedFile( );

            // Read names and write greetings, each in their own file.
            Scanner in = new Scanner(fileObj);
            while (in.hasNextLine( )) {
                String name = in.nextLine( );
                String greeting = "Hello " + name + ", how are you?";
                
                PrintWriter pw = new PrintWriter(name + ".txt");
                pw.println(greeting);
                pw.close( );
            }
            in.close( );
        }
    }

Solutions

Expert Solution

#include&lt;

iostream&gt;

#include&lt;

string&gt;

#include&lt;

stdlib&gt;

// during this example items aer represented as number values

// we are going to build them constants, in order that if at any time we would like to alternate

public class TestAmtToWords {

public static void main(String[] args) {

System.out.println(amtToWords(63.45));

System.out.println(amtToWords(18.06));

System.out.println(amtToWords(0.97));

}

public static String amtToWords(double amt) {

// Extract pieces of input.

int dollars = (int) amt;

int tens = dollars / 10;

int ones = dollars % 10;

int cents = (int) Math.round(100 * (amt - dollars));

// Definitions of words for digits.

String[ ] onesWords = {"zero", "one", "two", "three", "four",

"five", "six", "seven", "eight", "nine"};

String[ ] teensWords = {"", "eleven", "twelve", "thirteen",

"fourteen", "fifteen", "sixteen", "seventeen",

"eighteen", "nineteen"};

String[ ] tensWords = {"", "ten", "twenty", "thirty", "forty",

"fifty", "sixty", "seventy", "eighty", "ninety"};

// Declare output variable.

String output = "";

if (dollars <= 9) {

output = onesWords[ones];

}

else if (dollars <= 19) {

output = teensWords[ones];

}

else if (dollars <= 99) {

output = tensWords[tens] + " " + onesWords[ones];

}

else {

output = "Out of range";

}

return output + " and " + cents + "/100";

}

}

=======================================================

// TestScanner Example

// Source code file TestScanner.java

// Test a Scanner object that reads from a string.

// Also include code that uses the String method

// split to do the same thing.

import java.util.Scanner;

public class TestScanner {

public static void main(String[] args) {

Scanner scnr = new Scanner("This is a test.");

scnr.useDelimiter(" ");

while (scnr.hasNext( )) {

String word = scnr.next( );

System.out.println(word);

}

scnr.close( );

System.out.println( );

String s = "This is a test.";

String[ ] fields = s.split(" ");

for(String word : fields) {

System.out.println(word);

}

}

}

=======================================================

// ReadFromWeb Example

// Source code file ReadFromWeb.java

// Read raw HTML lines from input file and

// print them to stdout.

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Scanner;

public class ReadFromWeb {

public static void main(String[] args)

throws MalformedURLException, IOException {

Scanner in = new Scanner((new URL(

"http://facweb.cdm.depaul.edu/sjost/it313/test.htm")).

openStream( ));

while (in.hasNextLine( )) {

String line = in.nextLine( );

System.out.println(line);

}

in.close( );

}

}

=======================================================

// WriteGreetings Example

// Source code file WriteGreetings.java

// Read names of persons from input file, then write

// greetings to each of them, each in their own

// output file.

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class WriteGreetings {

public static void main(String[] args) throws FileNotFoundException {

// Declare PrintWriter reference variable.

PrintWriter pw;

// Write greeting to Larry in greeting.txt output file.

pw = new PrintWriter("greeting.txt");

pw.println("Hello, Larry, how are you?");

pw.close( );

// Write greetings to persons, each in their own

// output file.

Scanner in = new Scanner(new File("names.txt"));

while (in.hasNextLine( )) {

String name = in.nextLine( );

String greeting = "Hello " + name + ", how are you?";

String fileName = name + ".txt";

pw = new PrintWriter(fileName);

pw.println(greeting);

pw.close( );

}

in.close( );

}

}


Related Solutions

This program must be done in JAVA. Write a program to monitor the flow of an...
This program must be done in JAVA. Write a program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT