310 likes | 395 Views
Problem Solving #1. ICS201-071. Outline. Review of Key Topics Example Program Problem 7.1 Problem Solving Tips. Review of Main Topics. Creating subclasses: “is-a” relationship why & how What is inherited Using this reference Using super reference Derived class constructors
E N D
Problem Solving #1 ICS201-071
Outline • Review of Key Topics • Example Program • Problem 7.1 • Problem Solving Tips
Review of Main Topics • Creating subclasses: • “is-a” relationship • why & how • What is inherited • Using this reference • Using superreference • Derived class constructors • Overriding methods • Overriding versus overloading • The final modifier • The access (visibility) modifiers: public, private, protected, and default • Class hierarchies & UML class diagrams
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.
Solution • class Payment • { • private double amount; • public Payment(){ { • amount = 0; • } • public Payment(double amount){ • this.amount = amount; • } • public void setPayment(double amount){ • this.amount = amount; • } • public double getPayment(){ • return amount; • } • public void paymentDetails(){ • System.out.println("The payment amount is " + amount); • } • }
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).
Solution • class CashPayment extends Payment{ • public CashPayment(){ • super(); • } • public CashPayment(double amt){ • super(amt); • } • public void paymentDetails(){ • System.out.println("The cash payment amount is " • + getPayment()); • } • }
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.
Solution • class CreditCardPayment extends Payment{ • private String name; • private String expiration; • private String creditcard; • public CreditCardPayment(){ • super(); • name = ""; • expiration = ""; • creditcard = ""; • } • public CreditCardPayment(double amt, String name, String expiration, String creditcard){ • super(amt); • this.name = name; • this.expiration = expiration; • this.creditcard = creditcard; • } • public void paymentDetails(){ • System.out.println("The credit card payment amount is " + getPayment()); • System.out.println("The name on the card is: " + name); • System.out.println("The expiration date is: " + expiration); • System.out.println("The credit card number is: " + creditcard); • } • }
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.
Solution • class Question1Payment{ • public static void main(String[] args) • { • CashPayment cash1 = new CashPayment(50.5), cash2 = new CashPayment(20.45); • CreditCardPayment credit1 = new CreditCardPayment(10.5, "Fred", "10/5/2010", "123456789"); • CreditCardPayment credit2 = new CreditCardPayment(100, "Barney", "11/15/2009", "987654321"); • System.out.println("Cash 1 details:"); • cash1.paymentDetails(); • System.out.println(); • System.out.println("Cash 2 details:"); • cash2.paymentDetails(); • System.out.println(); • System.out.println("Credit 1 details:"); • credit1.paymentDetails(); • System.out.println(); • System.out.println("Credit 2 details:"); • credit2.paymentDetails(); • System.out.println(); • } • }
Problem Solving • The purpose of writing a program is to solve a problem • For large problems, we need a systematic way for software development • 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 • 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
Design methodologies • 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 • 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
Object-Oriented Design • Discover classes • Determine responsibilities of each class • Describe relationships between the classes
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
Discovering Classes … • Example: Invoice
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 problemdescription 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?
Example • Brainstorming and filtering • Circling the nouns and underlining the verbs
Example … • First pass at a list of classes
Example … • Filtered list
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
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)
Relationships Between Classes • Inheritance (A is-a B) • Aggregation (A has-a B) • Dependency (A uses B)
StudentBody Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + main (args : String[]) : void + toString() : String Address - streetAddress : String - city : String - state : String - zipCode : long + toString() : String Relationships Between Classes: Example • Later we will explain more about aggregation and dependancy
Inheritance Relationship: Example 2 • Example 2
Method Design • 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 • For some methods, careful planning is needed to make sure they contribute to an efficient and elegant system design
Method Design … • An algorithm is a step-by-step process for solving a problem • Examples: a recipe, travel directions • 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
Method Design … • 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 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