1 / 37

Comp 401 Objects

Comp 401 Objects. Instructor: Prasun Dewan. Computer vs. Program Model. Processor. ?. Program (source code). Compiler. Theater. Performer. Structuring in Scripts. Script. Follows. Structuring in Scripts. Script. Introduction. Body. Conclusion. Script components are abstract.

louis
Download Presentation

Comp 401 Objects

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. Comp 401Objects Instructor: PrasunDewan

  2. Computer vs. Program Model Processor ? Program (source code) Compiler

  3. Theater Performer Structuring in Scripts Script Follows

  4. Structuring in Scripts Script Introduction Body Conclusion Script components are abstract. Paragraph 1 Paragraph 2 So are program components! Sentence 1 Sentence 2

  5. Program Components ~ Physical Objects Natural Objects Manufactured Objects ~ Program Components

  6. Program Objects ~ Manufactured Objects Operations manufactured by accelerate perform brake Methods Class Program Object instance of execute invoke call add Program Object subtract

  7. Classification Through Factories manufactured by manufactured by

  8. Classification Through Factories ASquareCalculator ASquareCalculator Instance instance of ASquareCalculator Instance ABMICalculator ABMICalculator Instance instance of ABMICalculator Instance

  9. A Simple Instantiated Class No package No static because class will be instantiated • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } Object Use Object Creation publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  10. Packages Class in different package must be imported using full name of class ( a la full file name) • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } package main; import math.ASquareCalculator; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  11. Packages Class in same package need not be imported • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } package math; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  12. Packages No package means package named default, hence import needed • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } import math.ASquareCalculator; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  13. Packages Short name of class in default package same as its full name • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } package main; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  14. Packages No package means package named default, hence no import needed here • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  15. Long name with no import Can use the full name of class directly • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } package main; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newmath.ASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  16. Why imports/full name? • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } • package safemath; • publicclassASquareCalculator • { • publiclong square(int x) • { • return x*x; • } • } Twice the size of ints package main; import math.ASquareCalculator; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } } Disambiguates

  17. Amgigous Import • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } • package safemath; • publicclassASquareCalculator • { • publiclong square(int x) • { • return x*x; • } • } package main; import math.ASquareCalculator; import safemath.ASquareCalculator; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } } Ambiguous

  18. Why Packages? • Can create competing implementations of same class. • A la creating files Test.java in different assignment directories/folders • Can browse related classes • A la browsing through all files in an assignment directory/folder. • Like directories/folders packages can be hierarchical packagemath.power; public class ACubeCalculator {…}

  19. Browsing Java Classes Very useful package

  20. Language vs. Library Built-in classes

  21. Changing Parameter Calculates 5*5 • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(5)); } }

  22. Changing Parameter Must change code • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(341)); } }

  23. Rerun Program Must re-run program • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } How to not re-run program without writing tedious UI code? publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); System.out.println (squareCalculator.square(Ineteger.parseInt(args[0])); } }

  24. ObjectEditor ObjectEditor is predefined packaged class • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } package main; import math.ASquareCalculator; importbus.uigen.ObjectEditor; publicclassSquareCalculatorTester { publicstaticvoid main (String[] args) { ASquareCalculatorsquareCalculator = newASquareCalculator(); ObjectEditor.edit(squareCalculator ); } }

  25. Editing ASquareCalculator Instance

  26. Invoking a Method And Viewing the Result

  27. Another Simple Class: ABMICalculator ASquareCalculator ABMICalculator Specification: Given an integer x, calculate the square of x. Specification: Given the weight (kg) and height (m) of a person, calculate the person’s body mass index a.k.a. BMI. • package math; • publicclassASquareCalculator • { • publicint square(int x) • { • return x*x; • } • } ?

  28. ABMICalculator • package bmi; • publicclassABMICalculator • { • publicintcalculateBMI(int weight, int height) • { • return weight/(height*height); • } • } Parameter and return types are integers But height (m) and weight (kg) are expressed as decimals How do we solve the discrepancy?

  29. ABMICalculator double • package bmi; • publicclassABMICalculator • { • publicintcalculateBMI(int weight, int height) • { • return weight/(height*height); • } • } Doubles are decimal/real numbers

  30. ABMICalculator • package bmi; • publicclassABMICalculator • { • publicdouble calculateBMI(double weight, double height) • { • return weight/(height*height); • } • }

  31. variables memory weight 0 height 0 Formal vs. Actual Parameters • package bmi; • publicclassABMICalculator • { • publicdouble calculateBMI(double weight, double height) • { • return weight/(height*height); • } • } Parameters Formal Parameters Invoke calculateBMI assigned 74.98 1.94 Parameters Actual Parameters

  32. Programmed Call • publicclassABMICalculator • { • publicdouble calculateBMI(double weight, double height) • { • return weight/(height*height); • } • } Formal Parameters publicclassBMICalculatorTester { publicstaticvoid main (String[] args) { ABMICalculatorbmiCalculator = newABMICalculator(); System.out.println (bmiCalculator.calculateBMI(75, 1.77)); } } Actual Parameters

  33. Programmed vs. interactive call System.out.println(“setWeight Called”); Programmed Call Target Object Method Name Actual Parameters Interactive Call

  34. Internal vs. External Method Calls APoundInchBMICalculator publicclassAPoundInchBMICalculator { publicdoublecalculateBMI( doubleweightInLbs, doubleheightInInches) { return (newABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); } publicdoubletoMetres(doubleheightInInches) { … } publicdoubletoKgs(doubleweightInLbs) { … } } Actual parameters: <expression> External Internal Caller and callee methods are in different objects Caller and callee methods are in the same object Must specify target object Target object is implicit (this)

  35. Errors • package bmi; • classABMICalculator • { • double calculateBMI(double weight, double height) • { • return (height*heigh)/weight • } Logic Error Syntax Error Access Error Semantics Error

  36. Errors • package bmi; • classABMICalculator • { • double calculateBMI(double weight, double height) • { • return (height*heigh)/weight • } Logic Error Syntax Error Access Error Semantics Error

  37. Method Access Error • You instantiate a ABMICalculator object • but there is no ABMICalculator menu item • Reason • You have not defined any public methods in ABMICalculator

More Related