HELP! * YELLOW=global GREY=local Full HTML for

GLOBAL foilset Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O

Given by Nancy McCracken at CPS616 spring 1997 on Feb 3 1997. Foils prepared 4 February 97
Abstract * Foil Index for this file Secs 79.2 See also color IMAGE

AWT Components , Actions and Layout Managers
Skip over Thread Details
Important Java Classes including Object class
I/O and File Operations
Security Concerns in Applets
URL's and Networking

Table of Contents for full HTML of Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O


1 CPS 616 Java Lectures
with Audio February 3 97
AWT Through I/O
See:
http://www.npac.syr.edu/users/gcf/cps616-97feb3

2 Abstract of CPS616-97 Lecture of February 3
3 Abstract Windowing Toolkit (AWT): Components such as buttons, textfields, etc.
4 Structure of the java.awt GUI Components - I
5 Structure of the java.awt GUI Components - II
6 Picture of the AWT Component Class and its inheritance
7 Some Simple AWT Components -- label,button
8 AWT Components -- Checkbox
9 AWT Components -- Radio Buttons , CheckboxGroup
10 Some Further AWT Components -- typical subunits of panels
11 AWT Components -- Text Fields & Areas
12 Some Further AWT Components -- Canvas, Window - I
13 Some Further AWT Components -- Canvas, Window - II
14 Actions associated with Components in AWT - I
15 Actions associated with Components in AWT - II
16 A more general way to handle events
17 Actions associated with Components in AWT - II
18 Layout of Components in a Panel
19 Description and Example of BorderLayout
20 Brief Description of Four Other LayoutManager's
21 FlowLayouts in detail
22 Hierarchical use of LayoutManagers
23 AWT - Grid Layout with Two Components
24 AWT - Adding Components to a Layout
25 AWT - Canvas Component
26 AWT - Panel Component with Buttons
27 AWT - Handling Actions from Components
28 Java Tutorial - Fall 1996
Part 4: Multithreading, useful Java classes, I/O, Networking, and the future
http://www.npac.syr.edu/projects/tutorials/Java/

29 Threads are part of the Java Language!
30 Useful Basic Java Classes
31 The Overarching Object Class
32 Determining and Testing Class of Object
33 java.lang.Object Wrappers
34 The java.lang.Math class
35 The Date class
36 The String class
37 More on Strings, and the StringBuffer class
38 ReverseString Class
39 The Vector class
40 Methods for Vectors
41 The Hashtable class
42 I/O and the Stream Zoo
43 I/O Streams
44 The Input Stream Zoo
45 FilterInputStreams
46 Networking and Web Access
47 Networking and Web Access in Java
48 Security Concerns for Applets
49 Table for Java file and network access
50 Accessing URL's in Java -- URL, showDocument
51 Accessing URL's in Java -- URLConnection
52 I/O: Reading a File known by URL
53 I/O: Setting up the URL, Layout and Thread
54 I/O: Reading the Text File

This table of Contents Abstract



HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 1 CPS 616 Java Lectures
with Audio February 3 97
AWT Through I/O
See:
http://www.npac.syr.edu/users/gcf/cps616-97feb3

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 79.2 Full HTML Index
Instructor: Nancy McCracken
teamed with Meryem Ispirli, Geoffrey Fox,
Tom Scavo, John Yip
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 2 Abstract of CPS616-97 Lecture of February 3

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 79.2 Full HTML Index
AWT Components , Actions and Layout Managers
Skip over Thread Details
Important Java Classes including Object class
I/O and File Operations
Security Concerns in Applets
URL's and Networking

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 3 Abstract Windowing Toolkit (AWT): Components such as buttons, textfields, etc.

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 74.8 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 4 Structure of the java.awt GUI Components - I

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 144 Full HTML Index
In Java, the GUI (Graphical User Interface) is built hierarchically in terms of Components -- one Component nested inside another starting with the smallest Buttons, including Menus, TextFields etc. and ending with full Window divided into Frames, MenuBars etc.
Not all useful Classes are inherited from Component. For instance Menu inherits from MenuComponent (interface) --> MenuItem --> Menu
One also needs a set of methods and classes to define the layout of the Components in a particular Panel
LayoutManager is a java.awt interface with several particular layout strategies implemented as classes under this interface
The Container class has methods to interact with LayoutManager classes

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 5 Structure of the java.awt GUI Components - II

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 149.7 Full HTML Index
In the simplest use of AWT, one could add a Button to an Applet (grandchild of Container) using in the init() method for Applet
  • Button b = new Button("Are You Feeling well");
  • add(b); // add() is a Container method
