Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Similar documents
AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object-Oriented Programming Concepts

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Java Primer 1: Types, Classes and Operators

CSI33 Data Structures

Creating an object Instance variables

Chapter 6 Introduction to Defining Classes

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

Chapter 4. Defining Classes I

Building Java Programs

Assignment 1 due Monday at 11:59pm

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

EECS168 Exam 3 Review

Java Object Oriented Design. CSC207 Fall 2014

Defining Classes and Methods

Lecture 11: Intro to Classes

Selected Questions from by Nageshwara Rao

TaxiBot New attributes Variables Math! TaxiBot

Chapter 4 Defining Classes I

Separate Compilation Model

Chapter 4: Writing Classes

Topic 27 classes and objects, state and behavior

Introduction to Programming Using Java (98-388)

Encapsulation in C++

Defining Classes and Methods

Lesson 12: OOP #2, Accessor Methods (W03D4)

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CS304 Object Oriented Programming Final Term

Objects as a programming concept

Computational Expression

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

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Dot and Scope Resolution Operator

Array. Prepared By - Rifat Shahriyar

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CSC 533: Organization of Programming Languages. Spring 2005

Classes and Objects. CGS 3416 Spring 2018

Classes and Objects. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

APCS Semester #1 Final Exam Practice Problems

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 6 Lab Classes and Objects

c) And last but not least, there are javadoc comments. See Weiss.

Java Fall 2018 Margaret Reid-Miller

CS18000: Programming I

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Midterms Save the Dates!

Class I. Monir Zaman

Sample Paper of Computer for Class 10

This exam is open book. Each question is worth 3 points.

Object Oriented Design: Identifying Objects

Programming II (CS300)

CST141 Thinking in Objects Page 1

Admin. CS 112 Introduction to Programming. Exercise: Gene Finding. Language Organizing Structure. Gene Finding. Foundational Programming Concepts

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

CSCI 355 Lab #2 Spring 2007

Programming II (CS300)

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Final Exam Practice. Partial credit will be awarded.

Object Oriented Programming

Programming II (CS300)

Introduction to Java. Handout-1d. cs402 - Spring

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CPSC 233: Assignment 4 (Due March 26 at 4 PM)

ITI Introduction to Computing II

Object-Oriented Programming

7. C++ Class and Object

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Object Oriented Programming

Introduction to Programming (Java) 4/12

Some miscellaneous concepts

CS 112 Introduction to Programming

TypeScript. Types. CS144: Web Applications

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CSCI 355 LAB #2 Spring 2004

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Programming Language Concepts: Lecture 1

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Defining Classes and Methods

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

G52CPP C++ Programming Lecture 9

Building Java Programs

JAVA MOCK TEST JAVA MOCK TEST II

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Today s Agenda. Quick Review

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Chapter 6 Lab Classes and Objects

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Transcription:

Object-Oriented Programming (OOP) Basics CSCI 161 Introduction to Programming I

Overview Chapter 8 in the textbook Building Java Programs, by Reges & Stepp. Review of OOP History and Terms Discussion of Object State and Behavior

OOP History Object-Oriented Programming dates back to 1967 and the Simula computer programming language Used for simulation programs Its elements for holding data were known to be called objects Today there are many programming languages that are object-oriented, including Java, C++, Python,

OOP Terms Object-Oriented Programming A reasoning about a program as a set of objects rather than a set of actions. Reasoning in this context refers to many aspect of programming: design, construction, the view of the overall program and its component solution, Procedural Programming Is the oldest style of programming. Based on functions, procedures and methods, and is what this class has been focused on until now

OOP Terms (continued) Procedural vs OOP verb vs noun Verb Procedural or functional in nature. Action in nature Hinted by method names: displayprompt, getnextinteger, readfile. Noun Objects are the building blocks of the program As such, they are things, nouns, things actually found in the world and the space of the problem at hand. Object names read like nouns: file, person, reader, scanner, etc.

OOP Terms (continued) Java is a class-based OOP language: Class-based in that objects are instances of classes It is through that relationship (instance of a class) that an object gets its type Remember, primitive data types (PDTs) like int, double, char are not instances of classes, thus are not objects once declared: Example: int num = 5; However, the String class is just that, a class, so a variable declared from the String class is an object, with methods, and of the type String class: Example: String str = new String(5);

OOP Terms (continued) Object A programming entity that contains state (data) and behavior (methods). State A set of values (internal data) stored in an object. Behavior A set of actions and object can perform, often reporting or modifying its internal state. Client (or Client Code ) Code that interacts with the objects of a class (aka calling code, or caller).

Classes, Objects, State Classes, objects and state are not complete new: The variables within a class are what hold its data and define its state. You already develop each program and include one class, the program class, and it contains the required main method. You have already worked with several classes, including Scanner, File, String, Integer, Double You can also add and define your own classes, as separate.java files, and these act as blueprints for useful objects within your program.

Classes, Objects, State (continued) Class vs Object: A class in your program that provides a blueprint for how objects created from that class will act, behave, etc., since each class defines the following about its objects: The state state in each object, stored internally in private variables called fields. The behavior each object can performed as defined by the methods within the class. How to construct object of that class type.

Object Behavior There are several; types of methods that define object behavior, and they have names: Instance Method A method inside an object that operates on that object. Examples: String has.length(), Scanner has.hasnext(). Implicit Parameter The object itself is an implicit parameter of all Instance methods, meaning the state (data within) is always available within instance methods. Mutator An instance method that modifies the object s internal state. Accessor An instance method that provides information about the state of an object without modifying it.

Object Initialization: Constructors Constructor The method of the same name as the class, and it is used to Construct the object from the class: A class can have many constructors, all having the same name as the class, but with different parameters. A common use of a constructor is to allow the caller to specify initializing values for state (field) variables of the class (as in supplying X and Y for the Point class)

Constructors With and Without // Without a constructor Point p1 = new Point(); p1.x = 7; p1.y = 2; // With a constructor Point p1 = new Point(7, 2);

Constructors (continued) The default constructor is inserted by the JVM if no other constructors are defined in your classes code. It is a good practice to always include a constructor, even if it accepts no parameters and is used only to initialize state (field) variables to specific, known values. Warning Be careful NOT to include void as the return data type of a constructor. Constructors have no return type and including void out of habit will indicate to Java that the method is just another method and not a constructor. This The keyword this refers to the object derived from the class and is a convenient way within constructors and methods to reference object fields.