1 / 135

Chapter 10: Object-oriented Programming and Software Development

Chapter 10: Object-oriented Programming and Software Development. Kinds of Interclass Relationships Inheritance Relationships Abstract Classes The Software Lifecycle Object-oriented Software Development Operating Systems. A Big Problem. Severe shortage of good developers

walla
Download Presentation

Chapter 10: Object-oriented Programming and Software Development

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. Chapter 10:Object-oriented Programming and Software Development Kinds of Interclass Relationships Inheritance Relationships Abstract Classes The Software Lifecycle Object-oriented Software Development Operating Systems

  2. A Big Problem... • Severe shortage of good developers • Demand increasing 12% per year • Supply increasing 4% per year • Therefore, software development must become • More efficient • Higher quality Demand Supply Programming and Problem Solving With Java

  3. The Solution?? Object Orientation! • Easier to write new software • Easier to reuse existing software • Object-oriented development works like people think • Focus on real-world objects (doors, engines, transistors) • Focus on relationship between these objects (attached- to, part-of) • Traditional software development focuses mainly on functionality Programming and Problem Solving With Java

  4. Object-oriented Approach • Object • Represents a real-world "thing" • Class • Template for similar objects • Class notation in UML • Steps to object-oriented development • Identify objects in the real world • Determine characteristics and behaviors of objects • Decide how objects relate to each other Programming and Problem Solving With Java

  5. Interclass Relationships • After identifying classes, must decide how they relate to each other • Several common interclass relationships • Association • Dependency • Aggregate • Inheritance • Classes may or may not depend on each other • Existence-dependent: objects of one class depend on objects in another • Existence-independent: objects of one class do not depend on objects in another Programming and Problem Solving With Java

  6. Interclass Relationships • Association relationship • Appropriate for existence-independent classes, where each class is aware of the other • Most general kind of interclass relationship • Example: Department & Employee • Each employee works for one department • Each department can have any number of employees • Existence-independent classes • Example: Student & Course Programming and Problem Solving With Java

  7. Interclass Relationships • Implementation of association relationship • Each class should be aware of the other • Therefore, each class should reference the other • Example • class Department • { • // Methods here • // Reference an array of up to 100 employees • private Employee[] worker = new Employee[100]; • } • class Employee • { • // Methods here • // Reference a single department • private Department dept; • } Programming and Problem Solving With Java

  8. Interclass Relationships • Dependency Relationship • Appropriate for existence-independent entities, where one of the classes is not aware of the other • Class that provides a service is the server (not aware of the relationship) • Class that uses the service is the client (aware of the relationship) • Example: Department & Schedule Programming and Problem Solving With Java

  9. Interclass Relationships • Implementation of dependency relationship • Client only is aware of the relationship • Therefore, client class should reference the server • Example • class Department • { • public void allocateTime(Schedule weeklySchedule) • { • ... • } • ... • } • Reference is a parameter or local variable • Should not use instance or class variable • Reason: Schedule not part of Department Programming and Problem Solving With Java

  10. Interclass Relationships • Aggregate Relationship • Appropriate for existence-dependent classes • Objects one class depend on another • Example: Employee & Dependent Programming and Problem Solving With Java

  11. Interclass Relationships • Implementation of aggregation relationship • Implement as variable in containing class • Example • class Employee • { • // Methods here • private Dependent[] dependentSet = new Dependent[10]; • } Programming and Problem Solving With Java

  12. Inheritance Programming and Problem Solving With Java

  13. Inheritance Relationship • A class can inherit • Behavior - methods • State - variables • Allows creation of new classesthat extend existing classes • Notation Higher level classes specialize generalize Lower level classes Programming and Problem Solving With Java

  14. Inheritance: Example • public class Person • { • // Constructor • public Person(String name, int age) • { • this.name = name; • this.age = age; • } • // incrementAge: Adds one to the person's age • public void incrementAge() • { • age++; • } • // toString: Returns a string version of the person's • // information • public String toString() • { • return "Person (Name: " + getName() + " Age: " • + getAge() + ")"; • } • // getName: Returns the person's name • public String getName() • { • return name; • } • // getAge: Returns the person's age • public int getAge() • { • return age; • } • private String name; • private int age; • } Example: Existing Person class Programming and Problem Solving With Java

  15. Inheritance: Example • import Person; • public class Employee extends Person • { • // Constructor • public Employee(String name, int age, int empNum, String department) • { • super(name, age); • this.empNum = empNum; • this.department = department; • } • // changeDepartment: Sets the employee's department to the • // given department • public void changeDepartment(String department) • { • this.department = department; • } • // toString: Returns a string version of the employee's • // information • public String toString() • { • return "Employee (Name: " + getName() + " Age: " • + getAge() + " EmpNum: " + empNum • + " Dept: " + department + ")"; • } • // getEmpNum: Returns the employee's employee number • public int getEmpNum() • { • return empNum; • } • // getDepartment: Returns the employee's department • public String getDepartment() • { • return department; • } • private int empNum; • private String department; • } Example: new Employee class Programming and Problem Solving With Java

  16. Inheritance: With and Without Programming and Problem Solving With Java

  17. Inheritance: Super- & Subclasses • Subclass • Descendent class • Class can have any number ofsubclasses • Superclass • Ancestor class • Single inheritance: at most one superclass (Java) • Multiple inheritance: any number of superclasses (C++) • No cycles allowed in classhierarchy Programming and Problem Solving With Java

  18. Inheritance: Using • Define Person and Employee variablesand objects • Person p = new Person("Smith", // Name • 25); // Age • Employee e = new Employee("Jones", // Name • 33, // Age • 1234, // Employee number • "Accounting"); // Department • Display age • System.out.println(p.getAge()); // Displays 25 • System.out.println(e.getAge()); // Displays 33 • Display department • System.out.println(e.getDepartment()); // Displays "Accounting" • System.out.println(p.getDepartment()); // Compile-time error • Display objects (toString()) • System.out.println(p); • System.out.println(e); • // Person (Name: Smith Age: 25) • // Employee (Name: Jones Age: 33 EmpNum: 1234 Dept: Accounting) Programming and Problem Solving With Java

  19. Inheritance: Using • Example: Document & EncryptedDocument • public class Document • { • // Constructor • public Document(int id) • { • this.id = id; • text = ""; • } • // changeText: Changes text to a new value • public void changeText(string text) • { • this.text = text; • } • // changeID: Changes document ID to a new value • public void changeId(int id) • { • this.id = id; • } • // getText: Returns current text value • public String getText() • { • return text; • } • // getID: Returns current document id • public int getId() • { • return id; • } • private String text; • private int id; • } Programming and Problem Solving With Java

  20. Inheritance: Using • EncryptedDocument class • import Document; • public class EncryptedDocument extends Document • { • // Constructor • public EncryptedDocument(int id) • { • super(id); • } • // changeText: Change text to a new value, encrypting with • // the given encryption key • public changeText(String text, int encryptionKey) • { • super.changeText(encrypt(text, encryptionKey)); • } • // getText: Returns current text value in decrypted form, • // using the given decryptionKey • public String getText(int decryptionKey) • { • return encrypt(super.getText(), decryptionKey); • } • // encrypt: Encrypts the document with the given key • private String encrypt(String text, int key) • { • String result = text; • for (int i = 0; i < result.length(); i = i + 1) • { • result[i] = result[i] + key % 127; • } • return result; • } • } Programming and Problem Solving With Java

  21. Inheritance: Using • Using Document and EncryptedDocument • // Demonstrates use of Document and EncryptedDocument • // classes. • import Document; • import EncryptedDocument; • public class TestDocument • { • public static void main(String[] args) • throws java.io.IOException • { • Document plainDocument = new Document(123); • EncryptedDocument secretDocument = new EncryptedDocument(456); • String response; • System.out.println("--- Document example ---"); • response = Keyboard.readString("Enter a message: "); • // Store the response without encryption • plainDocument.changeText(response); • // Retrieve the response • System.out.println("Message of document id " • + plainDocument.getId() + " is: " • + plainDocument.getText()); Programming and Problem Solving With Java

  22. Inheritance: Using • Using Document and EncryptedDocument (cont'd) • System.out.println(); • System.out.println("--- EncryptedDocument example ---"); • response = Keyboard.readString("Enter a message: "); • int encryptionKey • = Keyboard.readInt("Enter an encryption key: "); • // Store response using the encryption key • secretDocument.changeText(response, encryptionKey); • // Retrieve the response without decrypting it • System.out.println("Encrypted message of document id " • + secretDocument.getId() + " is: " • + secretDocument.getText(0)); • // Retrieve the decrypted response, using the key • int decryptionKey • = Keyboard.readInt("Enter the decryption key: "); • System.out.println("Decrypted message of document id " • + secretDocument.getId() + " is: " • + secretDocument.getText(decryptionKey)); • } • } Programming and Problem Solving With Java

  23. Inheritance: Using • Using Document and EncryptedDocument (cont'd) • Output • --- Document example --- • Enter a message: This is NOT a secret • Message of document id 123 is: This is NOT a secret • --- EncryptedDocument example --- • Enter a message: This IS a secret • Enter an encryption key: 3 • Encrypted message of document id 456 is: Wklv#LV#d#vhfuhw • Enter the decryption key: -3 • Decrypted message of document id 456 is: This IS a secret Programming and Problem Solving With Java

  24. Inheritance: When to Use • Kinds of interclass relationships • Aggregate: existence-dependent classes • Dependency: object of one class uses services of another • Association: existence-independent classes • Inheritance: one class is a special case of another • Appropriate use of inheritance • Book is a kind of document • Automobile is a kind of vehicle • House is a kind of building • Transistor is a kind of discrete electronic component • Iron is a kind of metal • Lake is a kind of water body • Clarinet is a kind of woodwind instrument Programming and Problem Solving With Java

  25. Inheritance: When to Use • Inappropriate use of inheritance • Problem: How to classify electric automobiles? • Use inheritance only for "is a kind of" relationships • Use aggregation, dependency, or association for other relationships • Which relationship should be used above? Programming and Problem Solving With Java

  26. Interclass Relationships Summary Programming and Problem Solving With Java

  27. Abstract Classes • Abstract class • Standard superclass • Can't create objects of the class • Example • Two kinds of employees: Salaried & Hourly • Each employee is one of these two kinds • Employee should be an abstract class Programming and Problem Solving With Java

  28. Abstract Classes • To create an abstract class • Make one of the methods abstract • public class MyClass // Should define the class abstract, too • { • public abstract myMethod(); • ... • } • Or make the class abstract • public abstract class MyClass // Preferred approach • { • public abstract myMethod(); • ... • } • Abstract method • Has no body • Subclasses must override (provide implementation) Programming and Problem Solving With Java

  29. Abstract Classes: Example • import Person; • public abstract class Employee extends Person • { • // Constructor • public Employee(String name, int age, • int empNum, String department) • { • ... • } • // changeDepartment: Sets the employee's department to the • // given department • public void changeDepartment(String department) • { • ... • } • // toString: Returns a string version of the employee's • // information • public String toString() • { • ... • } • // getEmpNum: Returns the employee's employee number • public int getEmpNum() • { • ... • } • // getDepartment: Returns the employee's department • public String getDepartment() • { • ... • } • // getWeeklyPay: Returns the weekly pay for this employee • public abstract Money getWeeklyPay(); • private int empNum; • private String department; • } Example: abstractEmployee class Abstract method Programming and Problem Solving With Java

  30. Abstract Classes: Example • import Employee; • public class SalariedEmployee extends Employee • { • // Constructor • public SalariedEmployee(String name, int age, • int empNum, String department, • Money salary) • { • super(name, age, empNum, department); • this.salary = salary; • } • // changeSalary: Changes the salary to the new value • public void changeSalary(Money salary) • { • this.salary = salary; • } • // getWeeklyPay: Returns the weekly pay for the employee • public Money getWeeklyPay() • { • return salary; • } • private Money salary; • } Example: subclass of Employee Override abstract method Programming and Problem Solving With Java

  31. Abstract Classes: TextMenu • TextMenu: simplifies development of text-based menus • Design of program that uses TextMenu Programming and Problem Solving With Java

  32. Abstract Classes: TextMenu • Simple example of how to use TextMenu • +--- Test Menu --- • | F) Enter first name L) Enter last name D) Display name • | Q) Quit • | Enter selection: f • Enter first name: Sally • +--- Test Menu --- • | F) Enter first name L) Enter last name D) Display name • | Q) Quit • | Enter selection: l • Enter last name: Johnson • +--- Test Menu --- • | F) Enter first name L) Enter last name D) Display name • | Q) Quit • | Enter selection: d • Name is Sally Johnson • +--- Test Menu --- • | F) Enter first name L) Enter last name D) Display name • | Q) Quit • | Enter selection: q Programming and Problem Solving With Java

  33. Abstract Classes: TextMenu • Must provide application class • Subclass of TextMenu • Skeleton for application class • import TextMenu; • class MyApplication extends TextMenu • { • // Constructor • public MyApplication() • { • // Initialize the menu title and menu selections • } • // handleEvent: Handles the given event code, returns the • // same event code • public int handleEvent(int event) • throws java.io.IOException • { • // Take action according to the event code • } • // Private variables for the application go here • } Programming and Problem Solving With Java

  34. Abstract Classes: TextMenu • Constructor of application class • Set the menu title • Set up the menu • Other initialization required for the application • // Constructor • public MyApplication() • { • // Set menu title • super("Test Menu"); • // Initialize menu choices • addSelection("Enter first name", 'F', 1); • addSelection("Enter last name", 'L', 2); • addSelection("Display name", 'D', 3); • addSelection("Quit", 'Q', TextMenu.QUIT); • // Initialize variables for this application • firstName = ""; • lastName = ""; • } Programming and Problem Solving With Java

  35. Abstract Classes: TextMenu • addSelection() method sets up menu choice • +--- Test Menu --- • | F) Enter first name L) Enter last name D) Display name • | Q) Quit • | Enter selection: f • Enter first name: Sally • addSelection("Enter first name", 'F', 1); • Arguments • Menu text • Character to type to select • Event code (sent to handleEvent() method) Programming and Problem Solving With Java

  36. Abstract Classes: TextMenu • handleEvent() method in application class • // handleEvent: Handles the given event code, returns the • // same event code • public int handleEvent(int event) • throws java.io.IOException • { • switch (event) • { • case 1: • firstName • = Keyboard.readString("Enter first name: "); • break; • case 2: • lastName • = Keyboard.readString("Enter last name: "); • break; • case 3: • System.out.println("Name is " + firstName • + " " + lastName); • break; • } • return event; • } Programming and Problem Solving With Java

  37. Abstract Classes: TextMenu • Need main() method to run the program • public class DemonstrateMenu • { • public static void main(String[] args) • throws java.io.IOException • { • MyApplication app = new MyApplication(); • app.run(); • } • } • That's it! Programming and Problem Solving With Java

  38. Event-driven programming Programmer writes code to respond to events Program runs under control of a supervisor Gives user more control over how program runs Traditional programming Programmer writes code that controls all aspects of the computer Program in complete control Typically gives user little control over how program runs Abstract Classes: TextMenu Programming and Problem Solving With Java

  39. Abstract Classes: TextMenu • TextMenu source code • // This is a simple application framework for • // developing programs controlled by a menu of selections. • import Keyboard; • import Format; • import Debug; • // ---- TextMenuItem class ---- • class TextMenuItem • { • public static final int SELECTION_WIDTH = 20; • public static final int SELECTIONS_PER_LINE = 3; • // Constructor • public TextMenuItem(String title, char selectionChar, • int eventCode) • { • this.title = title; • this.selectionChar = selectionChar; • this.eventCode = eventCode; • } Programming and Problem Solving With Java

  40. Abstract Classes: TextMenu • TextMenu source code (continued) • // toString: Returns the menu item's selection character • // and title • public String toString() • { • return selectionChar + ") " • + Format.pad(title, SELECTION_WIDTH); • } • // getSelectionChar: Returns the menu item's selection • // character • public char getSelectionChar() • { • return selectionChar; • } • // getEventCode: Returns the menu item's event code • public int getEventCode() • { • return eventCode; • } • char selectionChar; • int eventCode; • String title; • } Programming and Problem Solving With Java

  41. Abstract Classes: TextMenu • TextMenu source code (continued) • // ------ TextMenu Class (abstract class) -------- • public abstract class TextMenu • { • public static final int QUIT = 0; • public static final int CANCEL_QUIT = -1; • static final int DEFAULT_SELECTIONS = 10; • static final int IGNORE_MAX = 80; • // Constructor • public TextMenu(String title, int maxSelections) • { • this.title = title; • numSelections = 0; • selection = new TextMenuItem[maxSelections]; • } • // Constructor • public TextMenu(String title) • { • this(title, DEFAULT_SELECTIONS); • } Programming and Problem Solving With Java

  42. Abstract Classes: TextMenu • TextMenu source code (continued) • // addSelection: Adds the selection to the list of selections • // and increments numSelections • // NOTE: isFull() should be false, and there should not be an • // existing menu selection with the same selection character, • // and the event code should be 0 or more (QUIT is defined as • // 0). • public void addSelection(String title, char selectionChar, • int eventCode) • { • Debug.assert(!isFull(), • "Menu system full: Use TextMenu(title, maxSelections)"); • Debug.assert(searchSelection(selectionChar) == -1, • "Duplicate selection character used for " + title); • Debug.assert(eventCode >= 0, • "Event code must be >= 0 for " + title); • selection[numSelections] • = new TextMenuItem(title, selectionChar, eventCode); • numSelections++; • } Programming and Problem Solving With Java

  43. Abstract Classes: TextMenu • TextMenu source code (continued) • // handleEvent: Returns event code (child class must override) • public abstract int handleEvent(int event) • throws java.io.IOException; • // run: Displays menu, gets user selection, handles associated • // event. Continues until quit menu item selected. • // NOTE: quit menu item must exist (else endless loop) • public void run() • throws java.io.IOException • { • do • { • display(); • } while (handleEvent( • selection[getMenuSelection()].getEventCode()) • != TextMenu.QUIT); • } • // getNumSelections: Returns the number of selections in the menu • public int getNumSelections() • { • return numSelections; • } Programming and Problem Solving With Java

  44. Abstract Classes: TextMenu • TextMenu source code (continued) • // isFull: Returns true if there's no room for an additional • // selection, false otherwise • public boolean isFull() • { • return numSelections == selection.length; • } • // display: Displays the menu on the output stream • private void display() • { • // Display menu title • System.out.println(); • System.out.println("+--- " + title + " ---"); • // Display menu choices • for (int i = 0; i < numSelections; i++) • { • if (i % TextMenuItem.SELECTIONS_PER_LINE == 0) • { • System.out.println(); • System.out.print("| "); • } • System.out.print(selection[i]); • } • System.out.println(); • } Programming and Problem Solving With Java

  45. Abstract Classes: TextMenu • TextMenu source code (continued) • // getMenuSelection: Get user's selection, continue until user • // types valid selection • private int getMenuSelection() • throws java.io.IOException • { • char response; • int menuSelection; • response = Keyboard.readChar("| Enter selection: "); • menuSelection = searchSelection(response); • while (menuSelection == -1) • { • System.out.println("| Please enter a valid selection"); • response = Keyboard.readChar("| Enter selection: "); • menuSelection = searchSelection(response); • } • return menuSelection; • } Programming and Problem Solving With Java

  46. Abstract Classes: TextMenu • TextMenu source code (continued) • // searchSelection: Returns index of the selection, or -1 • // if userSelection not in menu items (uses • // linear search) • private int searchSelection(char userSelection) • { • for (int i = 0; i < numSelections; i++) • { • if (Character.toLowerCase( • selection[i].getSelectionChar()) • == Character.toLowerCase(userSelection)) • { • return i; • } • } • return -1; • } • // Instance variables • String title; • TextMenuItem[] selection; • int numSelections; • } Programming and Problem Solving With Java

  47. Software Lifecycle • Software engineering concerned with • Improving software reliability • Increasing productivity of developers • Software engineering has contributed to • Programming in the small: individual developers • New programming languages and environments • More accurate program measurements • Better programming techniques • Programming in the large: large teams • New management techniques • Communication tools Programming and Problem Solving With Java

  48. Software Lifecycle • Early days of software • Early 1950's to about 1970 • Ad hoc development • Software development as an art • Problem: the software crisis • Programs are late, buggy, overbudget • Response in the late 1960's • Attempt to use sound engineering practices in software development • One result: the software lifecycle Programming and Problem Solving With Java

  49. Software Lifecycle • Software lifecycle indicates • How and when software is designed • How and when software is used • How and when software is maintained and retired • Software lifecycle helps developers • Gives them a plan to work from • Keeps teams more organized • Gives them an idea of how far along they are Programming and Problem Solving With Java

  50. Software Lifecycle • Waterfallmodel: the original softwarelifecyle • Finish each phasebefore moving to the next • Go through model oncefor each project • Works well for developmentof batch programs • Appropriate for expensivecomputer systems Programming and Problem Solving With Java

More Related