c omp 401 o bjects instructor: prasun dewan 2 c omputer vs. p rogram m odel processor compiler...

37
COMP 401 OBJECTS Instructor: Prasun Dewan

Post on 19-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • Slide 2
  • C OMP 401 O BJECTS Instructor: Prasun Dewan
  • Slide 3
  • 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
  • Slide 4
  • 3 S TRUCTURING IN S CRIPTS Script Performer Theater Follows
  • Slide 5
  • 4 S TRUCTURING IN S CRIPTS Script Introduction Body Conclusion Paragraph 1 Paragraph 2 Sentence 1Sentence 2 Script components are abstract. So are program components!
  • Slide 6
  • 5 P ROGRAM C OMPONENTS ~ P HYSICAL O BJECTS Natural Objects Manufactured Objects ~ Program Components
  • Slide 7
  • 6 P ROGRAM O BJECTS ~ M ANUFACTURED O BJECTS manufactured by perform Operations accelerate brake Class Program Object instance of Methods add subtract execute invoke call
  • Slide 8
  • 7 C LASSIFICATION T HROUGH F ACTORIES manufactured by manufactured by
  • Slide 9
  • 8 C LASSIFICATION T HROUGH F ACTORIES ASquareCalculator ABMICalculator ASquareCalculator Instance ABMICalculator Instance instance of
  • Slide 10
  • 9 A S IMPLE I NSTANTIATED C LASS public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Object Creation Object Use No static because class will be instantiated No package
  • Slide 11
  • 10 P ACKAGES package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import math.ASquareCalculator; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Class in different package must be imported using full name of class ( a la full file name)
  • Slide 12
  • 11 P ACKAGES package math; public class ASquareCalculator { public int square( int x) { return x*x; } package math; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Class in same package need not be imported
  • Slide 13
  • 12 P ACKAGES package math; public class ASquareCalculator { public int square( int x) { return x*x; } import math.ASquareCalculator; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } No package means package named default, hence import needed
  • Slide 14
  • 13 P ACKAGES public class ASquareCalculator { public int square( int x) { return x*x; } package main ; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Short name of class in default package same as its full name
  • Slide 15
  • 14 P ACKAGES public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } No package means package named default, hence no import needed here
  • Slide 16
  • 15 L ONG NAME WITH NO IMPORT package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new math.ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Can use the full name of class directly
  • Slide 17
  • 16 package safemath; public class ASquareCalculator { public long square( int x) { return x*x; } W HY IMPORTS / FULL NAME ? package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import math.ASquareCalculator; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Twice the size of ints Disambiguates
  • Slide 18
  • 17 package safemath; public class ASquareCalculator { public long square( int x) { return x*x; } A MGIGOUS I MPORT package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import math.ASquareCalculator; import safemath.ASquareCalculator; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Ambiguous
  • Slide 19
  • 18 W HY P ACKAGES ? 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 package math.power; public class ACubeCalculator {}
  • Slide 20
  • 19 B ROWSING J AVA C LASSES Very useful package
  • Slide 21
  • 20 L ANGUAGE VS. L IBRARY Built-in classes
  • Slide 22
  • 21 C HANGING P ARAMETER public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Calculates 5*5
  • Slide 23
  • 22 C HANGING P ARAMETER public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(341)); } Must change code
  • Slide 24
  • 23 R ERUN P ROGRAM public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(Ineteger.parseInt(args[0])); } Must re-run program How to not re- run program without writing tedious UI code?
  • Slide 25
  • 24 O BJECT E DITOR package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import math.ASquareCalculator; import bus.uigen.ObjectEditor; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); ObjectEditor.edit(squareCalculator ); } ObjectEditor is predefined packaged class
  • Slide 26
  • 25 E DITING AS QUARE C ALCULATOR I NSTANCE
  • Slide 27
  • 26 I NVOKING A M ETHOD A ND V IEWING THE R ESULT
  • Slide 28
  • 27 A NOTHER S IMPLE C LASS : ABMIC ALCULATOR ASquareCalculatorABMICalculator Specification: Given an integer x, calculate the square of x. package math ; public class ASquareCalculator { public int square( int x) { return x*x; } Specification: Given the weight (kg) and height (m) of a person, calculate the persons body mass index a.k.a. BMI. ?
  • Slide 29
  • 28 ABMIC ALCULATOR package bmi ; public class ABMICalculator { public int calculateBMI( 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?
  • Slide 30
  • 29 ABMIC ALCULATOR package bmi ; public class ABMICalculator { public int calculateBMI( int weight, int height) { return weight/(height*height); } double Doubles are decimal/real numbers
  • Slide 31
  • 30 ABMIC ALCULATOR package bmi ; public class ABMICalculator { public double calculateBMI( double weight, double height) { return weight/(height*height); }
  • Slide 32
  • 31 F ORMAL VS. A CTUAL P ARAMETERS package bmi ; public class ABMICalculator { public double calculateBMI( double weight, double height) { return weight/(height*height); } weight 0 0 height 0 0 variablesmemory Parameters Invoke calculateBMI Actual Parameters Formal Parameters 74.98 1.94 assigned
  • Slide 33
  • 32 P ROGRAMMED C ALL public class ABMICalculator { public double calculateBMI( double weight, double height) { return weight/(height*height); } public class BMICalculatorTester { public static void main (String[] args) { ABMICalculator bmiCalculator = new ABMICalculator(); System.out.println (bmiCalculator.calculateBMI(75, 1.77)); } Formal Parameters Actual Parameters
  • Slide 34
  • 33 P ROGRAMMED VS. INTERACTIVE CALL System.out.println(setWeight Called); Target ObjectMethod NameActual Parameters Programmed Call Interactive Call
  • Slide 35
  • 34 I NTERNAL VS. E XTERNAL M ETHOD C ALLS public class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) { return ( new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); } public double toMetres( double heightInInches) { } public double toKgs( double weightInLbs) { } } APoundInchBMICalculator External Caller and callee methods are in different objects Must specify target object Internal Caller and callee methods are in the same object Target object is implicit ( this ) Actual parameters:
  • Slide 36
  • 35 E RRORS package bmi ; class ABMICalculator { double calculateBMI( double weight, double height) { return (height*heigh)/weight } Syntax ErrorLogic Error Semantics Error Access Error
  • Slide 37
  • 36 E RRORS package bmi ; class ABMICalculator { double calculateBMI( double weight, double height) { return (height*heigh)/weight } Syntax ErrorLogic Error Semantics Error Access Error
  • Slide 38
  • 37 M ETHOD A CCESS E RROR You instantiate a ABMICalculator object but there is no ABMICalculator menu item Reason You have not defined any public methods in ABMICalculator