comp 401 objects
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Slide 1
Comp 401ObjectsInstructor: Prasun Dewan
#Computer vs. Program Model
ProcessorCompilerProgram (source code)?
#Structuring in ScriptsScriptPerformerTheater
Follows
#Structuring in ScriptsScriptIntroductionBodyConclusionParagraph 1Paragraph 2Sentence 1Sentence 2Script components are abstract.So are program components!
#Program Components ~ Physical ObjectsNatural Objects
Manufactured Objects
~ Program Components
#Program Objects ~ Manufactured Objects
manufacturedbyperform OperationsacceleratebrakeClassProgram ObjectProgram Objectinstance ofMethodsaddsubtractexecute invoke call
#Classification Through Factories
manufacturedbymanufacturedby
#Classification Through FactoriesASquareCalculatorABMICalculatorASquareCalculator InstanceASquareCalculator InstanceABMICalculator InstanceABMICalculator Instanceinstance ofinstance of
#A Simple Instantiated Classpublic 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 CreationObject UseNo static because class will be instantiatedNo package
#Packagespackage 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)
#Packagespackage 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
#Packagespackage 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
#Packagespublic 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
#Packagespublic 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
#Long name with no importpackage 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
#package safemath; public class ASquareCalculator {public long square(int x) {return x*x;}}Why 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 intsDisambiguates
#package safemath; public class ASquareCalculator {public long square(int x) {return x*x;}}Amgigous Importpackage 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
#Why Packages?Can create competing implementations of same class.A la creating files Test.java in different assignment directories/foldersCan browse related classesA la browsing through all files in an assignment directory/folder.Like directories/folders packages can be hierarchicalpackage math.power;public class ACubeCalculator {}
#Browsing Java Classes
Very useful package
#Language vs. Library
Built-in classes
#Changing Parameterpublic 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
#Changing Parameterpublic 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
#Rerun Programpublic 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 programHow to not re-run program without writing tedious UI code?
#ObjectEditorpackage 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
#Editing ASquareCalculator Instance
#Invoking a Method And Viewing the Result
#Another Simple Class: ABMICalculatorASquareCalculatorABMICalculatorSpecification: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.?
#ABMICalculatorpackage bmi;public class ABMICalculator{public int calculateBMI(int weight, int height) {return weight/(height*height);}}Parameter and return types are integersBut height (m) and weight (kg) are expressed as decimalsHow do we solve the discrepancy?
#ABMICalculatorpackage bmi;public class ABMICalculator{public int calculateBMI(int weight, int height) {return weight/(height*height);}}doubleDoubles are decimal/real numbers
#ABMICalculatorpackage bmi;public class ABMICalculator{public double calculateBMI(double weight, double height) {return weight/(height*height);}}
#Formal vs. Actual Parameterspackage bmi;public class ABMICalculator{public double calculateBMI(double weight, double height) {return weight/(height*height);}}
weight0height0variablesmemoryParametersParametersInvoke calculateBMI Actual ParametersFormal Parameters74.981.94assigned
#Programmed Callpublic 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 ParametersActual Parameters
#Programmed vs. interactive callSystem.out.println(setWeight Called);Target ObjectMethod NameActual Parameters
Programmed CallInteractive Call
#Internal vs. External Method Callspublic 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) { }}APoundInchBMICalculatorExternalCaller and callee methods are in different objectsMust specify target objectInternalCaller and callee methods are in the same objectTarget object is implicit (this)Actual parameters:
#Errorspackage bmi;class ABMICalculator{double calculateBMI(double weight, double height) {return (height*heigh)/weight}
Syntax ErrorLogic ErrorSemantics ErrorAccess Error
#Errorspackage bmi;class ABMICalculator{double calculateBMI(double weight, double height) {return (height*heigh)/weight}
Syntax ErrorLogic ErrorSemantics ErrorAccess Error
#Method Access Error
You instantiate a ABMICalculator objectbut there is no ABMICalculator menu itemReasonYou have not defined any public methods in ABMICalculator
#