intro to object-oriented (“oo”) design

23
Intro to Object-Oriented (“OO”) Design

Upload: misty

Post on 25-Feb-2016

21 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Intro to Object-Oriented (“OO”) Design

Intro to Object-Oriented (“OO”) Design

Page 2: Intro to Object-Oriented (“OO”) Design

OO Design• Simplified methodology

1. Write down detailed description of problem

2. Identify all (relevant) nouns and verbs3. From list of nouns, select objects4. Identify data components of each object5. From list of verbs, select operations

Page 3: Intro to Object-Oriented (“OO”) Design

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

Page 4: Intro to Object-Oriented (“OO”) Design

class Rectangle with Data Members and Operations

Page 5: Intro to Object-Oriented (“OO”) Design

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

Page 6: Intro to Object-Oriented (“OO”) Design

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.

Page 7: Intro to Object-Oriented (“OO”) Design

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.

Page 8: Intro to Object-Oriented (“OO”) Design

OO Design - Cash Register Example contd

Page 9: Intro to Object-Oriented (“OO”) Design

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)

Page 10: Intro to Object-Oriented (“OO”) Design

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.

Page 11: Intro to Object-Oriented (“OO”) Design

Wrapper Classes 1: The ‘Integer’ classConsider 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.

Page 12: Intro to Object-Oriented (“OO”) Design

The class Integer

Page 13: Intro to Object-Oriented (“OO”) Design

The class Integer (continued)

Page 14: Intro to Object-Oriented (“OO”) Design

The class Integer (continued)

Page 15: Intro to Object-Oriented (“OO”) Design

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:

Page 16: Intro to Object-Oriented (“OO”) Design

The class Integer (continued)

Integer num; num = new Integer(86)

num is a reference. 1350 is the address of num. Inside memory location 1350 is the object whose current state is 86.

Page 17: Intro to Object-Oriented (“OO”) Design

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.

Page 18: Intro to Object-Oriented (“OO”) Design

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

Page 19: Intro to Object-Oriented (“OO”) Design

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

Page 20: Intro to Object-Oriented (“OO”) Design

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

Page 21: Intro to Object-Oriented (“OO”) Design

There are other Wrapper classes:• Double• Long• Float• Character

Page 22: Intro to Object-Oriented (“OO”) Design

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

Page 23: Intro to Object-Oriented (“OO”) Design

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