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

Slides:



Advertisements
Similar presentations
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 1 Introducing Java.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 10 Classes Continued
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Getting Started What is Java? A programming language –Fully buzzword-compliant: A simple, object oriented, distributed, interpreted, robust, secure,
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Object Oriented Programming
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
POS 406 Java Technology And Beginning Java Code
JAVA BASICS Prepared by The Smartpath Information Systems
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Modern Programming Tools And Techniques-I
Java Programming Language
Chapter 13: Advanced GUIs and Graphics
Java Programming Language
UNIT-5.
Constructors, GUI’s(Using Swing) and ActionListner
(Computer fundamental Lab)
Exception and Event Handling
Java Exception Dept. Business Computing University of Winnipeg
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
CMSC 202 Exceptions.
Advanced GUIs and Graphics
Presentation transcript:

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)

Ade Azurat, Advanced Programming, 2004 Review: Slide 3 Java consists of three parts: the Java programming language the Java Virtual Machine the 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

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.

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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 6 Application class 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); }

Ade Azurat, Advanced Programming, 2004 Review: Slide 7 Application Structure and Elements

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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 9 Applet

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

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..."); }

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

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:

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.

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.

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.

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.

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.

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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 20 Layout Manager Border Layout CardLayout FlowLayout GridBagLayout … //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);

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

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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 23 Event Handling The 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; }

Ade Azurat, Advanced Programming, 2004 Review: Slide 24 Exception An exception is a class that descends from either java.lang.Exception or java.lang.RuntimeExcep tion 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.

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 occurred To 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.

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

Ade Azurat, Advanced Programming, 2004 Review: Slide 27 Exception Handling try The try clause simply establishes a block of code that is to have its exceptions and abnormal exists (through break, continue, return, or exception propagation) catch When 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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 28 Exception Handling Finally The 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 propagation Exceptions 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.

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

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

Ade Azurat, Advanced Programming, 2004 Review: Slide 31 Object Oriented Programming Classes A 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. Objects An 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.

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

Ade Azurat, Advanced Programming, 2004 Review: Slide 33

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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 35

Ade Azurat, Advanced Programming, 2004 Review: Slide 36 (cont.) Polymorphism Another 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.

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

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.

Ade Azurat, Advanced Programming, 2004 Review: Slide 39 Next… Multithreading