Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User...

39
Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming

Transcript of Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User...

Page 1: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 1

Review

Why Java ?Application and AppletUser InterfaceException Object-Oriented Programming

Page 2: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 2

What is Java

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language. (Sun Microsystems, Inc)

Page 3: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 3

Java consists of three parts:the Java programming languagethe Java Virtual Machinethe Java platform

The Java programming language is the language in which Java applications (including applets,servlets, and JavaBeans) are written.When a Java program is compiled, it is converted to byte codes that are the portable machine language of a CPU architecture known as the Java Virtual Machine (or JVM).

What is Java

Page 4: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 4

What is Java

The JVM can be implemented directly in hardware (a Java chip), but it is usually implemented in the form of a software program (an interpreter) that interprets and executes byte codes.The Java platform is distinct from both the Java language and Java VM. The Java platform is the predefined set of Java classes that exist on every Java installation. These classes are available for use by all Java programs.The Java platform is also referred to as the Java runtime environment or the core Java APIs. The Java platform can be extended with optional standard extensions.

Page 5: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 5

Why Java

The Java language provides a powerful addition to the tools that programmers have at their disposal.

Java makes programming easier because it is object-oriented and has automatic garbage collection.

In addition, because compiled Java code is architecture-neutral, Java applications are ideal for a diverse environment like the Internet.

JavaBeans, support for component based software engineering.

Page 6: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 6

Applicationclass LessonTwoD {

String text;

//Constructor LessonTwoD(){ text = "I'm a Simple Program"; }

//Accessor method String getText(){ return text; }

public static void main(String[] args){ LessonTwoD progInst = new LessonTwoD(); String retrievedText = progInst.getText(); System.out.println(retrievedText); }}

Page 7: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 7

Application Structure and Elements

Page 8: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 8

Constructors

Classes have a special method called a constructor that is called when a class instance is created.

The class constructor always has the same name as the class and no return type.

If you do not write your own constructor, the compiler adds an empty constructor, which calls the no-arguments constructor of its parent class.

The empty constructor is called the default constructor.

The default constructor initializes all non-initialized fields and variables to zero.

Page 9: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 9

Applet

Page 10: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 10

Applet (code)

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Color;

public class SimpleApplet extends Applet{

String text = "I'm a simple applet";

public void init() {

text = "I'm a simple applet";

setBackground(Color.cyan);

}

Page 11: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 11

Applet (code)

public void start() { System.out.println("starting..."); }

public void stop() { System.out.println("stopping..."); }

public void destroy() { System.out.println("preparing to unload..."); }

Page 12: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 12

Applet (code)

public void paint(Graphics g){

System.out.println("Paint");

g.setColor(Color.blue);

g.drawRect(0, 0,

getSize().width -1,

getSize().height -1);

g.setColor(Color.red);

g.drawString(text, 15, 25);

}

}

Page 13: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 13

Run the Applet

To see the applet in action, you need an HTML file with the Applet tag as follows:

<HTML>

<BODY>

<APPLET CODE=SimpleApplet.class WIDTH=200 HEIGHT=100>

</APPLET>

</BODY>

</HTML>

Page 14: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 14

Applet Structure and Elements

The Java API Applet class provides what you need to design the appearance and manage the behavior of an applet. This class provides a graphical user interface (GUI) component called a Panel and a number of methods. The applet's appearance is created by drawing onto the Panel or by attaching other GUI components such as push buttons, scrollbars, or text areas to the Panel. To create an applet, you extend (or subclass) the Applet class and implement the appearance and behavior you want.

Page 15: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 15

Extending a Class

Most classes of any complexity extend other classes.

To extend another class means to write a new class that can use the fields and methods defined in the class being extended.

The class being extended is the parent class, and the class doing the extending is the child class. Another way to say this is the child class inherits the fields and methods of its parent or chain of parents.

Child classes either call or override inherited methods. This is called single inheritance.

Page 16: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 16

Applet : Extending a Class

The SimpleApplet class extends Applet class, which extends the Panel class, which extends the Container class. The Container class extends Object, which is the parent of all Java API classes.

Page 17: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 17

SimpleApplet

The Applet class provides the init, start, stop, destroy, and paint methods you saw in the example applet. The SimpleApplet class overrides these methods to do what the SimpleApplet class needs them to do. The Applet class provides no functionality for these methods.

However, the Applet class does provide functionality for the setBackground method,which is called in the init method. The call to setBackground is an example of calling a method inherited from a parent class in contrast to overriding a method inherited from a parent class.

Page 18: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 18

Why ?

You might wonder why the Java language provides methods without implementations.

?

It is to provide conventions for everyone to use for consistency across Java APIs. If everyone wrote their own method to start an applet, for example, but gave it a different name such as begin or go, the applet code would not be interoperable with other programs and browsers, or portable across multiple platforms. For example, Netscape and Internet Explorer know how to look for the init and start methods.

Page 19: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 19

Graphical User Interface (Swing)

In contrast to the applet where the user interface is attached to a panel object nested in a top-level browser, the Project Swing application in this lesson attaches its user interface to a panel object nested in a top-level frame object. A frame object is a top-level window that provides a title, banner, and methods to manage the appearance and behavior of the window.

Page 20: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 20

Layout ManagerBorder LayoutCardLayoutFlowLayoutGridBagLayout…

