Lecture 3: The J# Language

Slides:



Advertisements
Similar presentations
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Advertisements

April 20023CSG11 Electronic Commerce Java (1) John Wordsworth Department of Computer Science The University of Reading Room 129,
TA: Nouf Al-Harbi NoufNaief.net :::
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Chapter 22 Sets and Maps. Chapter Scope The Java API set and map collections Using sets and maps to solve problems How the Java API implements sets and.
Lecture 9: Debugging & Testing. 9-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Unfortunately, errors are a part of life. Some.
Lecture 2: An Introduction to J# and.NET. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 2-2 Objectives “Microsoft.NET is based on the.NET.
Java Programming Review (Part I) Enterprise Systems Programming.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
1 Web Based Programming Section 6 James King 12 August 2003.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
Writing Classes (Chapter 4)
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 2: Working with Visual.
Java Classes Using Java Classes Introduction to UML.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Chapter 13 Sets and Maps Modified. Chapter Scope The Java API set and map collections Using sets and maps to solve problems How the Java API implements.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Arrays and Objects CIS 362. The familiar Employee class.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 2: Working with Visual.
Page 1 – Autumn 2009Steffen Vissing Andersen SDJ I1, Autumn 2009 Agenda: Java API Documentation Code Documenting (in javadoc format) Debugging.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Creating a Java Application and Applet
Chapter 3 Implementing Classes
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Electronic Commerce Java (1)
More on Arrays Review of Arrays of ints, doubles, chars
Slides by Evan Gallagher
Information and Computer Sciences University of Hawaii, Manoa
The need for Programming Languages
More Sophisticated Behavior
Lecture 3 John Woodward.
Lecture 14 Throwing Custom Exceptions
Chapter 10 – Exception Handling
Java Course Review.
Coding Defensively Coding Defensively
Creating and Modifying Text part 2
CS Week 14 Jim Williams, PhD.
Introduction to javadoc
Coding Standard & Javadoc
Java Programming Language
How to Run a Java Program
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
slides created by Ethan Apter
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Introduction to javadoc
Classes CS 21a: Introduction to Computing I
Java Looking at our first console application in Eclipse
Exceptions.
slides created by Ethan Apter
Exceptions.
IS 135 Business Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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", …

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]);

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(); }

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

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

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

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

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

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

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

JavaDoc comments 2. The J# Language Summer 2004 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 { . © 2004 Microsoft

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