Download presentation
Presentation is loading. Please wait.
1
Problem Solving #1 ICS201-071
2
2 Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem Solving Tips
3
3 Review of Main Topics Creating subclasses: Creating subclasses: –“is-a” relationship –why & how –What is inherited Using this reference Using this reference Using super reference Using super reference Derived class constructors Derived class constructors Overriding methods Overriding methods –Overriding versus overloading The final modifier The final modifier The access (visibility) modifiers: public, private, protected, and default The access (visibility) modifiers: public, private, protected, and default Class hierarchies & UML class diagrams Class hierarchies & UML class diagrams
4
Form Groups of 3 Students and Work on the Following Problem
5
5 Problem 7.1 a) Define a class named Payment that contains –a member variable of type double that stores the amount of the payment and appropriate accessor and mutator methods. –a method named paymentDetails that outputs an English sentence that describes the amount of the payment.
6
6 Solution 1. class Payment 2. { 3. private double amount; 4. public Payment(){{ 5. amount = 0; 6. } 7. public Payment(double amount){ 8. this.amount = amount; 9. } 10. public void setPayment(double amount){ 11. this.amount = amount; 12. } 13. public double getPayment(){ 14. return amount; 15. } 16. public void paymentDetails(){ 17. System.out.println("The payment amount is " + amount); 18. } 19. }
7
7 Problem 7.1 … b) Define a class named CashPayment that is derived from Payment. –This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor(s).
8
8 Solution 1. class CashPayment extends Payment{ 2. public CashPayment(){ 3. super(); 4. } 5. public CashPayment(double amt){ 6. super(amt); 7. } 8. public void paymentDetails(){ 9. System.out.println("The cash payment amount is " 10. + getPayment()); 11. } 12. }
9
9 Problem 7.1 … c) Define a class named CreditCardPayment that is derived from Payment. –This class should contain member variables for the name on the card, expiration date, and credit card number. Include appropriate constructor(s). –redefine the paymentDetails method to include all credit card information in the printout.
10
10 Solution 1. class CreditCardPayment extends Payment{ 2. private String name; 3. private String expiration; 4. private String creditcard; 5. public CreditCardPayment(){ 6. super(); 7. name = ""; 8. expiration = ""; 9. creditcard = ""; 10. } 11. public CreditCardPayment(double amt, String name, String expiration, String creditcard){ 12. super(amt); 13. this.name = name; 14. this.expiration = expiration; 15. this.creditcard = creditcard; 16. } 17. public void paymentDetails(){ 18. System.out.println("The credit card payment amount is " + getPayment()); 19. System.out.println("The name on the card is: " + name); 20. System.out.println("The expiration date is: " + expiration); 21. System.out.println("The credit card number is: " + creditcard); 22. } 23. }
11
11 Problem 7.1 … d) Define a test class having a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.
12
12 Solution 1. class Question1Payment{ 2. public static void main(String[] args) 3. { 4. CashPayment cash1 = new CashPayment(50.5), cash2 = new CashPayment(20.45); 5. CreditCardPayment credit1 = new CreditCardPayment(10.5, "Fred", "10/5/2010", "123456789"); 6. CreditCardPayment credit2 = new CreditCardPayment(100, "Barney", "11/15/2009", "987654321"); 7. System.out.println("Cash 1 details:"); 8. cash1.paymentDetails(); 9. System.out.println(); 10. System.out.println("Cash 2 details:"); 11. cash2.paymentDetails(); 12. System.out.println(); 13. System.out.println("Credit 1 details:"); 14. credit1.paymentDetails(); 15. System.out.println(); 16. System.out.println("Credit 2 details:"); 17. credit2.paymentDetails(); 18. System.out.println(); 19. } 20. }
13
13 Problem Solving The purpose of writing a program is to solve a problem The purpose of writing a program is to solve a problem For large problems, we need a systematic way for software development For large problems, we need a systematic way for software development Software development process Software development process –Problem solving phase –Implementation phase –Testing & debugging phase –Deployment & upgrading phase Problem solving should provide a solution without being distracted by programming language syntax Problem solving should provide a solution without being distracted by programming language syntax The general steps in problem solving are: The general steps in problem solving are: –Analyze and understand the problem –Design a solution to the problem –Analyze the complexity of the algorithm –Consider alternatives to the solution and refine it
14
14 Design methodologies Object oriented design (OOD) Object oriented design (OOD) –A technique used for developing software in which the solution is expressed in terms of objects (i.e. entities composed of data and operations on that data and interact by exchanging messages) Focus is on objects and their interaction Focus is on objects and their interaction Procedural design Procedural design –A technique for developing software in which the problem is divided into more easily subproblems, their solutions create a solution for the overall problem Focus is on procedures or functions Focus is on procedures or functions
15
15 Object-Oriented Design Discover classes Determine responsibilities of each class Describe relationships between the classes
16
16 Discovering Classes A class represents some useful concept Concrete entities: bank accounts, ellipses, and products Abstract concepts: streams and windows Find classes by looking for nouns in the task description Define the behavior for each class Find methods by looking for verbs in the task description
17
17 Discovering Classes … Example: Invoice Example: Invoice
18
18 Discovering Classes … Brainstorm: Simply keep a list of candidate classes Filter: Determine which are the useful classes in the problem and cross out others Keep the following points in mind: – –Class represents set of objects with the same behavior Entities with multiple occurrences in problem description are good candidates for objects Find out what they have in common Design classes to capture commonalities – –Not all classes can be discovered in analysis phase – –Represent some entities as objects, others as primitive types Should we make a class Address or use a String?
19
19 Example Brainstorming and filtering Brainstorming and filtering –Circling the nouns and underlining the verbs
20
Example … First pass at a list of classes First pass at a list of classes
21
Example … Filtered list Filtered list
22
22 Discovering Classes: Another Example Analyze classes Invoice Address LineItem // Records the product and the quantity Product Description // Field of the Product class Price // Field of the Product class Quantity // Not an attribute of a Product Total // Computed – not stored anywhere Amount Due // Computed – not stored anywhere Classes after a process of elimination Invoice Address LineItem Product Invoice Address LineItem Product Description Price Quantity Total Amount Due
23
23 CRC Cards Describes a class, its responsibilities, and its collaborators Use an index card for each class Pick the class that should be responsible for each method (verb) Write the responsibility onto the class card Indicate what other classes are needed to fulfill responsibility (collaborators)
24
24 CRC Cards
25
25 Relationships Between Classes Inheritance (A is-a B) Aggregation (A has-a B) Aggregation (A has-a B) Dependency (A uses B) Dependency (A uses B)
26
26 Relationships Between Classes: Example StudentBody + main (args : String[]) : void + toString() : String Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + toString() : String - streetAddress : String - city : String - state : String - zipCode : long Address Later we will explain more about aggregation and dependancy Later we will explain more about aggregation and dependancy
27
27 Inheritance Relationship: Example 1
28
28 Inheritance Relationship: Example 2 Example 2 Example 2
29
29 Method Design As we've discussed, high-level design issues include: As we've discussed, high-level design issues include: –identifying primary classes and objects –assigning primary responsibilities After establishing high-level design issues, its important to address low-level issues such as the design of key methods After establishing high-level design issues, its important to address low-level issues such as the design of key methods For some methods, careful planning is needed to make sure they contribute to an efficient and elegant system design For some methods, careful planning is needed to make sure they contribute to an efficient and elegant system design
30
30 Method Design … An algorithm is a step-by-step process for solving a problem An algorithm is a step-by-step process for solving a problem Examples: a recipe, travel directions Examples: a recipe, travel directions Every method implements an algorithm that determines how the method accomplishes its goals Every method implements an algorithm that determines how the method accomplishes its goals An algorithm may be expressed in pseudocode, a mixture of code statements and English that communicate the steps to take An algorithm may be expressed in pseudocode, a mixture of code statements and English that communicate the steps to take
31
31 Method Design … A method should be relatively small, so that it can be understood as a single entity A method should be relatively small, so that it can be understood as a single entity A potentially large method should be decomposed into several smaller methods as needed for clarity A potentially large method should be decomposed into several smaller methods as needed for clarity A public service method of an object may call one or more private support methods to help it accomplish its goal A public service method of an object may call one or more private support methods to help it accomplish its goal Support methods might call other support methods if appropriate Support methods might call other support methods if appropriate
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.