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