C OMP 401 O BJECTS Instructor: Prasun Dewan 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)

Slides:



Advertisements
Similar presentations
C OMP 110/401 P ACKAGES Instructor: Prasun Dewan.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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 D YNAMIC D ISPATCH AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
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 110 L OAN C ASE S TUDY Instructor: Jason Carter.
C OMP 110 O BJECTS Instructor: Jason Carter. 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Object-based Programming Intuitive explanation Two concrete examples Calculators addition BMI Programming Environment.
ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.
State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
Writing Classes (Chapter 4)
C OMP 110 – I NTRODUCTION TO P ROGRAMMING Instructor: Prasun Dewan.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Introduction to Programming Writing Java Beginning Java Programs.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 401 P ATTERNS, I NTERFACES AND O BJECT E DITOR Instructor: Prasun Dewan.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
The Java Programming Language
INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
C OMP 401 A DVANCED G ENERICS Instructor: Prasun Dewan.
Introduction to Programming Writing Java Beginning Java Programs.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Creating and Using Class Methods. Definition Class Object.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Chapter 9 Introduction to Arrays Fundamentals of Java.
C OMP 401 P ACKAGES Instructor: Prasun Dewan 2 P REREQUISITES Objects.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
L AB #3. System.out.println(“if you have” +eggPerBasket + “egg per basket and” + numberOfBaskets +”baskets, then the total number off eggs is“+totalEggs);
C OMP 401 D YNAMIC D ISPATCH AND V IRTUAL AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
Creating Your Own Classes
Chapter 7 User-Defined Methods.
AKA the birth, life, and death of variables.
Programming Language Concepts (CIS 635)
Instructor: Prasun Dewan
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Recitation 2 September 2, 2011 Ben Newton.
AKA the birth, life, and death of variables.
Defining Classes and Methods
Functions Imran Rashid CTO at ManiWeber Technologies.
Factoring if/else code
Presentation transcript:

C OMP 401 O BJECTS Instructor: Prasun Dewan

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

3 S TRUCTURING IN S CRIPTS Script Performer Theater Follows

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!

5 P ROGRAM C OMPONENTS ~ P HYSICAL O BJECTS Natural Objects Manufactured Objects ~ Program Components

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

7 C LASSIFICATION T HROUGH F ACTORIES manufactured by manufactured by

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

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

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)

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

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

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

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

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

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

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

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 {…}

19 B ROWSING J AVA C LASSES Very useful package

20 L ANGUAGE VS. L IBRARY Built-in classes

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

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

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?

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

25 E DITING AS QUARE C ALCULATOR I NSTANCE

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

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

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?

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

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

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 assigned

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

33 P ROGRAMMED VS. INTERACTIVE CALL System.out.println(“setWeight Called”); Target ObjectMethod NameActual Parameters Programmed Call Interactive Call

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:

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

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

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