310 likes | 424 Views
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
E N D
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 int area = Rectangle.area(8 , 3); Delegating to an Object Rectangle rectangle = new Rectangle(8,3); int area = rectangle.getArea(); int width = 8; int height = 3; int area = width * height;
Client View private attribute public = field feature public method private • API- Application Programming Interface Encapsulation, information hiding A component can be improved without impact on client
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
Exercises 2.4 • Do you see any compile-time errors? • double amount = 2500; • Int period = 4; • double pay = Orbit.payBack(amount,period); The following fragment uses a method in a class whose UML diagram is shown below.
Exercises 2.5 • Do you see any compile-time errors? • int amount = 2500; • Int period = 4; • double pay = Orbit.payBack(amount,period); The following fragment uses a method in a class whose UML diagram is shown below.
Exercises 2.6 • Do you see any compile-time errors? • float amount = 2500; • Int period = 4; • double pay = Orbit.payBack(amount,period); The following fragment uses a method in a class whose UML diagram is shown below.
Exercises 2.7 • Do you see any compile-time errors? • double amount = 2500; • long period = 4; • double pay = Orbit.payback(amout,period); The following fragment uses a method in a class whose UML diagram is shown below.
Exercises 2.8 • Do you see any compile-time errors? • double amount = 2500; • int period = 4; • Int pay = Orbit.payback(amout,period); The following fragment uses a method in a class whose UML diagram is shown below.
Exercises 2.9 • Do you see any compile-time errors? • Bond.rating = ‘C’; • Bond.rate = 0.12; • double x = Bound.estimate(); • output.println (Bound.inflate()); The following fragment uses a method in a class whose UML diagram is shown below.
Exercises 2.10 • Do you see any compile-time errors? • Bond.rating = ‘C’; • Bond.rate = 0.12; • Bound.estimate(); • Bound.inflate(); The following fragment uses a method in a class whose UML diagram is shown below.
Summary • Delegation • Complexity Reduction • Static Method • Object Method • Types and Casting
Method • Signature • name together with the types of parameters sin(double) getArea() • Return • Type such as void, double, int etc • Visibility • Private/Public
UML Unified Modelling Language
Utility Class Attributes Methods Utility Class all method are static Utility Class may contains constant UML
Objects • Is a software entity that is capable of storing data as well as performing computations • Object has • Attributes • Methods • State • Reference
Object Class reference Instantiation • Features • Methods • Attributes Rectangle rectangle = new Rectangle(4,5)
Class Attributes Methods • Attributes • Field • Private • Constructors • Methods
API Complexity Accountability Substitutability
Software Engineering • Patterns • Risk Mitigation by Early Exposure • Handling Constants • Contracts • Precondition • Postcondition
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; .... } }
I/O • Input from keyboard input.nextInt() • Output on screen output.println()
Errors • Compilation Errors • Post-Compilation Errors • Run-time Errors • Logical Errors
Post-Compilation Errors • Understand root of problem • Input • Location • Modify application • Verify solution • Re-test application
Debugging Techniques Add print statements Comment code
Exercises 2.12 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. The following fragment computes the average of three variables:
Exercises 2.13 • 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; • 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
Exercises 2.14 • 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; Rewrite the following fragment so that it is free of magic numbers: