Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 3: The J# Language Objectives “J# brings the Java programming language (1.4) to the .NET platform, providing full access to the .NET Framework” • The J# language by way of example… Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-2 2 Java and J# • Using an example, let's take a tour of J# • Goal? - to demonstrate that J# can be viewed as pure Java… Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-3 3 Banking Application • Neighborhood bank needs app to process daily transactions – daily transactions reside in a file "transactions.txt" – at night, transactions are applied to customer accounts stored in "customers.txt" transactions.txt customers.txt Introducing Microsoft J# in Visual Studio CS using .NET .NET Banking App 3-4 4 Program design • Design will consist of 4 classes: – App, Customer, CustomersIO, Transactions n App Customer represents 1 Customer 1 main( ) CustomersIO 1 read(filename) : Customer[ ] write(customers, filename) 1 Transactions process(customers, filename) Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-5 5 Main program • Coordinates transaction processing 1. read customer data into array of Customer objects 2. process transactions by updating objects 3. write objects back to file when done public class App { public static void main(String[] args) { System.out.println("** Banking Application **"); Customer[] customers; customers = CustomersIO.read("customers.txt"); Transactions.process(customers, "transactions.txt"); CustomersIO.write(customers, "customers.txt"); System.out.println("** Done **"); // keep console window open... . . . Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-6 6 Exception handling • Main program should deal with possibility of exceptions: public class App { public static void main(String[] args) { try { System.out.println("** Banking Application **"); . . . System.out.println("** Done **"); } catch(Exception ex) { System.out.println(">> ERROR: " + ex.toString()); } finally { // keep console window open... System.out.println(); System.out.print("Press ENTER to exit..."); try { System.in.read(); } catch(Exception ex) { } } }//main }//class Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-7 7 Build & run… • Stub out the classes & methods, build & run – test normal execution… – test throwing a java.io.IOException… Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-8 8 Customer Customer class • Represents 1 bank customer – minimal implementation at this point… public class Customer { public String firstName, lastName; public int id; public double balance; firstName : String lastName : String id : int balance : double Customer(fn, ln, id, balance) toString( ) : String // fields public Customer(String fn, String ln, int id, double balance) { this.firstName = fn; // constructor this.lastName = ln; this.id = id; this.balance = balance; } public String toString() // method { return this.id + ": " + this.lastName + ", " + this.firstName; } } Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-9 9 CustomersIO CustomersIO class read(filename) : Customer[ ] write(customers, filename) • Reads & writes the customer data • File format: – first line is total # of customers in file – then firstname, lastname, id, balance for each customer… 7 Jim Bag 123 500.0 Jane Doe 456 500.0 . . . Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-10 10 CustomersIO.read( ) • First implement read( ), which returns an array of customers… public class CustomersIO { public static Customer[] read(String filename) throws java.io.IOException { System.out.println(">> reading..."); java.io.FileReader file = new java.io.FileReader(filename); java.io.BufferedReader reader = new java.io.BufferedReader(file); Customer[] customers; String fn,ln; int id,N; N = Integer.parseInt(reader.readLine()); customers = new Customer[N]; for (int i = 0; i < N; i++) { fn = reader.readLine(); ln = reader.readLine(); id = Integer.parseInt(reader.readLine()); balance = Double.parseDouble(reader.readLine()); customers[i] = new Customer(fn, ln, id, balance); }//for reader.close(); return customers; double balance; … "Jane", … "Jim", … } Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-11 11 Build & run… • Let's test the input code… public class App { public static void main(String[] args) { try { System.out.println("** Banking Application **"); Customer[] customers; customers = CustomersIO.read("customers.txt"); for (int i = 0; i < customers.length; i++) System.out.println(customers[i]); Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-12 12 CustomersIO.write( ) • Now let's implement write( )… … "Jane", … public class CustomersIO { . . . "Jim", … public static void write(Customer[] customers, String filename) throws java.io.IOException { System.out.println(">> writing..."); java.io.FileWriter file = new java.io.FileWriter(filename); java.io.PrintWriter writer = new java.io.PrintWriter(file); writer.println(customers.length); for (int i = 0; i < customers.length; i++) { writer.println(customers[i].firstName); writer.println(customers[i].lastName); writer.println(customers[i].id); writer.println(customers[i].balance); }//for writer.close(); } Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-13 13 Build & run… • To test, just run it twice… – first run outputs to "customers.txt" – second run will re-input the new file & check its format Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-14 14 Transactions Transactions class process(customers, filename) • Reads the bank transactions & updates the customers • File format: – customer id, Deposit or Withdraw, then amount – last transaction is followed by -1 123 D 100.0 456 W 100.0 . . . -1 Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-15 15 Transactions.process( ) • Read Tx, find customer, update customer, repeat… public class Transactions { public static void process(Customer[] customers, String filename) throws java.io.IOException { System.out.println(">> processing..."); java.io.FileReader file = new java.io.FileReader(filename); java.io.BufferedReader reader = new java.io.BufferedReader(file); String action; int id; double amount; Customer c; id = Integer.parseInt(reader.readLine()); while (id != -1) // for each transaction... { action = reader.readLine(); amount = Double.parseDouble(reader.readLine()); c = findCustomer(customers, id); if (action.equals("D")) c.balance += amount; // deposit else c.balance -= amount; // withdrawal id = Integer.parseInt(reader.readLine()); // next Tx please... }//while reader.close(); } Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-16 16 findCustomer( ) • Performs a linear search, returning first matching customer… … "Jane", … . . . "Jim", … private static Customer findCustomer(Customer[] customers, int id) { for (int i = 0; i < customers.length; i++) // for each customer... if (customers[i].id == id) return customers[i]; // if get here, not found... return null; } }//class Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-17 17 Build & run… • Program should be complete at this point! – but let's check with debugging output – extend Customer to output balance… public class Customer { . . . public String toString() { return this.id + ": " + this.lastName + ", " + this.firstName + ": " + this.getFormattedBalance(); } public String getFormattedBalance() { java.text.DecimalFormat formatter; formatter = new java.text.DecimalFormat("$#,##0.00"); return formatter.format(this.balance); } }//class Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-18 18 J# is Java! • Program we just developed is pure Java – compiles with both Visual Studio .NET and Sun's JDK… transactions.txt customers.txt Introducing Microsoft J# in Visual Studio CS using .NET .NET Banking App 3-19 19 Compiling with Java: • If you have Sun's JDK installed, you can compile and run the app we just built: – rename .jsl files to .java – javac *.java – create sub-directory called BankingApp (since files are part of the package BankingApp) – move .class files into BankingApp sub-directory – copy "customers.txt" and "transactions.txt" into current directory (not the BankingApp sub-directory) – java BankingApp/App Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-20 20 JavaDoc comments • Support for JavaDoc comments provided by Visual Studio .NET – Tools menu, Build Comment Web Pages… • @param and @return are supported; others are ignored (@author, @version, and @see) package BankingApp; /** * Main class for Banking Application. * * Author: Joe Hummel, * Date: April 2004 */ public class App { . . . Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-21 21 Summary • J# is Java on the .NET platform – at least at the level of Java 1.4, with a subset of the class libraries. – eventually we'll see what else J# can do… Introducing Microsoft J# in Visual Studio CS using .NET .NET 3-22 22