CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Similar documents
CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

No SVN checkout today. Object-Oriented Design

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts. Object-Oriented Design CRC Cards - UML class diagrams

Object Oriented Design & Patterns. Part 1

Containers and Components

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Introduction to the JAVA UI classes Advanced HCI IAT351

Errors and Exceptions

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Laying Out Components. What is Widget Layout?

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Exceptions in Java

Exceptions. Examples of code which shows the syntax and all that

Basicsof. JavaGUI and SWING

CS 251 Intermediate Programming GUIs: Components and Layout

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Graphical User Interfaces. Swing. Jose Jesus García Rueda

CSCI 201L Midterm Written SOLUTION Fall % of course grade

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Graphical User Interfaces. Comp 152

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Java Programming Lecture 6

Today. cisc3120-fall2012-parsons-lectiii.3 2

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Layout. Dynamic layout, Swing and general layout strategies

Graphical User Interface (GUI)

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Part I: Learn Common Graphics Components

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Sri Vidya College of Engineering & Technology

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Introduction Unit 4: Input, output and exceptions

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Window Interfaces Using Swing Objects

Systems Programming Graphical User Interfaces

Dr. Hikmat A. M. AbdelJaber

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

CS 3331 Advanced Object-Oriented Programming Final Exam

Agenda. Container and Component

CS11 Java. Fall Lecture 4

Graphical User Interface (GUI)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Unit 6: Graphical User Interface

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Implementing Rules. Java rules ok

Lecture 19 Programming Exceptions CSE11 Fall 2013

CS11 Java. Fall Lecture 3

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CS 11 java track: lecture 3

CS 180 Fall 2006 Exam II

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

CSCI 201L Midterm Written Fall % of course grade

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Part 3: Graphical User Interface (GUI) & Java Applets

Graphical User Interfaces in Java - SWING

Java. GUI building with the AWT

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CS Exam 1 Review Suggestions

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

This page intentionally left blank

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

Murach s Beginning Java with Eclipse

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Abstract Classes, Exceptions

Introduction to Java Programming

Top-Level Containers

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Java Programming. Price $ (inc GST)

CS 180 Final Exam Review 12/(11, 12)/08

Window Interfaces Using Swing. Chapter 12

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

Introduction to Java Programming

Come & Join Us at VUSTUDENTS.net

Lab 2: File Input and Output

Mobile MOUSe JAVA2 FOR PROGRAMMERS ONLINE COURSE OUTLINE

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Transcription:

CSSE 220 Day 19 Object-Oriented Design Files & Exceptions Check out FilesAndExceptions from SVN

A practical technique OBJECT-ORIENTED DESIGN

Object-Oriented Design We won t use full-scale, formal methodologies Those are in later SE courses We will practice a common object-oriented design technique using CRC Cards Like any design technique, the key to success is practice

Key Steps in Our Design Process 1. Discover classes based on requirements 2. Determine responsibilities of each class 3. Describe relationships between classes

Discover Classes Based on Requirements Brainstorm a list of possible classes Anything that might work No squashing

Discover Classes Based on Requirements Prompts: Look for nouns Multiple objects are often created from each class So look for plural concepts Consider how much detail a concept requires: A lot? Probably a class Not much? Perhaps a primitive type Tired of hearing this yet? Don t expect to find them all add as needed

Determine Responsibilities Look for verbs in the requirements to identify responsibilities of your system Which class handles the responsibility? Can use CRC Cards to discover this: Classes Responsibilities Collaborators

CRC Cards Use one index card per class Class name Responsibilities Collaborators Q1

