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

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

C OMP 110/401 P ACKAGES Instructor: Prasun Dewan.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.
C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not.
C OMP 401 O BJECTS Instructor: Prasun Dewan 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Midterm Exam 75 points 1 min per point Allocate time proportionate to points Closed book Chapters 1-5 (except char) PDF/PS with index and corrections coming.
COMP14 RECITATION 1 by- Bian Wu. Bare Bone Environment MS-DOS Prompt (Command Interpreter) Notepad (editor, c:\autoexec.bat) Jvc (J++ compiler) Jview.
Object-based Programming Intuitive explanation Two concrete examples Calculators addition BMI Programming Environment.
State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Computer Science 101 Introduction to Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C OMP 110 – I NTRODUCTION TO P ROGRAMMING Instructor: Prasun Dewan.
J AVA S CRIPT Shadi Banitaan 1. O UTLINE Introduction JavaScript Functions Using Objects in JavaScript Built-in Objects User-Defined Objects Examples.
Introduction to Python
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Computer Science 101 Introduction to Programming.
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
C OMP 401 P ACKAGES Instructor: Prasun Dewan 2 P REREQUISITES Objects.
Windows Programming Lecture 03. Pointers and Arrays.
L AB #3. System.out.println(“if you have” +eggPerBasket + “egg per basket and” + numberOfBaskets +”baskets, then the total number off eggs is“+totalEggs);
Working with Java.
Chapter 3 Syntax, Errors, and Debugging
Pamela Moore & Zenia Bahorski
Value-Returning Functions
Objectives Identify the built-in data types in C++
Instructor: Prasun Dewan
Chapter 2 Elementary Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
MSIS 655 Advanced Business Applications Programming
CS2011 Introduction to Programming I Methods (I)
Java Programming Review 1
Object Oriented Programming in java
Defining Classes and Methods
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

C OMP 110 O BJECTS Instructor: Jason Carter

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

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 S TRUCTURING IN S CRIPTS Script Performer Theater Follows

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

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

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

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

11 N ATURE OF A F UNCTION DomainRange Parameter Values Result Values Mapping … …

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 I NSTANTIATING AS QUARE C ALCULATOR

14 AS QUARE C ALCULATOR I NSTANCE

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

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

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 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 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 ABMIC ALCULATOR public class ABMICalculator { public int calculateBMI( int weight, int height) { return weight/(height*height); } double

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

22 D EVELOPING BMI C ALCULATOR Use Eclipse

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

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

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 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 J AVA VS. R EAL - WORLD

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

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 E RRORS class ABMICalculator { double calculateBMI( double weight, double height) { return (height*heigh)/weight } Syntax ErrorLogic Error Semantics Error Access Error

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

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 U SER E RROR While instantiating a AXYZCalculator object you get the following error Reason AXYZCalculator class does not exist

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

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

45 E XTRA S LIDES

46

47

48

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 U SER E RROR

51