CSE 1020:Programming by Delegation
description
Transcript of CSE 1020:Programming by Delegation
CSE 1020:Programming by DelegationMark Shtern
1-1
Summary
• Java VM• Edit-Compile-Run cycle
– Edit• Create or edit• Save
– Compile– Run
• Lunch main class• Interact with user
1-2
Summary
• Math & Computer• Memory and Declaration
1-3
Delegation
int width = 8;int height = 3;int area = width * height;
1-4
Delegating to a static Method int area = Rectangle.area(8 , 3);
Delegating to an ObjectRectangle rectangle = new Rectangle(8,3);
int area = rectangle.getArea();
Client View• Encapsulation, information hiding • A component can be improved without impact
on client
1-5
feature
attribute
method
private
public = field
public
private
• API- Application Programming Interface
Contracts
• Precondition - the client’s responsibility• Postcondition – the implementer’s
responsibility
Example 01
• Write a program that outputs the return of the getTime and toString methods of Date class whose UML diagram is shown below
1-7
Exercises 2.4
• The following fragment uses a method in a class whose UML diagram is shown below.
1-8
• Do you see any compile-time errors?
double amount = 2500;Int period = 4;double pay =
Orbit.payBack(amount,period);
Exercises 2.5
• The following fragment uses a method in a class whose UML diagram is shown below.
1-9
• Do you see any compile-time errors?
int amount = 2500;Int period = 4;double pay =
Orbit.payBack(amount,period);
Exercises 2.6
• The following fragment uses a method in a class whose UML diagram is shown below.
1-10
• Do you see any compile-time errors?
float amount = 2500;Int period = 4;double pay =
Orbit.payBack(amount,period);
Exercises 2.7
• The following fragment uses a method in a class whose UML diagram is shown below.
1-11
• Do you see any compile-time errors?
double amount = 2500;long period = 4;double pay =
Orbit.payback(amout,period);
Exercises 2.8
• The following fragment uses a method in a class whose UML diagram is shown below.
1-12
• Do you see any compile-time errors?
double amount = 2500;int period = 4;Int pay =
Orbit.payback(amout,period);
Exercises 2.9
• The following fragment uses a method in a class whose UML diagram is shown below.
1-13
• Do you see any compile-time errors?
Bond.rating = ‘C’;Bond.rate = 0.12;
double x = Bound.estimate();output.println (Bound.inflate());
Exercises 2.10
• The following fragment uses a method in a class whose UML diagram is shown below.
1-14
• Do you see any compile-time errors?
Bond.rating = ‘C’;Bond.rate = 0.12;
Bound.estimate();Bound.inflate();
Summary
• Delegation– Complexity Reduction– Static Method– Object Method
• Types and Casting
1-15
Method
• Signature– name together with the types of parameters sin(double) getArea()
• Return– Type such as void, double, int etc
• Visibility– Private/Public
1-16
UML
• Unified Modelling Language
1-17
Utility Class
• Utility Class all method are static• Utility Class may contains constant• UML
1-18
Attributes
Methods
Objects
• Is a software entity that is capable of storing data as well as performing computations
• Object has– Attributes– Methods– State– Reference
1-19
Object
• Features– Methods– Attributes
Rectangle rectangle = new Rectangle(4,5)
1-20
Class reference
Instantiation
Class
• Attributes– Field– Private
• Constructors• Methods
1-21
Attributes
Methods
API
• Complexity• Accountability• Substitutability
1-22
Software Engineering
• Patterns• Risk Mitigation by Early Exposure• Handling Constants• Contracts
– Precondition– Postcondition
1-23
1020 TempletImport java.util.ScannerImport java.io.PrintStream
public class Templet{ public static void main(String[] args) { Scanner input = new Scanner (System.in); PrintStream output = System.out; .... } }
1-24
I/O
• Input from keyboard input.nextInt()
• Output on screenoutput.println()
1-25
Errors
• Compilation Errors• Post-Compilation Errors
– Run-time Errors– Logical Errors
1-26
Post-Compilation Errors
• Understand root of problem– Input– Location
• Modify application• Verify solution• Re-test application
1-27
Debugging Techniques
• Add print statements• Comment code
1-28
Exercises 2.12
• The following fragment computes the average of three variables:
1-29
int x = 6/2;int y = 12 / (x - 3);int z = - 3;double mean = x + y + z )/3;
• It contains a compile-time, a runtime error and a logical error. Identify each.
Exercises 2.13
• A Develop decided to declare 5/9 literal as a final and gave it the identifier SCALE_FACTOR
• Which of the following declaration does not lead to a logical error
1-30
a. final double SCALE_FACTOR = 5 / 9;b. final double SCALE_FACTOR = 5.0 / 9.0;
c. final double SCALE_FACTOR = 5 / 9.0;
d. final double SCALE_FACTOR = (double)(5 / 9);
e. final double SCALE_FACTOR = (double) 5/ 9;
f. final double SCALE_FACTOR = 5/(double)9;
Exercises 2.14
• Rewrite the following fragment so that it is free of magic numbers:
1-31
output.println (“Enter speed in km/h”);
double speed = input.nextDouble();
speed = speed * 1000 / 3600;
output.println (“Enter speed in ft”);
double distance = distance * 0.3048;
double time = distance/speed;