Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Similar documents
Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Method. Why Write Methods? Overview. CS256 Computer Science I Kevin Sahr, PhD. Lecture 21: Writing Class Methods. ClassName.methodName(arguments)

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

Structured Programming

Objects and Classes Lecture 2

Flow of Control. Conditional Statements. Conditional Statements. CS256 Computer Science I Kevin Sahr, PhD. Lecture 15: The If-Statement

Recommended Group Brainstorm (NO computers during this time)

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

ITI Introduction to Computing II

Chapter 4 Defining Classes I

ITI Introduction to Computing II

Object-Oriented Programming Concepts

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

Chapter 6 Introduction to Defining Classes

Lecture 12 Modular Programming with Functions

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Sri Vidya College of Engineering & Technology

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CS445 Week 9: Lecture

CS 1316 Exam 1 Summer 2009

Chapter 14 Abstract Classes and Interfaces

Inheritance, and Polymorphism.

Creating a Java class with a main method

Lecture 6 Introduction to Objects and Classes

EECS 1001 and EECS 1030M, lab 01 conflict

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

CISC 1600 Lecture 3.1 Introduction to Processing

Nesting. Abstraction & Nesting. Example. if x is less than y output x is smaller else output y is smaller. CS256 Computer Science I Kevin Sahr, PhD


CMSC 132: Object-Oriented Programming II. Inheritance

Lesson 6 Introduction to Object-Oriented Programming

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Function Overloading

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Classes and Objects. CGS 3416 Spring 2018

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Object Oriented Programming

Inheritance and Polymorphism

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Class object initialization block destructor Class object

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Chapter 4. Defining Classes I

1.00 Lecture 8. Using An Existing Class, cont.

JAVA: A Primer. By: Amrita Rajagopal

Lecture 7: Classes and Objects CS2301

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Lecture 02, Fall 2018 Friday September 7

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Lecture 7 Objects and Classes

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

1 Getting started with Processing

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CS1004: Intro to CS in Java, Spring 2005

Chapter 8 Objects and Classes

Inheritance and Polymorphism

CS111: PROGRAMMING LANGUAGE II

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

7. C++ Class and Object

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

CS-202 Introduction to Object Oriented Programming

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

CS 170, Section /3/2009 CS170, Section 000, Fall

Partha Sarathi Mandal

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Chapter 3 Objects and Classes

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Loop - 3 & Class. Cheng, Wei COMP May 22, Title

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS/ENGRD 2110 SPRING 2018

Java Object Oriented Design. CSC207 Fall 2014

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Chapter 10 Classes Continued. Fundamentals of Java

Programming Language. Machine Code. CS200: Computer Science I. Module 3 Programming Languages

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

10-2 Circles. Warm Up Lesson Presentation Lesson Quiz. Holt Algebra2 2

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CLASSES AND OBJECTS. Summer 2018

Fundamental Concepts and Definitions

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

C Programming for Engineers Functions

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Transcription:

CS256 Computer Science I Kevin Sahr, PhD Lecture 11: Objects 1 Programs as Models remember: we write programs to solve realworld problems programs act as models of the real-world problem to be solved one way to model problems is to use a mathematical approach model the problem using variables and formulas/ functions so far, all of our programs have used this approach 2 Procedural Paradigm in Computer Science this is known as the Procedural Programming Paradigm under this paradigm, programs consist of two basic building blocks: 1.data 2.methods 3 Class Methods Java implements the procedural programming paradigm using class methods method header contains the keyword static invoked using ClassName.methodName(arguments) so far all of our methods have been class methods 4

Class Methods (cont) class methods work fine for: mathematical functions EX: Math.sin methods associated with a single object EX: Input class, where all input comes from the keyboard (not from multiple sources) 5 EX: Cylinder using this approach, we can model a cylinder using: two variables representing the cylinder s height and radius class methods that calculate the surface area, volume, etc. for a given height and radius (passed as arguments) 6 Multiple Cylinders works fine for one cylinder what if we have two cylinders? need variables for each: height1 and radius1 height2 and radius2 but what if we have two thousand cylinders? what if each also has a weight, a name, a color, a location, etc.? 7 Objects need a better way to model the real world when we look around us, we don t see data and methods we see objects that have: 1. attributes: associated data values 2. behavior: what the object can do 8

