1 / 44

A Second Look at Classes and Objects

A Second Look at Classes and Objects. Contents. Stock Purchase Problem Employee Problem Drawing Lines Exercises. A. Stock Purchase Problem. A stock purchase consists of info of a stock and number of shares Info of a stock includes the trading symbol and share price

Télécharger la présentation

A Second Look at Classes and Objects

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. A Second Look at Classes and Objects

  2. Contents • Stock Purchase Problem • Employee Problem • Drawing Lines • Exercises

  3. A. Stock Purchase Problem • A stock purchase consists of info of a stock and number of shares • Info of a stock includes the trading symbol and share price • Trading symbol is a short series of characters that are used to identify the stock on the stock exchange • Develop a program to calculate the cost of a stock purchase, given that cost = number-of shares×share-price

  4. Solution • Problem Description • Preliminary Design • Test Cases • Developing the Test Class • Fixing the Test Class

  5. 1. Problem Description • Calculate the cost of a stock purchase • Input: number of shares, share price • Output: cost

  6. 2. Preliminary Design

  7. 3. Test Cases • Input: • Stock • Trading symbol: XYZ • Share price: $9.62 • Number of shares: 100 • Output: $962.0

  8. 4. Developing the Test Class

  9. 5. Fixing Test Class 4.1. Creating Class Stock 4.2. Creating Class StockPurchase 4.3. Running Test Class To See Red Bar 4.4. Changing delta in Test Method

  10. 4.1. Creating Class Stock

  11. 4.2. Creating Class StockPurchase

  12. 4.4. Changing delta in Test Method

  13. B. Employee Problem • Info of an employee includes: first name, last name, birthday, and hire day. Develop the Employee class

  14. Solution • Problem Description • Preliminary Design • Test Cases • Developing the Test Class • Fixing the Test Class

  15. 1. Problem Description • Develop the Employee class

  16. 2. Preliminary Design

  17. Alternative 2

  18. Alternative 3

  19. Alternative 4

  20. 3. Test Cases • Example 1 • Person • Name • First name: Sue • Last name: Jones • Birthday: Sept 5, 1986 • Hire day: Jan 1, 2007 • Example 2 • Person • Name • First name: Bob • Last name: Blue • Birthday: July 24, 1949 • Hire day: March 12, 1988

  21. 4. Developing Test Class for Alternative 3

  22. 5. Fixing Test Class • Develop Name

  23. Develop Person

  24. Develop Employee

  25. C. Drawing Lines 1. Drawing Random Lines 2. Drawing a Flower

  26. 1. Drawing Random Lines • Draw a random number of lines (at least 1 and at most 5) inside a frame

  27. Solution 1.1. Developing the Raw View 1.2. Drawing Random Lines to the View

  28. 1.1. Developing the Raw View

  29. 1.2. Drawing Random Lines to the View

  30. 2. Drawing a Flower

  31. D. Exercises • The Savings Account Problem • The Course Problem • The Sports Car Problem • The Retail Problem • The Parking Problem

  32. 1. The Savings Account Problem • A savings account has a balance indicating the amount the saver currently has on deposit. The bank applies the same annual interest rate for all account holders • Develop a program that has the following behaviors • Setting a new value for the annual interest rate • Calculating the monthly interest (by multiplying the balance by the annual interest rate divided by 12) • Updating the balance monthly by adding the monthly interest to the current balance • Test your program with two savings accounts with balances of $2000.00 and $3000.00. In the beginning, the annual interest rate is 4%. After a month, the annual interest rate is changed to 5%

  33. 2. The Course Problem • A course holds the following information: • The course name • The instructor’s last name, first name, and office number • The textbook’s title, author, and publisher

  34. 3. The Sports Car Problem • A sports car has the following information: • The type (make) • The color • The price • The type is either Porsche, Ferrari, or Jaguar • The color is either red, black, blue, or silver

  35. 4. The Retail Problem • An item in a retail store holds the following information • A brief description of the item • The number of units currently in inventory • The item’s retail price • For example

  36. A cash register is a sale of a retail item • Given the quantity of items being purchased, the cash register provides the sale’s subtotal, amount of sales tax, and total • The subtotal is the quantity multiplied by the price • The sales tax rate is 6% of a retail sale • The total is the subtotal plus the sales tax

  37. 5. The Parking Problem • This problem is about a police officer issuing a parking ticket • A parked car holds the following information • The car’s make (for example Toyota) • The model (for example, NissanZ28) • The color • The license number (for example XY-1234) • The number of minutes that the car has been parked • A parking meter records the number of minutes of parking time that has been purchased • A parking ticket is responsible for • Reporting the make, model, color, and license number of the illegally parked car • Reporting the amount of the fine, which is $25 for the first hour or part of an hour that the car is illegally parked, plus $10 for every additional hour or part of an hour that the car is illegally parked • Reporting the name and badge number of the police officer issuing the ticket • A police officer inspecting parked cars is responsible for • Telling his/her name and badge number • Examining the parked car and the parking meter, and determining whether the car’s time has expired • Issuing a parking ticket if the car’s time has expired

  38. References • Java How to Program, Seventh Edition, Chapter 8. Deitel & Deitel, 2007 • Starting Out with Java. From Control Structures Through Objects, Fourth Edition, Chapter 9. Tony Gaddis. Pearson Education International, 2010 • Core Java, Volume II – Advanced Features, Eighth Edition, Chapter 1. Cay S. Horstmann & Gary Cornell. Prentice Hall, 2008

More Related