A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing...
-
Upload
ariel-ferguson -
Category
Documents
-
view
212 -
download
0
Transcript of A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing...
![Page 1: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/1.jpg)
A Second Look at Classes and Objects
![Page 2: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/2.jpg)
Contents
A. Stock Purchase Problem
B. Employee Problem
C. Drawing Lines
D. Exercises
![Page 3: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/3.jpg)
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
![Page 4: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/4.jpg)
Solution
1. Design
2. Test Case
3. Developing Test Class
4. Fixing Test Class
![Page 5: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/5.jpg)
1. Design
StockPurchase- Stock- nShares
+ getCost()
Stock- tradingSymbol- sharePrice11
![Page 6: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/6.jpg)
2. Test Case
• Input:– Stock
• Trading symbol: XYZ• Share price: $9.62
– Number of shares: 100
• Output: $962.0
![Page 7: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/7.jpg)
3. Developing Test Class
![Page 8: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/8.jpg)
4. 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
![Page 9: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/9.jpg)
4.1. Creating Class Stock
![Page 10: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/10.jpg)
4.2. Creating Class StockPurchase
![Page 11: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/11.jpg)
4.4. Changing delta in Test Method
![Page 12: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/12.jpg)
B. Employee Problem
• Info of an employee includes: first name, last name, birthday, and hire day. Develop the Employee class
![Page 13: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/13.jpg)
Solution
1. Design
2. Test Cases
3. Developing Test Class
4. Fixing Test Class
![Page 14: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/14.jpg)
1. Design
Employee- String firstName- String lastName- Date birthDay- Date hireDay
![Page 15: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/15.jpg)
A Better Design
Employee- PersonalInfo- Date hireDay
PersonalInfo- String firstName- String lastName- Date birthDay11
![Page 16: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/16.jpg)
An Even Better Design
Employee- PersonalInfo- Date hireDay
PersonalInfo- Name name- Date birthDay11
Name- String firstName- String lastName11
![Page 17: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/17.jpg)
2. Test Cases
• Example 1– Personal Info
• Name– First name: Sue– Last name: Jones
• Birthday: Sept 5, 1986
– Hire day: Jan 1, 2007
• Example 2– Personal Info
• Name– First name: Bob– Last name: Blue
• Birthday: July 24, 1949
– Hire day: March 12, 1988
![Page 18: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/18.jpg)
3. Developing Test Class
![Page 19: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/19.jpg)
4. Fixing Test Class
• Develop PersonalInfo
Develop class Name
Handling Date
![Page 20: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/20.jpg)
![Page 21: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/21.jpg)
Handling Date
Develop static factory method makeDate()
![Page 22: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/22.jpg)
![Page 23: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/23.jpg)
Develop Class Employee
![Page 24: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/24.jpg)
C. Drawing Lines
1. Drawing Random Lines
2. Drawing a Flower
![Page 25: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/25.jpg)
1. Drawing Random Lines• Draw a random number of lines (at least 1
and at most 5) inside a frame
![Page 26: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/26.jpg)
![Page 27: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/27.jpg)
![Page 28: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/28.jpg)
2. Drawing a Flower
![Page 29: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/29.jpg)
![Page 30: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/30.jpg)
![Page 31: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/31.jpg)
D. Exercises
1. The Savings Account Problem
2. The Course Problem
3. The Sports Car Problem
4. The Retail Problem
5. The Parking Problem
![Page 32: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/32.jpg)
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%
![Page 33: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/33.jpg)
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
![Page 34: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/34.jpg)
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
![Page 35: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/35.jpg)
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
Description Units on Hand Price
Item #1 Jacket 12 59.95
Item #2 Designer Jeans 40 34.95
Item #3 Shirt 20 24.95
![Page 36: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/36.jpg)
• 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
![Page 37: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/37.jpg)
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, Nissan Z28)– 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
![Page 38: A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.](https://reader035.fdocuments.in/reader035/viewer/2022081603/56649ea95503460f94bad06a/html5/thumbnails/38.jpg)
References
1. Java How to Program, Seventh Edition, Chapter 8. Deitel & Deitel, 2007
2. Starting Out with Java, Chapter 9. Tony Gaddis and Godfrey Muganda, 2007
3. Core Java, Volume II – Advanced Features, Eighth Edition, Chapter 1. Cay S. Horstmann & Gary Cornell. Prentice Hall, 2008