of 22
8/2/2019 03 Language
1/22
Lecture 3:
The J# Language
8/2/2019 03 Language
2/22
2Microsoft
Introducing CS using .NET
J# in Visual Studio .NET 3-2
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
8/2/2019 03 Language
3/22
3Microsoft
Introducing CS using .NET
J# in Visual Studio .NET 3-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
8/2/2019 03 Language
4/22
4Microsoft
Introducing CS using .NET
J# in Visual Studio .NET 3-4
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 storedin "customers.txt"
transactions.txt
customers.txt Banking App
8/2/2019 03 Language
5/22
5Microsoft
Introducing CS using .NET
J# in Visual Studio .NET 3-5
Program design
Design will consist of 4 classes:
App, Customer, CustomersIO, Transactions
App
main( ) CustomersIO
read(filename) : Customer[ ]write(customers, filename)
Transactions
process(customers, filename)
Customer
represents 1 Customer1
n
1
1
8/2/2019 03 Language
6/22
6Microsoft
Introducing CS using .NET
J# in Visual Studio .NET 3-6
Main program
Coordinates transaction processing
1. read customer data into array of Customerobjects
2. process transactions by updating objects
3. write objects back to file when done
public classApp
{
public static voidmain(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......
8/2/2019 03 Language
7/22
7MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-7
Exception handling
Main program should deal with possibility of exceptions:
public classApp
{
public static voidmain(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/2/2019 03 Language
8/22
8MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-8
Build & run
Stub out the classes & methods,build & run
test normal execution
test throwing ajava.io.IOException
8/2/2019 03 Language
9/22
9MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-9
Customer class
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;
}
}
Customer
firstName : StringlastName : Stringid : intbalance : double
Customer(fn, ln, id, balance)toString( ) : String
8/2/2019 03 Language
10/22
10MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-10
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
CustomersIO
read(filename) : Customer[ ]write(customers, filename)
7
Jim
Bag
123
500.0
Jane
Doe456
500.0...
8/2/2019 03 Language
11/22
11MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-11
CustomersIO.read( )
First implementread( ), 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",
8/2/2019 03 Language
12/22
12MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-12
public classApp
{
public static voidmain(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]);
Build & run
Let's test the input code
8/2/2019 03 Language
13/22
13MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-13
CustomersIO.write( )
Now let's implement write( )
public class CustomersIO
{...
public static voidwrite(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();
}
"Jane",
"Jim",
8/2/2019 03 Language
14/22
14MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-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
8/2/2019 03 Language
15/22
15MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-15
Transactions class
Reads the bank transactions & updates the customers
File format:
customer id, Deposit or Withdraw, then amount
last transaction is followed by -1
Transactions
process(customers, filename)
123
D
100.0
456
W
100.0
..
.
-1
8/2/2019 03 Language
16/22
16MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-16
Transactions.process( )
Read Tx, find customer, update customer, repeat
public class Transactions
{
public static voidprocess(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();
}
8/2/2019 03 Language
17/22
17MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-17
findCustomer( )
Performs a linear search, returning first matching customer
.
.
.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
"Jane",
"Jim",
8/2/2019 03 Language
18/22
18MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-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
8/2/2019 03 Language
19/22
19MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-19
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
8/2/2019 03 Language
20/22
20MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-20
Compiling with Java:
If you have Sun's JDK installed, you can compile and run theapp we just built:
rename .jsl files to .java
javac *.java
create sub-directory called BankingApp (since files are part ofthe package BankingApp)
move .class files into BankingApp sub-directory
copy "customers.txt" and "transactions.txt" into currentdirectory (not the BankingApp sub-directory)
java BankingApp/App
8/2/2019 03 Language
21/22
21MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-21
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 classApp
{
.
.
.
8/2/2019 03 Language
22/22
22MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 3-22
Summary
J# is Java on the .NET platform
at least at the level of Java 1.4, with a subset of the classlibraries.
eventually we'll see what else J# can do