In: Computer Science
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( ); } }
#include<
iostream>
#include<
string>
#include<
stdlib>
// 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( );
}
}