EX: Student for example, each student has attributes: name, ID#, age, grades, etc. behavior: talk, walk, take quiz, etc. 9 Many Students each student has different attribute values Bob has ID#555 Mary has ID#101 each student has independent behavior Bob talks Mary takes a quiz 10 OOP this approach to modeling led to the Object Oriented Programming (OOP) Paradigm basic building blocks of programs are objects, each consisting of attributes and behavior Java is an OOP language 11 Class want each student to be represented by a student object in our program in Java we do this by expanding our definition of what a class is class: a blueprint or template for creating objects 12

- ClassName Classes & Objects each class defines the attributes and behavior associated with an object of that class every object is an instance of some class the attributes associated with an object are called instance variables the behaviors associated with an object are defined by methods called instance methods classes are like data types for objects 13 Using Objects how do we use objects in Java? for our examples, let s assume there exists a class named Square that specifies the attributes and behavior of a square object 14 Creating an Object Variable creating a variable that represents an object requires two steps: 1. declaration - creates a variable that will refer to the object 2. instantiation - creates the actual object 15 Variable Declaration syntax: variablename; EX: Square sqr; assigns the label sqr to a memory location tells Java that this location is going to refer to an instance of the Square class 16

ClassName - variablename Object Instantiation syntax: = new ClassName(); EX: sqr = new Square(); creates an instance of the Square class makes the variable sqr refer to the new Square object 17 One Step declaration and instantiation can be done in a single statement syntax: variablename = new ClassName(); EX: create two Square object variables Square s1 = new Square(); Square s2 = new Square(); 18 Class API a class API can contain three different types of methods: 1.constructors 2.class methods - have keyword static in method header 3.instance methods - method header exactly like class methods except no static 19 Class API a class API can contain three different types of methods: 1.constructors 2.class methods - have keyword static in method header 3.instance methods - method header exactly like class methods except no static 20

Constructors purpose is to initialize the object give initial values to the object attributes invoked using the new operator when an object is instantiated method name is the same as the class name have no return type (not even void) 21 Default Constructor the default constructor is a constructor that takes no arguments initializes object attributes to default values 22 EX: Square Class Square Class API Constructors public Square() Default constructor to create a Square that is red, 30 pixels in size, is located at (60, 50), and is not initially visible. 23 Invoking Constructors invoked with the new operator EX: Square s1 = new Square(); invokes default Square constructor 24

Multiple Constructors a class may have more than one constructor multiple constructors must take different arguments Java invokes the correct constructor based on the arguments you give it 25 EX: Person Class Person Class API Constructors public Person() Constructor for a Person named John Doe who is 0 years old. Initially this Person is alive. public Person(String name, int age) Constructor for a Person with the specified name and age. Initially this Person is alive. 26 Invoking Constructors invokes default Person constructor EX: Person p1 = new Person(); Person p2 = new Person( Mary, 20); invokes second Person constructor 27 Class API a class API can contain three different types of methods: 1.constructors 2.class methods - have keyword static in method header 3.instance methods - method header exactly like class methods except no static 28

Instance Methods once we have an object, we can invoke instance methods on it invocation syntax is similar to class methods except invoked on a particular instance syntax: objectname.methodname(arguments); the available instance methods are given in the class API instance method headers look exactly like class method headers, except they do not contain static 29 Square Class the Square class API is attached note it contains no class methods Note the changecolor() instance method with method header: public void changecolor(string newcolor) EX: Create two Square objects. Change the color of one to green, and the other to blue. Square sqra = new Square(); Square sqrb = new Square(); sqra.changecolor( green ); sqrb.changecolor( blue ); 30 Shape Example three shape classes (Square, Circle, and Triangle) are provided ShapeDemo.java is a program that demonstrates how to use these classes the compiled shape class files must be placed in the same folder with ShapeDemo.java spend some time understanding this Example 31 Lecture 11 Vocabulary procedural programming paradigm class method object attribute object oriented programming (OOP) paradigm class instance instance variable constructor default constructor instance method instantiation 32