Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: The J# Language

Similar presentations


Presentation on theme: "Lecture 3: The J# Language"— Presentation transcript:

1 Lecture 3: The J# Language
Summer 2004 Lecture 3: The J# Language © 2004 Microsoft

2 Objectives 2. The J# Language Summer 2004 “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… © 2004 Microsoft

3 Java and J# Using an example, let's take a tour of J# Goal?
- to demonstrate that J# can be viewed as pure Java…

4 Banking Application 2. The J# Language Summer 2004 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 Banking App Note that input files should be placed in same directory as the Banking App program; when working with Visual Studio, this is the bin\Debug sub-folder of your Visual Studio .NET solution folder. © 2004 Microsoft

5 Program design Design will consist of 4 classes:
2. The J# Language Summer 2004 Design will consist of 4 classes: App, Customer, CustomersIO, Transactions Customer represents 1 Customer n App main( ) 1 CustomersIO read(filename) : Customer[ ] write(customers, filename) 1 The above diagram is depicted using UML ("Unified Modeling Language"). Each box is a class, with fields and methods listed. The dotted arrows denote that one class is making use of another, and the numbers shown on the endpoints of the arrows denote how many instances of that class are needed. Note that when the # of instances needed is 1, this is an indicator that static methods can be used. For this reason we'll use static methods in the CustomersIO and Transactions classes. Transactions process(customers, filename) 1 © 2004 Microsoft

6 Main program Coordinates transaction processing
2. The J# Language Summer 2004 Coordinates transaction processing read customer data into array of Customer objects process transactions by updating objects 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... . © 2004 Microsoft

7 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

8 Build & run… Stub out the classes & methods, build & run
2. The J# Language Summer 2004 Stub out the classes & methods, build & run test normal execution… test throwing a java.io.IOException… We recommend developing the app in a top-down fashion, where you stub out the classes and methods as needed to get the main program to compile & run. Then, evolve program step-by-step by flushing out the classes & methods… © 2004 Microsoft

9 Customer class Represents 1 bank customer
2. The J# Language Customer firstName : String lastName : String id : int balance : double Customer(fn, ln, id, balance) toString( ) : String Summer 2004 Represents 1 bank customer minimal implementation at this point… public class Customer { public String firstName, lastName; // fields public int id; public double balance; 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; Let's keep the design very simple at this point: public fields, a constructor, and override toString() for print debugging purposes. © 2004 Microsoft

10 CustomersIO class Reads & writes the customer data File format:
2. The J# Language CustomersIO read(filename) : Customer[ ] write(customers, filename) Summer 2004 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 . For now, file will be in sorted order by lastname, first (which implies sorted by id number as well). This input file should be placed in the bin\Debug sub-folder of the BankingApp solution folder. © 2004 Microsoft

11 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; double balance; 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; } "Jim", … "Jane", …

12 Build & run… Let's test the input code… Customer[] customers;
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]);

13 CustomersIO.write( ) … Now let's implement write( )… "Jane", …
"Jim", … "Jane", … public class CustomersIO { . 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(); }

14 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

15 Transactions class Reads the bank transactions & updates the customers
2. The J# Language Transactions process(customers, filename) Summer 2004 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 . -1 This input file should be placed in the bin\Debug sub-folder of the BankingApp solution folder. © 2004 Microsoft

16 Transactions.process( )
2. The J# Language Summer 2004 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(); } For now, we're going to assume perfect input: customer ids are always valid, as well as all deposits & withdrawals. Everyone has an infinite supply of money to withdraw :-) © 2004 Microsoft

17 findCustomer( ) Performs a linear search, returning first matching customer… "Jim", … "Jane", … . 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

18 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

19 J# is Java! Program we just developed is pure Java
2. The J# Language Summer 2004 Program we just developed is pure Java compiles with both Visual Studio .NET and Sun's JDK… transactions.txt customers.txt Banking App 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 © 2004 Microsoft

20 Compiling with Java: 2. The J# Language Summer 2004 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 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 © 2004 Microsoft

21 JavaDoc comments 2. The J# Language Summer 2004 Support for JavaDoc comments provided by Visual Studio .NET Tools menu, Build Comment Web Pages… @param are supported; others are package BankingApp; /** * Main class for Banking Application. * * Author: Joe Hummel, * Date: April 2004 */ public class App { . © 2004 Microsoft

22 Summary J# is Java on the .NET platform
2. The J# Language Summer 2004 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… © 2004 Microsoft


Download ppt "Lecture 3: The J# Language"

Similar presentations


Ads by Google