Object-Oriented Software Engineering

Slides:



Advertisements
Similar presentations
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Advertisements

Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Object-Oriented Programming with Java Java with added Swing Lecture 3.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming, Second Edition Chapter Five Input and Selection.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Object Oriented Programming Object and Classes Lecture 3 MBY.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Introduction to GUI in 1 Graphical User Interface Nouf Almunyif.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
3 Introduction to Classes and Objects.
Building a Swing Interface
Data Types – Reference Types
Introduction to Classes and Objects
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Recitation 2 Exception handling.
Chapter 10 – Exception Handling
“Form Ever Follows Function” Louis Henri Sullivan
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Some Eclipse shortcuts
MIT AITI 2003 Lecture14 Exceptions
2.5 Another Java Application: Adding Integers
Creating and Modifying Text part 2
Using Procedures and Exception Handling
Message, Input, Confirm, and Specialized Dialogs
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
Introduction to GUI in Graphical User Interface Nouf Almunyif.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Object-Oriented Software Engineering
CS18000: Problem Solving and Object-Oriented Programming
Chapter 2 - Introduction to Java Applications
Object-Oriented Software Engineering
Coding Concepts (Basics)
Message, Input, and Confirm Dialogs
CS288 Lab Exercise 2.
Constructors, GUI’s(Using Swing) and ActionListner
CS431 ws99 Half Text Half Graphics
Exception Handling Contents
Getting Started with Java
Barb Ericson Georgia Institute of Technology Oct 2005
JOptionPane class.
F II 2. Simple Java Programs Objectives
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
CMSC 202 Exceptions.
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Object-Oriented Software Engineering Java Exceptions, Dialog Boxes, and Icons

Contents Error Handling Exceptions Dialogs Option Dialogs Icons

Error Handling Best way to handle errors is to avoid them in the first place. Java is designed to be modular to separate out functionality. However modularity can be broken if fields are not protected. public class MyClass { Double dbl; } public class RogueClass { MyClass mc = new MyClass(); public void make_trouble (String x) { mc.dbl = new Double(x); make_trouble("shoo") will result in an error. There is no type checking to prevent this.

Error Handling Fields (variables) should be declared as private normally. That makes them inaccessible directly from any other class. set and get methods should be defined for any fields that are to be accessed from other classes. When this is inconvenient fields should be declared protected. That is only to be accessed from sub-classes and classes in the same package.

Error Handling Note this is not valid UML, its just to illustrate the idea SomeClass private JFrame frame; protected Double dbl; get_frame() set_frame()

Exceptions public class MyClass { private Double dbl; public set_dbl (String st) { try { dbl = new Double(st); } catch (NumberFormatException exp1) { System.out.println("OOOPS: " + e1.toString() + "\n"); } public get_dbl () {return dbl;} If you're not sure what type of exception to expect force an error in the code. The compiler will helpfully display an error message including the exception type. TIP

Other Exceptions try { icon_file_name = file.getCanonicalPath(); Certain methods throw exceptions. In this case the compiler will not allow those methods to be used unless the exceptions are caught. try { icon_file_name = file.getCanonicalPath(); } catch (IOException exp0) { System.out.println("IOException : \n" + e1.toString() + "\n"); } Note body of catch statement can be anything. Even empty.

Dialogs string_age = stringAge; try { age = new Double(stringAge); The following code taken from the PersonMaker.java application. The full code is on the course web site. With user provided values, rather than print error messages, can use dialogs to correct values. public void setAge (String stringAge) { string_age = stringAge; try { age = new Double(stringAge); } catch (NumberFormatException exp1) { string_age = handleTextInput("Age"); age = new Double(string_age); }

Dialogs String s = (String)JOptionPane.showInputDialog( null, Useful bit of code to cope with capturing user input for simple values: public String handleTextInput (String dis_text) { String s = (String)JOptionPane.showInputDialog( null, dis_text, "PersonMaker Class", JOptionPane.PLAIN_MESSAGE, "Fluffy"); return s; }

Dialogs JOptionPane.showInputDialog Parameters: parentComponent - the parent Component for the dialog message - the Object to display title - the String to display in the dialog title bar messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE icon - the Icon image to display selectionValues - an array of possible selections initialSelectionValue - the value used to initialize the input field

Message Dialogs Simple alerts can also be shown as dialogs. JOptionPane.showMessageDialog(null, "This string", "That string", JOptionPane.PLAIN_MESSAGE);

Input Dialogs Simple input dialogs can also be shown with little code: JOptionPane.showInputDialog(null, "This string", "That string", JOptionPane.PLAIN_MESSAGE);

Input Dialogs Simple input dialogs with optional argument can also be shown with little code: JOptionPane.showInputDialog(null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, null, "Fluffy");

Code to implement option pane import java.awt.*; import javax.swing.*; import javax.swing.JOptionPane; import javax.swing.JDialog; public class SimDialog { public static void main(String[ ] args) { JOptionPane.showInputDialog( null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, "Fluffy"); }

Using Icons in dialogs Icon is an image of fixed size that can be used to decorate most Swing components in some way or other. For any class named, say, Foo we can add method to define icons using java.net.URL (taken from Java Tutorial). return value type protected ImageIcon createImageIcon(String path, String description) { java.net.URL imgURL = Foo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, description); } else { System.err.println("Couldn't find file: " + path); return null; } Every class is derived from Object class. Hence can use methods and fields of that class.

Using Icons in dialogs Can now add Icons as decoration to dialog boxes. Full code is in file SimIcon.java public static void main(String[ ] args) { ImageIcon dragon = createImageIcon("images/black-dragon.gif", "Black Dragon"); JOptionPane.showInputDialog( null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, dragon, "Fluffy"); }

Using Icons in dialogs

Lists of Options in Dialog To make the user choose one of a fixed number of alternatives can invoke showInputDialog with an array of allowed values. Can instantiate a finite array of known values very simply: String[ ] arrayOfOpts = { "Fee", "Fi", "Fo", "Fum" }; Normally when array holds unknown values is created with new statement like all other fields: float[] arrayOfFloats = new float[10];

Lists of Options in Dialog Putting it together we can create SimOpts.java application. This file can be found in code directory for this lecture. public static void main(String[ ] args) { ImageIcon dragon = createImageIcon("images/black-dragon.gif", "Black Dragon"); String[] arrayOfOpts = { "Fee", "Fi", "Fo", "Fum" }; JOptionPane.showInputDialog( null, "This string", "That string", JOptionPane.PLAIN_MESSAGE, dragon, arrayOfOpts, null); }

Lists of Options in Dialog