Problem Solving Session 1

Download Report

Transcript Problem Solving Session 1

Problem Solving #1
ICS201-071
Outline


Review of Key Topics
Example Program
– Problem 7.1

Problem Solving Tips
2
Review of Main Topics

Creating subclasses:
– “is-a” relationship
– why & how
– What is inherited




Using this reference
Using super reference
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
3
Form Groups of 3 Students and
Work on the Following Problem
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.
5
Solution
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
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);
}
}
6
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).
7
Solution
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
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());
}
}
8
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.
9
Solution
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
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);
10
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.
11
Solution
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
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();
}
}
12
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
–
–
–
–
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
Problem solving should provide a solution without being
distracted by programming language syntax
The general steps in problem solving are:
13
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
14
Object-Oriented Design



Discover classes
Determine responsibilities of each
class
Describe relationships between the
classes
15
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
16
Discovering Classes …

Example:
Invoice
17
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

Should we make a class Address or use a String?


– Not all classes can be discovered in analysis phase
– Represent some entities as objects, others as primitive
types
18
Example

Brainstorming and filtering
– Circling the nouns and underlining the verbs
19
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
Description
Price
Quantity
Total
Amount Due
Invoice
Address
LineItem
Product
22
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)
23
CRC Cards
24
Relationships Between
Classes



Inheritance (A is-a B)
Aggregation (A has-a B)
Dependency (A uses B)
25
Relationships Between
Classes: Example
StudentBody
+ main (args : String[]) : void
Student
- firstName : String
- lastName : String
- homeAddress : Address
- schoolAddress : Address
+ toString() : String
Address
- streetAddress : String
- city : String
- state : String
- zipCode : long
+ toString() : String

Later we will explain more about aggregation and dependancy
26
Inheritance Relationship:
Example 1
27
Inheritance Relationship:
Example 2

Example 2
28
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
29
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
30
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
31