CRC Card Technique 1. Pick a responsibility of the program 2. Pick a class to carry out that responsibility Add that responsibility to the class s card 3. Can that class carry out the responsibility by itself? Yes Return to step 1 No Decide which classes should help List them as collaborators on the first card ` Design a simple e-mail messaging system. A message has a recipient, a sender, a subject, and a message text. A mailbox can store messages. Supply a number of mailboxes for different users and a user interface for users to log in, send messages to other users, read their own messages, and log out.

CRC Card Tips Spread the cards out on a table Or sticky notes on a whiteboard instead of cards Use a token to keep your place A quarter or a magnet Focus on high-level responsibilities Some say < 3 per card Keep it informal Rewrite cards if they get too sloppy Tear up mistakes Shuffle cards around to keep friends together

Design a simple e-mail messaging system. A message has a recipient, a sender, a subject, and a message text. A mailbox can store messages. Supply a number of mailboxes for different users and a user interface for users to log in, send messages to other users, read their own messages, and log out.

BREAK

NEW! Describe the Relationships Classes usually are related to their collaborators Draw a UML class diagram showing how Common relationships: Inheritance: only when subclass is a special case Aggregation: when one class has a field that references another class Dependency: like aggregation but transient, usually for method parameters, has a temporarily Association: any other relationship, can label the arrow, e.g., constructs

Summary of UML Class Diagram Arrows Q2

Draw UML class diagrams based on your CRC cards Initially just show classes (not insides of each) Add insides for two classes OBJECT-ORIENTED DESIGN

When JFrame s and JPanel s defaults just don t cut it. SOME NOTES ON LAYOUT MANAGERS

Recall: How many components can a JFrame show by default? Answer: 5 We use the two-argument version of add: JPanel p = new JPanel(); frame.add(p, BorderLayout.SOUTH); JFrame s default LayoutManager is a BorderLayout LayoutManager instances tell the Java library how to arrange components BorderLayout uses up to five components

Recall: How many components can a JPanel show by default? Answer: arbitrarily many Additional components are added in a line JPanel s default LayoutManager is a FlowLayout

Setting the Layout Manager We can set the layout manager of a JPanel manually if we don t like the default: JPanel panel = new JPanel(); panel.setlayout(new GridLayout(4,3)); panel.add(new JButton("1")); panel.add(new JButton("2")); panel.add(new JButton("3")); panel.add(new JButton("4")); //... panel.add(new JButton("0")); panel.add(new JButton("#")); frame.add(panel);

Lots of Layout Managers A LayoutManager determines how components are laid out within a container BorderLayout. When adding a component, you specify center, north, south, east, or west for its location. (Default for a JFrame.) FlowLayout: Components are placed left to right. When a row is filled, start a new one. (Default for a JPanel.) GridLayout. All components same size, placed into a 2D grid. Many others are available, including BoxLayout, CardLayout, GridBagLayout, GroupLayout If you use null for the LayoutManager, then you must specify every location using coordinates More control, but it doesn t resize automatically Q3-4

Reading & writing files When the unexpected happens FILES AND EXCEPTIONS

Review of Anonymous Classes Look at GameOfLifeWithIO GameOfLife constructor has 2 listeners, two local anonymous class ButtonPanel constructor has 3 listeners which are local anonymous classes Feel free to use as examples for your project

File I/O: Key Pieces Input: File and Scanner Output: PrintWriter and println Be kind to your OS: close() all files Letting users choose: JFileChooser and File Expect the unexpected: Exception handling Refer to examples when you need to

Exceptions Used to signal that something went wrong: throw new EOFException( Missing column ); Can be caught by exception handler Recovers from error Or exits gracefully Q5

A Checkered Past Java has two sorts of exceptions 1. Checked exceptions: compiler checks that calling code isn t ignoring the problem Used for expected problems 1. Unchecked exceptions: compiler lets us ignore these if we want Used for fatal or avoidable problems Are subclasses of RunTimeException or Error

A Tale of Two Choices Dealing with checked exceptions 1.Can propagate the exception Just declare that our method will pass any exceptions along public void loadgamestate() throws IOException Used when our code isn t able to rectify the problem 1.Can handle the exception Used when our code can rectify the problem Q6

Handling Exceptions Use try-catch statement: try { // potentially exceptional code } catch (ExceptionType var) { } // handle exception Related, try-finally for clean up: Can repeat this part for as many different exception types as you need. try { // code that requires clean up } finally { // runs even if exception occurred } Q7