Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Programming Language Technologies and Paradigm - Basic Java | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Hicks; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-vef
koofers-user-vef 🇺🇸

10 documents

1 / 22

Toggle sidebar

Related documents


Partial preview of the text

Download Programming Language Technologies and Paradigm - Basic Java | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity! CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 1 CMSC433, Fall 2002 Programming Language Technology and Paradigms Basic Java Michael Hicks Sep 12, 2002 CMCS 433, Fall 2002 - Michael Hicks 59 Exceptions • On an error condition, we throw an exception • At some point up the call chain, the exception is caught and the error is handled • Separates normal from error-handling code • A form of non-local control-flow – Like goto, but structured CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 2 CMCS 433, Fall 2002 - Michael Hicks 60 Throwing an Exception • Create a new object of the class Exception, and throw it if (i > 0 && i < a.length ) { return (a[I]) } else throw new ArrayIndexOutOfBounds(); • Exceptions thrown are part of return type – when overriding a method in a superclass – can’t throw anything that would surprise a superclass object CMCS 433, Fall 2002 - Michael Hicks 61 Method throws declarations • A method declares the exceptions it might throw – public void openNext() throws UnknownHostException, EmptyStackException { … } • Must declare any exception the method might throw – unless it is caught in the method – includes exceptions thrown by called methods – certain built-in exceptions excluded CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 5 Java Libraries CMCS 433, Fall 2002 - Michael Hicks 67 You should familiarize yourself • Packages – java.lang – java.util – java.net – java.io • Read the documentation on line CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 6 I/O and Network Libraries CMCS 433, Fall 2002 - Michael Hicks 69 • Raw communication takes place using streams • Java also provides readers and writers • Applies to files, network connections, strings, etc. I/O streams InStream OutStream Process CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 7 CMCS 433, Fall 2002 - Michael Hicks 70 I/O Classes • OutputStream – byte stream going out • Writer – character stream going out • InputStream – byte stream coming in • Reader – character stream coming in CMCS 433, Fall 2002 - Michael Hicks 71 OutputStream - bytes • Example classes – ByteArrayOutputStream – goes to byte [] – FileOutputStream – goes to file • Wrappers – wrapped around OutputStream – BufferedOutputStream – ObjectOutputStream – serialization of object graph CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 10 CMCS 433, Fall 2002 - Michael Hicks 76 Input (JDK 1.1 and higher) • Wrap System.in in an InputStreamReader – converts from bytes to characters • Wrap the result in a BufferedReader – makes input operations efficient – supports readline() interface • readline() returns a string – returns null if at EOF CMCS 433, Fall 2002 - Michael Hicks 77 Example Echo Application import java.io.*; public class Echo { public static void main(String [] args) { String s; BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); int i = 1; try { while((s = in.readLine()) != null) System.out.println((i++) + “: “ + s); } catch(IOException e) { System.out.println(e); } } } CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 11 CMCS 433, Fall 2002 - Michael Hicks 78 Java Networking • class Socket – Client-side connections to servers • class ServerSocket – Server-side “listen” socket – Awaits and responds to connection requests CMCS 433, Fall 2002 - Michael Hicks 79 Example Client/Server ServerSocket s = new ServerSocket(5001); Socket conn = s.accept(); InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); Socket conn = new Socket (5001); InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); Client code Server code server client CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 12 CMCS 433, Fall 2002 - Michael Hicks 80 Example Client/Server ServerSocket s = new ServerSocket(5001); Socket conn = s.accept(); InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); Socket conn = new Socket (5001); InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); Client code Server code server client CMCS 433, Fall 2002 - Michael Hicks 81 Example Client/Server ServerSocket s = new ServerSocket(5001); Socket conn = s.accept(); InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); Socket conn = new Socket (5001); InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); Client code Server code server client ? CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 15 Utility Libraries CMCS 433, Fall 2002 - Michael Hicks 87 java.util • Lots of stuff – Vector – Dictionaries – Enumerations and Bitsets – Container classes • We will focus on Container classes CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 16 CMCS 433, Fall 2002 - Michael Hicks 88 Other libraries • java.lang.Math – abstract final class – only static members – includes constants e and π – includes static methods for trig, exponentiation, min, max, … • java.text – text formatting tools • class MessageFormat provides printf/scanf functionality – lots of facilities for internationalization CMCS 433, Fall 2002 - Michael Hicks 89 Java Container Classes • A unified architecture for representing and manipulating collections of objects • Container classes contain three things: – Interfaces: abstract data types representing collections of objects – Implementations: concrete implementations of the collection interfaces – Algorithms: methods that perform computations on objects that implement collection interfaces CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 17 CMCS 433, Fall 2002 - Michael Hicks 90 Container class hierarchy CMCS 433, Fall 2002 - Michael Hicks 91 Collection Classes • Collections contain groups of objects (elements) • Collection interface is not implemented in Java. Subinterfaces implemented – Set: unordered, can’t contain duplicate elements • Hashset – unordered, no duplicates • TreeSet - ordered, no duplicates – List: ordered, can contain duplicate elements • LinkedList – unordered, dynamic size, add/delete quick • ArrayList – unordered, dynamic size, random access CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 20 CMCS 433, Fall 2002 - Michael Hicks 96 Example1 import java.util.*; import java.awt.*; class MyPoint extends java.awt.Point implements Comparable { MyPoint(int x, int y) {super(x,y);} public int compareTo(Object o) { MyPoint p = (MyPoint)o; double d1 = Math.sqrt(x*x + y*y); double d2 = Math.sqrt(p.x*p.x + p.y*p.y); if (d1 < d2) {return -1;} else if (d2 < d1) {return 1;} return 0; } } class Sort3 { public static void main(String[] args) { Random rnd = new Random(); MyPoint[] points = new MyPoint[10]; for (int i=0; i<points.length; i++) { points[i] = new MyPoint (rnd.nextInt(100),rnd.nextInt(100)); System.out.println(points[i]); } System.out.println("-----------"); Arrays.sort(points); //Print the points for (int i=0; i<points.length; i++){ System.out.println(points[i]); } } } CMCS 433, Fall 2002 - Michael Hicks 97 Example2 import java.util.*; import java.awt.*; class MyPoint extends java.awt.Point implements Comparable { MyPoint(int x, int y) { super(x, y); } public int compareTo(Object o) { MyPoint p = (MyPoint)o; return x - p.x; } } class Sort2 { public static void main(String[] args) { Random rnd = new Random(); MyPoint[] points = new MyPoint[10]; for (int i=0; i<points.length; i++) { points[i] = new MyPoint (rnd.nextInt(100), rnd.nextInt(100)); System.out.println(points[i]); } System.out.println("-----------"); Arrays.sort(points); //Print the points for (int i=0; i<points.length; i++){ System.out.println(points[i]); } } } CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter) 21 CMCS 433, Fall 2002 - Michael Hicks 98 Output Sort2 …….. // after sort MyPoint[x=1,y=95] MyPoint[x=2,y=16] MyPoint[x=3,y=26] MyPoint[x=12,y=95] MyPoint[x=22,y=55] MyPoint[x=30,y=73] MyPoint[x=31,y=42] MyPoint[x=66,y=33] MyPoint[x=70,y=33] MyPoint[x=80,y=31] Sort3…….. // after sort MyPoint[x=2,y=0] MyPoint[x=0,y=15] MyPoint[x=18,y=4] MyPoint[x=39,y=13] MyPoint[x=39,y=19] MyPoint[x=42,y=23] MyPoint[x=65,y=5] MyPoint[x=38,y=74] MyPoint[x=80,y=40] MyPoint[x=87,y=62] This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.