Intro to Object-Oriented (“OO”) Design. OO Design Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Unified Modeling Language
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 41 Defining Classes and Methods Chapter 4.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Computer Science 1620 Programming & Problem Solving.
COMP 110 Introduction to Programming Mr. Joshua Stough October 1, 2007.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Chapter 11: Classes and Data Abstraction
Software Engineering Principles and C++ Classes
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 24, 2004.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
BPJ444: Business Programming using Java Java basics Tim McKenna
Chapter 8 Arrays and Strings
Input, Output, and Processing
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Copyright © Curt Hill First Window Builder Program Easy GUIs in Eclipse.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 8: User-Defined Classes and ADTs
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 7: Characters, Strings, and the StringBuilder.
Lecture 2 Objectives Learn about objects and reference variables.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Data Structures Using Java1 Chapter 1 Software Engineering Principles and Java Classes.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Arrays Chapter 7.
Java Programming: Guided Learning with Early Objects
Outline Creating Objects The String Class Packages Formatting Output
A First Look at GUI Applications
Java Primer 1: Types, Classes and Operators
2.5 Another Java Application: Adding Integers
Java Programming: From Problem Analysis to Program Design,
User-Defined Classes and ADTs
Chapter 8: User-Defined Classes and ADTs
User-Defined Classes and ADTs
Outline Creating Objects The String Class The Random and Math Classes
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Intro to Object-Oriented (“OO”) Design

OO Design Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns and verbs 3. From list of nouns, select objects 4. Identify data components of each object 5. From list of verbs, select operations

Object-Oriented Design Example 1 Problem Statement –Write a program to input the length and width of a rectangle, and calculate and print the perimeter and area of the rectangle Nouns –Length, width, rectangle, perimeter, area

class Rectangle with Data Members and Operations

Object-Oriented Design - Example 2 An inoperable candy machine has a cash register and several dispensers to hold and release items sold by the machine Let’s design the software outline for a machine that dispenses 4 items: candies, chips, gum, and cookies Dispensers Cash register

Object-Oriented Design Example 2 (continued) The program should do the following: –Show the customer the different products sold by the candy machine –Let the customer make the selection –Show the customer the cost of the item selected –Accept money from the customer –Return change –Release the item; that is, make the sale Notice the careful thought to nouns & verbs. More on this shortly… The dispenser can keep track of the cost of the item, number in stock, etc.

OO Design - Cash Register Example contd We will need a Dispenser class to record info about the items we are selling. We will design a CashRegister class to record how much money was inserted, and how much should be returned. We will need a CandyMachine class in which we will instantiate 4 Dispenser objects, and 1 CashRegister object. Can you think of the fields & methods we would use for these classes? This is the key step. Don’t move on until you’ve spent some time thinking about this.

OO Design - Cash Register Example contd

Implementing Classes and Operations Algorithms are used to implement operations Construct and implement your own methods classes Integer, Double, Character, Long, Float –Known as wrapper classes –Provided so that values of primitive data types can be treated as objects –Have limitations (cannot change value stored in objects)

Wrapper Classes: A Wrapper is a class that wraps primitive data types into corresponding objects. While this can be useful at times (for example to convert a String into one of the primitive data types as we shall see), we still need a basic understanding of how they work.

Wrapper Classes 1: The ‘ Integer ’ class Consider the following line of code: int x; x = JOptionPane.showInputDialog(“Enter your age: “); You ma not realize it, but we have a type mismatch because the showInputDialog() method returns a String. As it turns out, this is not usually a problem because Java is often smart enough to try and cast an input that “looks” like an integer. Still, there is a much better way to handle things. There is a method called parseInt () that exists in a class called Integer. This method accepts a string and if possible, will convert this string to an integer. The method then returns that string. Here is how the code might look: String input; int age; input = JOptionPane.showInputDialog(“Enter your age: “); age = Integer.parseInt( input ); //parseInt converts input to an int A more efficient way of writing this code might be: int age; age = Integer.parseInt( JOptionPane.showInputDialog(“Enter your age: “) ) ; In this case, the String returned by the showInputDialog method is immediately converted to an int.

The class Integer

The class Integer (continued)

An ‘Integer’ object and an ‘int’ variable are NOT the same! You are familiar with variables of type int. However, an object of type Integer is NOT the same. Recall that what we typically call ‘objects’ are actually references. Also recall that references do NOT hold information related to the state of the object. Instead,a reference holds only the address of the object.We will discuss this with some examples:

The class Integer (continued) Integer num; num = new Integer(86) num is a reference is the address of num. Inside memory location 1350 is the object whose current state is 86.

Auto-boxing and Auto-Unboxing: Now as it turns out, because Integer objects are meant exclusively to hold int values, Java lets us take some shortcuts. Consider: Integer num = 25; Technically this should NOT mean that Integer holds the int value 25. It SHOULD mean that the address of the reference num is memory location 25. However, Java, recognizing what the Integer class is really all about, does allow us to assign the integer number 25 to the reference ‘num’. Although we can get away with this, I hope you recognize that the technically accurate way of doing this is: Integer num = new Integer( 25 ); Similarly, you should recognize that the statement: System.out.println( num ); should output the reference (address) of num. Instead, this statement will in fact, output the state (i.e. the integer value) held inside that reference location. Technically, however, we should have to say: System.out.println( num.intValue() ); All this being said, feel free to use the automatic ‘unboxing’ done by Java. I do require you to understand however, that this process is occuring behind the scenes.

The class Integer (continued) int x; Integer num; num = 25; For the most part, this statement is similar to the statement: num = new Integer(25); The expression: num = 25; ( as opposed to num.setInteger(25); ) is referred to as autoboxing of the int type

The class Integer (continued) int x; Integer num; The statement: x = num; This statement is equivalent to the statement: x = num.intValue(); This statement is referred to as auto unboxing of the int type

The class Integer (continued) To compare the values of two Integer objects (i.e. to see which is greater), you can use the method compareTo If you want to compare the values of two Integer objects for equality, you could use the method equals

There are other Wrapper classes: Double Long Float Character

Chapter Summary Every GUI contains a window Various components are added to content pane of window class JFrame is used to create windows JLabel is used to label GUI components and display information to user JTextFiled is used for input/output JButton generates action event Action event is sent to action listener

Chapter Summary (continued) Action listener must have method called actionPerformed class: collection of data members and methods associated with those members Object-Oriented Design (OOD) –Starts with a problem statement –Identifies classes required with nouns in problem statement –Identifies methods required with verbs in problem specification