370 likes | 515 Views
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.
E N D
Comp 401Objects Instructor: PrasunDewan
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. Paragraph 1 Paragraph 2 So are program components! Sentence 1 Sentence 2
Program Components ~ Physical Objects Natural Objects Manufactured Objects ~ Program Components
Program Objects ~ Manufactured Objects Operations manufactured by accelerate perform brake Methods Class Program Object instance of execute invoke call add Program Object subtract
Classification Through Factories manufactured by manufactured by
Classification Through Factories ASquareCalculator ASquareCalculator Instance instance of ASquareCalculator Instance ABMICalculator ABMICalculator Instance instance of ABMICalculator Instance
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)); } }
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)); } }
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)); } }
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)); } }
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)); } }
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)); } }
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)); } }
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
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
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 {…}
Browsing Java Classes Very useful package
Language vs. Library Built-in classes
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)); } }
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)); } }
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])); } }
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 ); } }
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; • } • } ?
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?
ABMICalculator double • package bmi; • publicclassABMICalculator • { • publicintcalculateBMI(int weight, int height) • { • return weight/(height*height); • } • } Doubles are decimal/real numbers
ABMICalculator • package bmi; • publicclassABMICalculator • { • publicdouble calculateBMI(double weight, double height) • { • return weight/(height*height); • } • }
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
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
Programmed vs. interactive call System.out.println(“setWeight Called”); Programmed Call Target Object Method Name Actual Parameters Interactive Call
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)
Errors • package bmi; • classABMICalculator • { • double calculateBMI(double weight, double height) • { • return (height*heigh)/weight • } Logic Error Syntax Error Access Error Semantics Error
Errors • package bmi; • classABMICalculator • { • double calculateBMI(double weight, double height) • { • return (height*heigh)/weight • } Logic Error Syntax Error Access Error Semantics Error
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