Download presentation
Presentation is loading. Please wait.
Published byAgnes Shepherd Modified over 6 years ago
1
Phil Tayco Slide version 1.0 Created Nov. 26, 2017
More Exercises Phil Tayco Slide version 1.0 Created Nov. 26, 2017
2
More Practice Problem statement: Design a class infrastructure that manages payroll for a department as follows: All departments have at most 10 employees Employees are either paid hourly or weekly All employees have a numeric id and a name Hourly employees have an hourly pay rate Salary employees have a weekly pay rate Demonstrate this program by showing different employees added to the department and reporting the following: Total monthly pay for all department employees Who is paid the highest and lowest
3
More Practice A prime benefit of OO design is the infrastructure giving you power to handle change New problem requirements: Years of service for every employee Create a commission based employee who gets a percentage of total sales for pay Given multiple departments, how do we find out which department gets paid more? How do we find out which employee in all departments is the highest paid?
4
Observations To be discussed
5
Static and Final Classes may contain properties and methods that may not require an object to be created for it to be used public double calculateArea() { return Math.PI * radius * radius; } “Math.PI” is a property in the Math class and is static When a method or property is public and static, it means you can use it without having the need to create an object In this example, we don’t have to create a Math object to use PI. We are simply accessing the definition of it made public to us through the Math class This makes sense because there really isn’t a need to use memory for concepts that are fundamentally fixed (their definition is “static”)
6
Static and Final Notice PI is also in all caps. This is the standard Java notation to define a constant Constants in classes are often static and made public to show certain fixed information associated with the concept of the class In our example, we can design that all departments have a fixed maximum number of employees: public class Department { public final static MAX_EMPLOYEES = 10; } This can be used by code in the Department class as well as outside it as needed Constants help make code readable and when used properly, can be adjusted to maximum code change efficiency (i.e. changing the max in a department to 50)
7
Interfaces Java also supports a special type of abstract class called an interface Interfaces are restricted to defining only abstract methods and can be thought of as a form of a superclass public interface Payable { public double calculateMonthlyPay(); } Any subclass can inherit from this through coding that the class will “implement” the interface: public class Employee implements Payable This gets the same treatment as a subclass inheriting from an abstract class like “Payable” in that it must now define “calculateMonthlyPay” (or else Employee must be abstract)
8
Interfaces Interfaces only define method signatures and is a form of an abstract class There are two differences with abstract classes and interfaces: Abstract classes can still contain properties and methods that can be inherited. Interfaces only define abstract methods Subclasses in Java can only “extends” one superclass. Subclasses that “implements” interfaces can implement multiple interfaces More examples can and should be practiced from these concepts
9
Another Example Design and implement a prototype that manages sales of computer products for employees Sales employees are identified by a name and number A record of up to 1000 sales transactions is maintained for each sales employee Sales transactions are either standard or bulk Standard transactions are identified by a number, a product sold and the quantity sold for that product Bulk transactions are also identified by a number and up to 100 products for the sale Bulk transactions also can have a discount amount for the sale
10
Another Example Computer products are either hardware or software
Hardware products are identified by name, price and manufacturing company Software products are identified by name, price, developer company and software version number The management system allows for creation of up 5 sales employees, recording sales and reporting employee performance When a sale is recorded, the sales employee is identified for the sale and the type of transaction Employee performance shows each sales transactions and the total revenue of them all
11
Observations The main program is complex and is separate from the class design allowing it to be developed in different ways (command line, windows or web application) Composition and Polymorphism: Sales Employee containing an array of Sales Transactions and Transaction types containing use of Products Inheritance: Software and Hardware products inheriting basic information from Product. Standard and Bulk transactions inherit from Sales Transaction Abstract classes: Sales Transaction is abstract requiring calculating sale in the subclasses
12
Programming Project Create a task management planner program
Tasks have a description and are either time constrained or either dependent or independent of other tasks Time constrained tasks have a start and end date and time. Time constrained tasks cannot overlap (If one time constrained task is for 2pm-4pm on a given day, a second time constrained task such as from 3pm-5pm on the same day would not be allowed) Once the end date and time have passed for a time constrained task, the task is considered to be complete Time constrained tasks can also be marked as complete before the end date and time is reached
13
Programming Project Create a task management planner program
Dependent and independent tasks are not time constrained and only have a start date (no start time needed) Independent tasks are considered started once the start date is reached Dependent tasks have another task (which can be dependent or independent in itself) that it depends on to be completed as well as its own start date Dependent tasks can only start if the start date is reached and the task it is dependent on is considered complete Once any of these tasks are started, they are considered finished when the task is manually marked as complete
14
Programming Project To simulate and test the task management planner, create a calendar where you can manually change the current date and time As the current date and time is updated in the calendar, all task statuses are updated as needed The main planning program shall allow for the following features: Create a new task Change the current date and time Mark a task as complete Report tasks Features 1-3 should obtain the necessary information to perform the function with appropriate error handling Reporting tasks should at least include listing all tasks, which ones have not started, started and complete
15
Programming Project Data for tasks should be persistent and should be saved/loaded to a text file or database The requirements list is somewhat open-ended by design. You are free to design your classes and implement your main program as you like. Any questions should be asked sooner rather than later Programming requirements are to demonstrate all concepts discussed in class: Class design, use of composition, inheritance, polymorphism, error handling and data saving Program can and encouraged to be developed in teams of at most, 2 people. Single individual development is acceptable, but requirements list does not change if you’re doing it by yourself Program is due for demonstration on the last day of class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.