Download presentation
Presentation is loading. Please wait.
2
C OMP 401 O BJECTS Instructor: Prasun Dewan
3
2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
4
3 S TRUCTURING IN S CRIPTS Script Performer Theater Follows
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!
6
5 P ROGRAM C OMPONENTS ~ P HYSICAL O BJECTS Natural Objects Manufactured Objects ~ Program Components
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
8
7 C LASSIFICATION T HROUGH F ACTORIES manufactured by manufactured by
9
8 C LASSIFICATION T HROUGH F ACTORIES ASquareCalculator ABMICalculator ASquareCalculator Instance ABMICalculator Instance instance of
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
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)
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
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
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
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
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
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
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
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 {…}
20
19 B ROWSING J AVA C LASSES Very useful package
21
20 L ANGUAGE VS. L IBRARY Built-in classes
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
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
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?
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
26
25 E DITING AS QUARE C ALCULATOR I NSTANCE
27
26 I NVOKING A M ETHOD A ND V IEWING THE R ESULT
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 person’s body mass index a.k.a. BMI. ?
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?
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
31
30 ABMIC ALCULATOR package bmi ; public class ABMICalculator { public double calculateBMI( double weight, double height) { return weight/(height*height); }
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
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
34
33 P ROGRAMMED VS. INTERACTIVE CALL System.out.println(“setWeight Called”); Target ObjectMethod NameActual Parameters Programmed Call Interactive Call
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:
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
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.