Download presentation
Presentation is loading. Please wait.
Published byDerrick Strickland Modified over 8 years ago
1
Chapter 8 Classes and Objects: A Deeper Look
2
The Time Object
3
Problem Description Time consists of hour (0 - 23), minute (0 - 59), and second (0 - 59). Develop the JTime class to represent time. JTime should have the following behaviors –Set a new time according to the new hour, minute, and second. Do not forget to validate these new values –Return a string to represent itself in the universal-time format: HH:MM:SS –Return a string to represent itself in the standard-time format: HH:MM:SS AM or PM
4
Questions 1.What is a valid hour? Minute? Second? 2.What is the universal-time format? 3.What is the standard-time format?
5
Answers 1.Valid hour: 0 - 23 Valid minute: 0 - 59 Valid second: 0 - 59 2.Universal-time format: HH:MM:SS 3.Standard-time format: H:MM:SS AM or PM –If hour is 0 or 12, H will be 12. Otherwise, H will be (hour % 12) –If hour less than 12, it’s AM. Otherwise, it’s PM
6
Class Diagram
7
Developing the Constructor Without Input Data Validation
10
Adding Input Data Validation to the Constructor
13
Setting New Time
16
Getting the Universal-Time Format
19
Getting the Standard-Time Format
22
Exercise Compare our solution with the Deitel’s solution (in the next slides)
25
The Date Object
26
Problem Description Date consists of month (1 - 12), day (1 - 31), and year. Develop the JDate class and its constructor to represent a date. Do not forget to check the validity of input data Develop the toString() method for the JDate class according to the MM/DD/YYYY format
27
Question Q: How can we know that a year is leap year? A: A year is leap year if 1)It is divisible by 400, OR 2)It is divisible by 4 but not divisible by 100
28
Class Diagram To be filled
29
Step 1. Developing Constructor Without Input Validation
32
Validating the Month
33
Validating the Day
34
The Employee Object
35
Problem Description Info of an employee includes: First name, last name, birth date, and hire date. Develop the Employee class
36
Example First name: Sue Last name: Jones Birthday: Sept 5, 1986 Hire date: Jan 1, 2007
37
Class Diagram
41
A Better Design
42
An Even Better Design
46
Drawing Random Lines
47
Problem Draw a random number of lines (at least 1 and at most 5) inside a frame
50
Exercise Exercise 8.6
51
The Stock Problem A company’s stock consists of –The trading symbol. A short series of characters that are used to identify the stock on the stock exchange –The current price per share of the stock A stock purchase has the following information –The stock that was purchased –The number of shares Calculate the cost of the purchase
52
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
53
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
54
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 DescriptionUnits on HandPrice Item #1Jacket1259.95 Item #2Designer Jeans4034.95 Item #3Shirt2024.95
55
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
56
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.