1 / 31

CSE 1020:Programming by Delegation

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

raine
Télécharger la présentation

CSE 1020:Programming by Delegation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1020:Programming by Delegation Mark Shtern

  2. Summary • Java VM • Edit-Compile-Run cycle • Edit • 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 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;

  5. 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

  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 • 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. Summary • Delegation • Complexity Reduction • Static Method • Object Method • Types and Casting

  16. Method • Signature • 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 Attributes Methods Utility Class all method are static Utility Class may contains constant UML

  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 Class reference Instantiation • Features • Methods • Attributes Rectangle rectangle = new Rectangle(4,5)

  21. Class Attributes Methods • Attributes • Field • Private • Constructors • 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 input.nextInt() • Output on screen 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 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:

  30. 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

  31. 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:

More Related