The various added Components are put in the panel by the LayoutManager using order in which they were added
A Final critical part of the AWT is the actions generated by these components which are processed by overriding the action() method in Component
  • action(Event e, Object Anyargemightliketoreturn);
We define extra events -- such as those connected with scrolling or selecting buttons to those of basic mouse/keyboard

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 6 Picture of the AWT Component Class and its inheritance

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 257.7 Full HTML Index
This is incomplete!

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 7 Some Simple AWT Components -- label,button

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 95 Full HTML Index
add(new Label("aligned left")); // default alignment
produces a text string using constructor Label of Label class
add(new Button("Grade is A"));
add(new Button("Grade is B")); // surely this is lowest grade for a course on such an easy language?

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 8 AWT Components -- Checkbox

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 141.1 Full HTML Index
Checkbox's are on-off toggles implemented as
add(new Checkbox("Red"));
add(new Checkbox("Green"));
add(new Checkbox("Blue"),null, true);
The first two are initially set to "false" as the optional third argument is not given. The last one is initially set to "true".
The state of a checkbox, i.e. whether it is checked, is given by the method, getState:
Checkbox cb = new Checkbox("Red");
add (cb);
. . .
if (cb.getState()) . . .;

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 9 AWT Components -- Radio Buttons , CheckboxGroup

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 113.7 Full HTML Index
Radiobuttons are identical to Checkbox's but grouped so that only one checkbox in a group can be on at a time. They use same class for buttons but add CheckboxGroup class
CheckboxGroup cbg = new CheckboxGroup();
cb1 = new Checkbox("Red", cbg, false));
cb2 = new Checkbox("Green", cbg, false));
cb3 = new Checkbox("Blue", cbg, true));
add(cb1); add(cb2); add(cb3);
In addition to checking the state of checkboxes in a group, the method getCurrent returns the one checkbox which is on:
  • cb = cbg.getCurrent();

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 10 Some Further AWT Components -- typical subunits of panels

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 145.4 Full HTML Index
Choice is a class that gives a menu where you choose from various items
TextField is a simple class where user can enter information into fields
TextArea is a somewhat more sophisticated text entry area which are scrollable and so useful where amount of data to be entered is unlimited
List is another child of Component that is similar in use to Choice but gives a fixed size list which can be scrolled and where you can select one or more entries
Scrollbar is a class that defines a horizontal or vertical scrollbar. Note this is distinct from scrollbars that come with TextArea and List

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 11 AWT Components -- Text Fields & Areas

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 80.6 Full HTML Index
To add a text field for display or input one line of text (in this case, 30 characters wide):
  • TextField tf = new TextField("initial text", 30);
  • add(tf);
The text which is displayed can be changed:
  • tf.setText("now show a new text");
If the user can type input into the text field, it can be obtained:
  • stringvar = tf.getText();
Or you can disallow the user to type:
  • tf.setEditable(false);
The TextArea class also has these methods, but it can display multiple lines.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 12 Some Further AWT Components -- Canvas, Window - I

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 92.1 Full HTML Index
Canvas is a simple class which are used to draw on as in artist's canvas. They cannot contain other components. This is the class on which you use the graphics methods.
Upto now, we have described how to build typical Applet panels inside browser window. There are classes that allow one to generate complete windows separately from the browser window
Window Class has subclasses Frame and Dialog
Frame("TitleofWindow"); // creates a window with memubar and given title
  • Note Frame is a Container and can thereof be defined hierarchically with components that are laid out by LayoutManagers
  • Note a Frame in Java is NOT THE SAME as a frame in Netscape 2.0 and higher

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 13 Some Further AWT Components -- Canvas, Window - II

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 51.8 Full HTML Index
A Frame has a set of classes to define MenuBars and Menus:
  • MenuBar(); // defines a menubar which can be used in a frame
  • Menu("Type"); // defines a menu which can itself have hierarchically defined submemus
Another type of separate window is the Dialog box, which is not so elaborate as a frame.
  • Dialog(Frame, String Title, boolean mustbeansweredatonceornot); // defines a dialog box
Dialog boxes are used for transient data
  • Issue warning to user or require (third argument true) user to verify some action etc.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 14 Actions associated with Components in AWT - I

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 172.8 Full HTML Index
We already discussed handling Mouse and Keyboard Events. These AWT components come with new actions which need to be handled with an action() method in your applet
Put action ( a method of class Component) in Container instance that is at lowest possible level so you can customize action to things in that Container
action(Event evt, Object arg)'s are looked for in same fashion as exceptions. Scan up Containers looking for a method of this name. Scanning stops when you find an action and that method returns true
evt.target holds the object that caused the Event
Object Arg returned depends on particular Component invoked
There are further evt.id's associated with the various peculiar Components -- see description of class Event for current detailed description.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 15 Actions associated with Components in AWT - II

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 123.8 Full HTML Index
Suppose we have a bunch of buttons in a particular Container saying Red, Green, Blue as we illustrated earlier. Then an action method would be
  • public boolean action(Event evt, Object arg) {
  • if( evt.target instanceof Button)
    • changeColor((String) arg);
    • // where changeColor is a method supplied by the user
  • return true; // tell runtime that this event fully processed
  • }
  • void changeColor(String bname) { // suitable user method
  • if( bname.equals("Red")) setBackground(Color.red);
    • else if (bname.equals("Green")) setBackground(Color.green);
    • else if (bname.equals("Blue")) setBackground(Color.blue);
    • else setBackground(Color.pink); // our favorite color
  • }

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 16 A more general way to handle events

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 131 Full HTML Index
In general, declare names for all components such as labels, buttons and textfields, and use the variable evt.target to distinguish which component generated the action.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 17 Actions associated with Components in AWT - II

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 34.5 Full HTML Index
Suppose we have a bunch of buttons in a particular Container saying Red, Green, Blue as we illustrated earlier. Then an action method would be
  • public boolean action(Event evt, Object arg) {
  • if( evt.target instanceof Button)
    • changeColor((String) arg);
    • // where changeColor is a method supplied by the user
  • return true; // tell runtime that this event fully processed
  • }
  • void changeColor(String bname) { // suitable user method
  • if( bname.equals("Red")) setBackground(Color.red);
    • else if (bname.equals("Green")) setBackground(Color.green);
    • else if (bname.equals("Blue")) setBackground(Color.blue);
    • else setBackground(Color.pink); // our favorite color
  • }

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 18 Layout of Components in a Panel

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 118 Full HTML Index
The various panels in a container are laid out separately in terms of their subcomponents
One can lay components out "by hand" with positioning in pixel space
However this is very difficult to make machine independent. Thus one tends to use general strategies which are embodied in 5 LayoutMangers which all implement the LayoutManager Interface. One can expect further custom LayoutManager's to become available on the Web
setLayout(new FlowLayout()); // creates a basic flow layout in your panel -- actually unnecessary as default
Other available LayoutManager's are GridLayout(), BorderLayout() (default for Frame's), CardLayout() (Used for dynamic layouts) and GridBagLayout() (the most flexible)

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 19 Description and Example of BorderLayout

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 133.9 Full HTML Index
BorderLayout has five cells called North South East West Center and components are assigned to these cells with the add method. As used in a window, one would naturally use:
  • add("North", new TextField("Title",50));
  • add("South", new TextField("Usuallyreservedforstatusmessage",50));
