Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110 O BJECTS Instructor: Jason Carter. 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)

Similar presentations


Presentation on theme: "C OMP 110 O BJECTS Instructor: Jason Carter. 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)"— Presentation transcript:

1 C OMP 110 O BJECTS Instructor: Jason Carter

2 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)

3 3 O UTLINE Intuitive Explanation Two Concrete Examples Calculators square BMI Basic Java program elements, programming styles, error handling Running and interacting with a Java program

4 4 S TRUCTURING IN S CRIPTS Script Performer Theater Follows

5 5 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 6 P ROGRAM C OMPONENTS ~ P HYSICAL O BJECTS Natural Objects Manufactured Objects ~ Program Components

7 7 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 8 C LASSIFICATION T HROUGH F ACTORIES manufactured by manufactured by

9 9 C LASSIFICATION T HROUGH F ACTORIES ASquareCalculator ABMICalculator ASquareCalculator Instance ABMICalculator Instance instance of

10 10 A S IMPLE C LASS : AS QUARE C ALCULATOR public class ASquareCalculator { public int square( int x) { return x*x; }

11 11 N ATURE OF A F UNCTION DomainRange Parameter Values Result Values Mapping 1 2 3 1 4 9 … …

12 12 M ATHEMATICS VS. J AVA F UNCTION S YNTAX Mathematics Java square: I  I square(x) = x 2 public int square( int x) { return x*x; }

13 13 I NSTANTIATING AS QUARE C ALCULATOR

14 14 AS QUARE C ALCULATOR I NSTANCE

15 15 I NVOKING A M ETHOD A ND V IEWING THE R ESULT

16 16 O BJECT E DITOR VS. AS QUARE C ALCULATOR Two objects with different methods

17 17 A NOTHER S IMPLE C LASS : ABMIC ALCULATOR ASquareCalculatorABMICalculator Specification: Given an integer x, calculate the square of x. 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. ? How do we modify the ASquareCalculator code to meet the BMI calculator specification?

18 18 AS QUARE C ALCULATOR VS. ABMIC ALCULATOR public class ASquareCalculator { public int square( int x) { return x*x; } public class ASquareCalculator { public int square( int x) { return x*x; } ABMICalculator calculateBMI int weight, int height return weight/(height*height)

19 19 ABMIC ALCULATOR 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?

20 20 ABMIC ALCULATOR public class ABMICalculator { public int calculateBMI( int weight, int height) { return weight/(height*height); } double

21 21 ABMIC ALCULATOR public class ABMICalculator { public double calculateBMI( double weight, double height) { return weight/(height*height); }

22 22 D EVELOPING BMI C ALCULATOR Use Eclipse

23 23 I NTERACTING WITH O BJECT E DITOR Run the calculateBMI function in ABMICalculator

24 24 I NTERACTING WITH O BJECT E DITOR Fill out the parameters of the calculateBMI function Press the “Calculate BMI(double,double)” button to see the result

25 25 P ROGRAM D EVELOPMENT P ROCESS Compiler ABMICalculator Source Code ABMICalculator Object (byte) Code Eclipse creates reads writes Interpreter calls ObjectEditor main instantiates calculateBMI calls ABMICalculator Instance

26 26 A NATOMY OF A C LASS 1: public class ABMICalculator 2: { 3: public double calculateBMI ( double weight, double height) 4:{ 5: return weight/(height*height); 6:} 7: } Class Header Class Body Method Header Method Body Parameter Type Parameter Name Access Specifier Return Type Return Statement Return Expression

27 27

28 28 S TRUCTURING IN S CRIPTS Script Introduction Body Conclusion Paragraph 1 Paragraph 2 Sentence 1Sentence 2 Script components are abstract. So are program components!

29 29 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

30 30 J AVA VS. R EAL - WORLD

31 31 A NATOMY OF A C LASS 1: public class ABMICalculator 2: { 3: public double calculateBMI ( double weight, double height) 4:{ 5: return weight/(height*height); 6:} 7: } Class Header Class Body Method Header Method Body Parameter Type Parameter Name Access Specifier Return Type Return Statement Return Expression

32 32 F ORMAL VS. A CTUAL P ARAMETERS 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 33 O NE M ORE S IMPLE C LASS : A N A VERAGE C ALCULATOR AnAverageCalculator Specification: Given two real numbers, return their average (as a real number). 1: 2: 3: 4: 5: 6: 7: public class AnAverageCalculator public double calculateAverage(double num1, double num2) return (num1+num2)/2; { { } } } } { {

34 34 E RRORS class ABMICalculator { double calculateBMI( double weight, double height) { return (height*heigh)/weight } Syntax ErrorLogic Error Semantics Error Access Error

35 35 C LASS A CCESS E RROR While instantiating a ABMICalculator object you get the following error: Reasons ABMICalculator class is not public

36 36 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

37 37 U SER E RROR While instantiating a AXYZCalculator object you get the following error Reason AXYZCalculator class does not exist

38 38 U SER E RROR While instantiating a abmicalculator object you get the following error Reason Class name is spelled with incorrect case

39 39 U SER E RROR When invoking the Square method in ASquareCalculator you get the following error Reason Parameter value is not in the function domain (set of allowed parameter values)

40 40 JAVAC E RROR R EPORTING ABMICalculator.java (3,3) : error J0232: Expected '{' or ';' ABMICalculator.java (3,3) : error J0021: Expected type specifier ABMICalculator.java (3,3) : error J0019: Expected identifier ABMICalculator.java (5,1) : error J0020: Expected 'class' or 'identifier' Line Number Character Number

41 41 C ASE C ONVENTIONS Start Class Names With Upper Case Letters aBMICalculatorABMICalculator Start Variable and Method Names With Lower Case Letters Weightweight Squaresquare Start Variable and Method Names With Lower Case Letters Each New Word in the Name Starts with a Capital Letter converttoinchesconvertToInches aBMICalculatorABMICalculator

42 42 O BJECT E DITOR C HANGES C ASE Method name starts with capital letter Adds spaces to names Different conventions for programmers and end users.

43 43 I DENTIFIERS Reserved Words (Keywords) Program-defined Names int, double, class, return, public boldface Variable names, method names, class names Must begin with a letter Other characters can be letters, digits, or underscore “_” calculate_bmi2 calculateBMI3

44 44

45 45 E XTRA S LIDES

46 46

47 47

48 48

49 49 A S IMPLE C LASS public class ABMICalculator { public int calculateBMI ( int weight, int height) { return weight/(height*height); } public class ABMICalculator { public double calculateBMI ( double weight, double height) { return weight/(height*height); }

50 50 U SER E RROR

51 51


Download ppt "C OMP 110 O BJECTS Instructor: Jason Carter. 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)"

Similar presentations


Ads by Google