cse 1020:programming by delegation

31
CSE 1020:Programming by Delegation Mark Shtern 1-1

Upload: raine

Post on 06-Jan-2016

38 views

Category:

Documents


4 download

DESCRIPTION

CSE 1020:Programming by Delegation. Mark Shtern. Summary. Java VM Edit-Compile-Run cycle Edit Create or edit Save Compile Run Lunch main class Interact with user. Summary. Math & Computer Memory and Declaration. Delegation. Delegating to a static Method - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 1020:Programming by Delegation

CSE 1020:Programming by DelegationMark Shtern

1-1

Page 2: CSE 1020:Programming by Delegation

Summary

• Java VM• Edit-Compile-Run cycle

– Edit• Create or edit• Save

– Compile– Run

• Lunch main class• Interact with user

1-2

Page 3: CSE 1020:Programming by Delegation

Summary

• Math & Computer• Memory and Declaration

1-3

Page 4: CSE 1020:Programming by Delegation

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();

Page 5: CSE 1020:Programming by Delegation

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

Page 6: CSE 1020:Programming by Delegation

Contracts

• Precondition - the client’s responsibility• Postcondition – the implementer’s

responsibility

Page 7: CSE 1020:Programming by Delegation

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

Page 8: CSE 1020:Programming by Delegation

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);

Page 9: CSE 1020:Programming by Delegation

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);

Page 10: CSE 1020:Programming by Delegation

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);

Page 11: CSE 1020:Programming by Delegation

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);

Page 12: CSE 1020:Programming by Delegation

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);

Page 13: CSE 1020:Programming by Delegation

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());

Page 14: CSE 1020:Programming by Delegation

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();

Page 15: CSE 1020:Programming by Delegation

Summary

• Delegation– Complexity Reduction– Static Method– Object Method

• Types and Casting

1-15

Page 16: CSE 1020:Programming by Delegation

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

Page 17: CSE 1020:Programming by Delegation

UML

• Unified Modelling Language

1-17

Page 18: CSE 1020:Programming by Delegation

Utility Class

• Utility Class all method are static• Utility Class may contains constant• UML

1-18

Attributes

Methods

Page 19: CSE 1020:Programming by Delegation

Objects

• Is a software entity that is capable of storing data as well as performing computations

• Object has– Attributes– Methods– State– Reference

1-19

Page 20: CSE 1020:Programming by Delegation

Object

• Features– Methods– Attributes

Rectangle rectangle = new Rectangle(4,5)

1-20

Class reference

Instantiation

Page 21: CSE 1020:Programming by Delegation

Class

• Attributes– Field– Private

• Constructors• Methods

1-21

Attributes

Methods

Page 22: CSE 1020:Programming by Delegation

API

• Complexity• Accountability• Substitutability

1-22

Page 23: CSE 1020:Programming by Delegation

Software Engineering

• Patterns• Risk Mitigation by Early Exposure• Handling Constants• Contracts

– Precondition– Postcondition

1-23

Page 24: CSE 1020:Programming by Delegation

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

Page 25: CSE 1020:Programming by Delegation

I/O

• Input from keyboard input.nextInt()

• Output on screenoutput.println()

1-25

Page 26: CSE 1020:Programming by Delegation

Errors

• Compilation Errors• Post-Compilation Errors

– Run-time Errors– Logical Errors

1-26

Page 27: CSE 1020:Programming by Delegation

Post-Compilation Errors

• Understand root of problem– Input– Location

• Modify application• Verify solution• Re-test application

1-27

Page 28: CSE 1020:Programming by Delegation

Debugging Techniques

• Add print statements• Comment code

1-28

Page 29: CSE 1020:Programming by Delegation

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.

Page 30: CSE 1020:Programming by Delegation

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;

Page 31: CSE 1020:Programming by Delegation

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;