Академический Документы
Профессиональный Документы
Культура Документы
The J# Language
Objectives
J# brings the Java programming language (1.4) to the .NET platform, providing full access to the .NET Framework
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
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
Banking App
3-4 4
Program design
Design will consist of 4 classes: App, Customer, CustomersIO, Transactions
n
App main( )
CustomersIO
1
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... . . . J# in Visual Introducing CS using .NET Microsoft Studio .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
3-7 7
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; }
} J# in Visual Introducing CS using .NET Microsoft Studio .NET 3-9 9
CustomersIO class
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 . . .
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;
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; }
"Jane", "Jim",
3-11 11
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]);
3-12 12
CustomersIO.write( )
Now let's implement write( )
public class CustomersIO { . . .
"Jane", "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(); }
3-13 13
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
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(); }
3-16 16
findCustomer( )
Performs a linear search, returning first matching customer
"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 "Jim",
3-17 17
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
Banking App
3-19 19
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
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 { . . .
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.
3-22 22