//Create panel panel = new JPanel();//Specify layout manager and background color panel.setLayout(new BorderLayout(1,1)); panel.setBackground(Color.white);//Add label and button to panel getContentPane().add(panel); panel.add(BorderLayout.CENTER, text); panel.add(BorderLayout.SOUTH, button);

Page 21: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 21

Action

In your code, you implement the actionPerformed method to take the appropriate action based on which button is clicked.

button = new JButton("Click Me");

//Add button as an event listener

button.addActionListener(this);

Page 22: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 22

Listener

The component classes have the appropriate add methods to add action listeners to them. In the code the JButton class has an addActionListener method. The parameter passed to addActionListener is this, which means the SwingUI action listener is added to the button so button-generated actions are passed to the actionPerformed method in the SwingUI object.

Page 23: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 23

Event HandlingThe actionPerformed method is passed an event object that represents the action event that occurred. Next, it uses an if statement to find out which component had the event, and takes action according to its findings.

public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if (_clickMeMode) { text.setText("Button Clicked"); button.setText("Click Again"); _clickMeMode = false; } else { text.setText("I'm a Simple Program"); button.setText("Click Me"); _clickMeMode = true; } }

Page 24: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 24

Exception

An exception is a class that descends from either java.lang.Exception or java.lang.RuntimeException that defines mild error conditions your program might encounter. Rather than letting the program terminate, you can write code to handle exceptions and continue program execution.

Page 25: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 25

Exception (Terminology)

Exception is a signal that indicates that some sort of exceptional condition (such as an error) has occurredTo throw an exception is to signal an exceptional condition.To catch an exception is to handle it – to take whatever actions are necessary to recover from it.

Page 26: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 26

public static int c(int i) throws MyException, MyOtherException {

switch (i) {

case 0: // processing resumes at point 1 above

throw new MyException("input too low");

case 1: // processing resumes at point 1 above

throw new MySubException("input still too low");

case 99:// processing resumes at point 2 above

throw new MyOtherException("input too high");

default:

return i*i;

}

}

Page 27: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 27

Exception Handling

tryThe try clause simply establishes a block of code that is to have its exceptions and abnormal exists (through break, continue, return, or exception propagation)

catchWhen an exception occurs, the first catch clause that has an argument of the appropriate type is invoked. The type of the argument must match the type of the exception object, or it must be a superclass of the exception.

Page 28: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 28

Exception Handling

FinallyThe finally clause is generally used to clean up (close files, release resources, etc) after the try clause. The code in a finally block is guaranteed to be executed, regardless of how the code in the try block completes.

Exception propagationExceptions propagate up to through the lexical block structure of a java method, and then up to the method call stack.

If an exception is never caught, it propagates all the way to the main() method from which the program started, and cause the Java Interpreter to print an error message and a stack trace and exit.

Page 29: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 29

Exception

public static void b(int i) throws MyException {

int result;

try {

System.out.print("i = " + i);

result = c(i);

System.out.print(" c(i) = " + result);

}

catch (MyOtherException e) { // Point 2

// Handle MyOtherException exceptions:

System.out.println("MyOtherException: " + e.getMessage());

System.out.println("Handled at point 2");

}

finally {

// Terminate the output we printed above with a newline.

System.out.print("\n");

}

}

Page 30: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 30

Defining your own Exception

// Here we define some exception types of our own.

// Exception classes generally have constructors but no data or

// other methods. All these do is to call their superclass constructors.

class MyException extends Exception {

public MyException() { super(); }

public MyException(String s) { super(s); }

}

class MyOtherException extends Exception {

public MyOtherException() { super(); }

public MyOtherException(String s) { super(s); }

}

Page 31: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 31

Object Oriented Programming

ClassesA class is a structure that defines the data and the methods to work on that data. When you write programs in the Java language, all program data is wrapped in a class, whether it is a class you write or a class you use from the Java platform API libraries.

ObjectsAn instance is an executable copy of a class. Another name for instance is object. There can be any number of objects of a given class in memory at any one time.

Page 32: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 32

Example

class ExampleProgram {

public static void main(String[] args){

String text = new String("I'm a simple Program ");

System.out.println(text);

String text2 = text.concat(

"that uses classes and objects");

System.out.println(text2);

}

}

The output looks like this:

I'm a simple Program

I'm a simple Program that uses classes and objects

Page 33: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 33

Page 34: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 34

(cont.)

Inheritance Inheritance defines relationships among classes in an object-oriented language. In the Java programming language, all classes descend from java.lang.Object and implement its methods.

Page 35: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 35

Page 36: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 36

(cont.)

PolymorphismAnother way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.

Page 37: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 37

(cont.)

String custID = "munchkin";

Integer creditCard = new Integer(25);

Set s = new HashSet();

s.add(custID);

s.add(creditCard);

Page 38: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 38

(cont.)

Fields and Methods

Fields and methods can be declared private, protected, public, or package. If no access level is specified, the field or method access level is package by default.

private: A private field or method is accessible only to the class in which it is defined. protected: A protected field or method is accessible to the class itself, its subclasses, and classes in the same package.

public: A public field or method is accessible to any class of any parentage in any package. In Part 2, Lesson 6: Internationalization server data accessed by client programs is made public.

package: A package field or method is accessible to other classes in the same package.

Page 39: Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.

Ade Azurat, Advanced Programming, 2004 Review: Slide 39

Next…

Multithreading