1 / 23

Intro to Object-Oriented (“OO”) Design

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.

misty
Download Presentation

Intro to Object-Oriented (“OO”) Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. class Rectangle with Data Members and Operations

  5. 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

  6. 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.

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

  8. OO Design - Cash Register Example contd

  9. 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)

  10. 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.

  11. 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.

  12. The class Integer

  13. The class Integer (continued)

  14. The class Integer (continued)

  15. 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:

  16. 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.

  17. 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.

  18. 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

  19. 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

  20. 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

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

  22. Chapter Summary • Every GUI contains a window • Various components are added to content pane of window • classJFrame 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

  23. 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

More Related