Remember this is default for a Frame Container

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 20 Brief Description of Four Other LayoutManager's

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 216 Full HTML Index
FlowLayout is a one dimensional layout where components are "flowed" into panel in order they were defined. When a row is full up, it is wrapped onto next row
GridLayout is a two dimensional layout where you define a N by M set of cells and again the components are assigned sequentially to cells starting at top left hand corner -- one component is in each cell
GridBagLayout uses a new class GridBagConstraints to customize positioning of individual components in one or more cells
CardLayout lays out in time not space and each card (Displayed at one time) can be laid out with one of spatial layout schemes above

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 21 FlowLayouts in detail

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 44.6 Full HTML Index
This simple layout manager starts putting components in the window from the top left, continues across the row until there is no more room, starts the next row, and so on. The components can be aligned, and space between them given by the arguments hgap and vgap.
setLayout(new FlowLayout(FlowLayout.LEFT, 5, 1);
setLayout(new FlowLayout(FlowLayout.CENTER);
setLayout(new FlowLayout(FlowLayout.RIGHT);

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 22 Hierarchical use of LayoutManagers

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 149.7 Full HTML Index
Layout's can be made very sophisticated using an hierarchical approach
setLayout(new GridLayout(1,3,10,5));
  • // Number of cells in y, Number in x, Horizontal gap, Vertical Gap
subpanel1 = new MysubpanelClass(); // Add arguments to make subpanel1 special
subpanel2 = new MysubpanelClass();
add(Some Simple Component such as a Button);
add(subpanel1);
add(subpanel2);
. . . . .
Class MysubpanelClass extends panel { // has constructor
MysubpanelClass() { // that includes another layout such as
setLayout(new GridLayout(2,2,5,5); // etc.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 23 AWT - Grid Layout with Two Components

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 80.6 Full HTML Index
A simple example of using the Abstract Windowing Toolkit divides the applet space into two components. One is a Canvas for drawing graphics or images; one is a Panel which has three buttons. We choose to draw the Canvas within the Applet and to create the Panel in another class.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 24 AWT - Adding Components to a Layout

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 108 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 25 AWT - Canvas Component

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 93.6 Full HTML Index
The initialization of the Canvas was done in the applet init method; the other method for painting the Canvas simply changes the background color. About the only thing a Canvas can do is explicit drawing and graphics.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 26 AWT - Panel Component with Buttons

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 126.7 Full HTML Index
This class does not need to be a subclass of Applet - like all components except Canvas, you don't need to paint, just add components and handle events and actions.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 27 AWT - Handling Actions from Components

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 87.8 Full HTML Index
The Button component generates an action whenever the button is clicked on. In this example, we call the method that changes the color of the Canvas in another part of the layout.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 28 Java Tutorial - Fall 1996
Part 4: Multithreading, useful Java classes, I/O, Networking, and the future
http://www.npac.syr.edu/projects/tutorials/Java/

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 34.5 Full HTML Index
Instructors: Geoffrey Fox ,
Nancy McCracken
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 29 Threads are part of the Java Language!

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 48.9 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 30 Useful Basic Java Classes

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 25.9 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 31 The Overarching Object Class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 74.8 Full HTML Index
public class Object is the root of the Class hierarchy. Every java class has Object as its ultimate parent and so any object (object with a small o is any instance of any class) can use methods of Object
Methods of Object (used as object.Method()) include:
clone() Creates a clone of the object
equals(another object) compares two objects returning boolean answer
getClass() returns a descriptor of type Class (a child of Object) defining class of object
toString() returns a String which represents "value" of object. It is expected that each subclass will override this method
wait() in various forms is used to cause threads to wait
finalize() contains code to perform when object is deleted by system (I.e. garbage collected)

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 32 Determining and Testing Class of Object

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 20.1 Full HTML Index
Suppose an object is called obj , then we get Class of obj by
Class itsclass = obj.getClass(); // and the name of this class by:
String name = itsclass.getName();
One can also use instanceof in following fashion
"foo" instanceof String // is evaluated to true but
mPoint pt = new mPoint(x,y);
pt instanceof String // is evaluated to false

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 33 java.lang.Object Wrappers

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 37.4 Full HTML Index
"types" such as int char float etc. are NOT classes
Thus one cannot use methods such as
int var;
var.toString
Thus ALL types have associated wrappers used like
Character wrappedchar = new Character(char);
now wrappedchar has lots of good methods you can use such as:
wrappedchar.equals(anotherobject);
wrappedchar.toString();
There are also static (class) functions such as:
toUppercase(char ch);
isUpperCase(char ch);

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 34 The java.lang.Math class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 30.2 Full HTML Index
This class provides the standard mathematical functions, using types int, long, float and double as appropriate.
It is a static class, meaning that you only use the methods, never creating objects from this class.
The methods include
  • IEEEremainder, abs, ceil, cos, exp, floor, log, max, min, pow, random, sin, sqrt, and other trig functions.
  • The random number generator is a linear congruential generator, which is fast but not random enough for many scientific applications.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 35 The Date class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 8.6 Full HTML Index
This class exists to provide an implementation of "date" structures. As is typical of data encapsulation classes, it has methods to create dates, obtain and set the parts of dates, and convert dates to other data types.
The constructor either creates today's date or any other day (and time) that you provide:
  • Date today = new Date();
  • Date ancient = new Date(999,12,31); /* Dec. 31, 999 */
  • Date deadline = new Date(1996,12,31,23,59,59)
    • /* Dec.31,1996 at 23:59:59 */

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 36 The String class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 69.1 Full HTML Index
Strings are fixed length collections of Unicode characters stored as an instance of the class.
Most commonly, a string is created from a string literal or by using the constructor on an array of characters:
  • String greeting = "Hello";
or
  • char[] bunch = {'H','e','l','l','o'};
  • String greeting = new String(bunch);
Once created, individual characters of a string cannot be changed in place. This example shows using the methods of indexing, catenation (+), and substring to create a new string with one character changed:
  • String test = "Chicken soup with rice";
  • int a = test.indexOf('w');
  • String newtest = substring(1,a-1)+"is n"+substring(a+5);
  • /* getting "Chicken soup is nice" */

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 37 More on Strings, and the StringBuffer class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 44.6 Full HTML Index
String comparison can be done with the methods equals and equalsIgnoreCase. Note that == tests if two strings are the same string instance, while equals tests if two different strings have the same characters.
Other methods include length, CharAt and toString.
The StringBuffer class has mutable strings, but is created with a fixed maximum size. It has methods such as append to extend the length of the string.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 38 ReverseString Class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 11.5 Full HTML Index
This class returns object of class String which reverses order of characters in input object source which is also an instance of class String
class ReverseString {
  • public static String reverseIt(String source) {
    • int i, len= source.length();
    • StringBuffer dest = new StringBuffer(len);
    • for( i= (len-1); i >=0 ; i--) {
    • dest.append(source.charAt(i));
    • }
    • return dest.toString();
    • }
  • }

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 39 The Vector class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 95 Full HTML Index
In Java, while you can give the size of arrays at run time, you cannot dynamically change the size of an array during the computation. The vector class provides a data structure with this property - the restriction is that all of the elements must be of type Object.
  • It is usually simple to insert an element of any type and Java will convert it to an Object. But when you extract an element, you must explicitly cast it to convert it back to the type you want.
A vector is created with an "initial capacity" and a "capacity increment". It always starts with 0 elements. As you add elements, if the initial capacity is exceeded, then more memory is automatically allocated in the size of the capacity increment. The default is an initial capacity of 10 and an increment which doubles each time.
  • Vector shoes = new Vector();
  • Vector orders = new Vector(100, 10);

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 40 Methods for Vectors

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 12.9 Full HTML Index
Elements are created with the addElement method:
  • Order missouri = new Order();
  • orders.addElement(missouri);
The object missouri of type Order is automatically converted to an Object in vector instance orders defined on previous foil.
There are methods for indexing vectors. Like arrays, the indexing is zero-based.
  • x = (Typeofx) v.elementAt(i);
  • v.setElementAt(x,i);
The length of the Vector can be obtained:
  • int size = v.size;

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 41 The Hashtable class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 41.7 Full HTML Index
This class is similar to the Perl associative array (or hash array with {} brackets). It can store a set of key and value pairs, neither of which can be null.
  • Hashtable staff = new Hashtable();
  • Employee harry = new Employee("Harry Hacker");
  • staff.put("987-98-9996", harry);
Values are retrieved by indexing with a key. Like Vectors, Hashtables only store things of type Object and you must cast the result.
  • steve = (Employee) staff.get("149-26-7355");
If there was no entry, a null is returned.
Performance of the Hashtable can also be affected by giving an initialCapacity and a loadFactor for reallocation.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 42 I/O and the Stream Zoo

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 17.2 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 43 I/O Streams

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 131 Full HTML Index
Streams are an abstraction of a sequence of bytes.
The sources and destinations of these sequences can be
  • files
  • network connections
  • blocks of memory
That is, all of these types of I/O are treated in the same way.
The most basic I/O streams are InputStream and OutputStream. These classes have methods that can read or write a byte from the stream:
  • int read ();
  • void write (byte);
  • also skip(), available(), close()
There are also methods for reading and writing arrays of bytes.
These methods all "block" on the read or write during the data transfer.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 44 The Input Stream Zoo

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 76.3 Full HTML Index
The subclasses of InputStream offer more methods that can read the stream in a more structured way or provide other functionality.
Standard UNIX file I/O methods are available
FileInputStream s = new FileInputStream("/usr/gcf/greatidea");

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 45 FilterInputStreams

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 178.5 Full HTML Index
There is a clever class called FilterInputStream which can be used to add value to a raw InputStream. You can define your own filters but important ones provided are:
  • BufferedInputStream -- establishs an intermediate buffer to service stream
  • DataInputStream -- allows one to address stream in terms of higher level constructs -- namely read a line, read a long integer etc.
  • LineNumberInputStream -- adds line numbers to a stream
  • PushbackInputStream -- allows one to "unread" a character and put it back on the input stream
These streams can be constructed from each other or InputStream to give added functionality. If "is" is an InputStream, then it can be converted to a buffered input stream with methods from datainputstream:
DataInputStream data = new DataInputStream(new BufferedInputSream(is));

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 46 Networking and Web Access

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 23 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 47 Networking and Web Access in Java

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 80.6 Full HTML Index
This area will evolve rapidly as existing I/O systems get linked to Java with special classes such as those needed to link MPI (HPCC Message Passing) or Nexus (Well known distributed memory thread package)
One can establish Web Connection with URL class and access documents there
One can set up a more general URLConnection for more general input/output operations through Web(HTTP) protocol
One can set up a Socket link which is permanent rather than on again off again Web client-server interaction
One can send messages and so transfer information between different Applets

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 48 Security Concerns for Applets

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 76.3 Full HTML Index
One aspect of Java security is language restrictions designed not to let a Java applet or application access memory on the machine outside of its own space.
Applets have additional restrictions:
  • they can never run a local executable program;
  • they cannot communicate with any host other than the server from which they were downloaded (the originating host);
  • they cannot read or write to the local computer's file system, except through the browser mechanism;
  • they cannot find out information about the local computer (see table on next slide for details).
As of summer 1996, no known applets have seriously broken security to steal client information or trash the local disk. Exceptions:
  • applets have been written to use up arbitrary amounts of client cpu.
  • applets with native code can trash the local disk. So far, native code is disallowed on publicly released browsers.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 49 Table for Java file and network access

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 12.9 Full HTML Index
This table shows what Java programs can do in the following four cases:
  • NL - Netscape loading a URL for applet
  • NF - Netscape loading a local file for applet
  • AV - Applet viewer
  • JA - Java application (not an applet)
    • NL NF AV JA
  • read local file no no yes yes
  • write local file no no yes yes
  • get file information no no yes yes
  • delete file no no no yes
  • run another program no no yes yes
  • read the user.name proper no yes yes yes
  • connect to network port on server yes yes yes yes
  • connect to network port on other host no yes yes yes
  • load Java library no yes yes yes
  • call exit no no yes yes
  • create a pop-up window with warning yes yes yes

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 50 Accessing URL's in Java -- URL, showDocument

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 87.8 Full HTML Index
First you set the URL in various ways using something like
String npacurl = "http://www.npac.syr.edu/index.html";
try {theinputurl=newURL(npacurl);} // URL class is in java.net
catch ( MalformedURLException e) {
  • System.out.println("Bad URL: " + npacurl); }
where you are meant to test to see if URL is legal!
The simplest thing to do now is to see this page with
getAppletContext().showDocument(theinputurl, Frame targetdisplayframe);

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 51 Accessing URL's in Java -- URLConnection

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * See also color IMAGE
Secs 77.7 Full HTML Index
More interesting is to access a URL and process the information there!
This is done using streams:
There are methods in class URL --
InputStream in = instanceofURL.openStream(); // opens an InputStream associated with given url
More general is instanceofURL.openConnection() establishes a general connection and returns an instance of class URLConnection
  • This provides most general HTTP connectivity and indeed has some advantages over sockets as these are subject to special security restrictions in Netscape's current implementation
Note that one can connect not just to HTML files but also to CGI scripts i.e. programs at server and so obtain flexible connectivity

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 52 I/O: Reading a File known by URL

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 51.8 Full HTML Index
This applet creates a text area in the window, reads text from a file known by its URL, and displays the text in the text area. It uses the class URL from java.net, to set up a socket to the server, and the class InputStream from java.io.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 53 I/O: Setting up the URL, Layout and Thread

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 76.3 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 4 February 97

Foil 54 I/O: Reading the Text File

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. * Critical Information in IMAGE
Secs 218.8 Full HTML Index
The openStream method opens the socket connection and returns an object of type InputStream. We convert this to a BufferedInputStream for performance and then to DataInputStream, where we have methods to read lines or characters of data.

Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Feb 16 1997