Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Similar documents
Chapter 4 Defining Classes I

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

5. Defining Classes and Methods

Defining Classes and Methods

Defining Classes and Methods

Lecture 02, Fall 2018 Friday September 7

Introduction to Programming Using Java (98-388)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

by Pearson Education, Inc. All Rights Reserved. 2

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

And Even More and More C++ Fundamentals of Computer Science

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

Object-Oriented Programming Concepts

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

5. Defining Classes and Methods

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Classes - Using your classes. How the classes you write are being used

CSC207 Week 2. Larry Zhang

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CS313D: ADVANCED PROGRAMMING LANGUAGE

CMSC131. Library Classes

Java Fundamentals (II)

COP 3330 Final Exam Review

CMSC 132: Object-Oriented Programming II

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Chapter 6 Introduction to Defining Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Defining Classes and Methods

MODULE 3p - A Java Object

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Chapter 15: Object Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Programming, numerics and optimization

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

Inheritance and Polymorphism

Methods. Methods. Mysteries Revealed

JAVA: A Primer. By: Amrita Rajagopal

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

Programming for Engineers in Python

Lecture 7 Objects and Classes

CS1004: Intro to CS in Java, Spring 2005

CS24 Week 3 Lecture 1

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

Anatomy of a Class Encapsulation Anatomy of a Method

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Java Magistère BFA

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Methods and Data (Savitch, Chapter 5)

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

CHAPTER 7 OBJECTS AND CLASSES

System.out.print(); Scanner.nextLine(); String.compareTo();

Lecture 06: Classes and Objects

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

OBJECT ORİENTATİON ENCAPSULATİON

Object oriented programming Concepts

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Classes. Classes as Code Libraries. Classes as Data Structures

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Chapter 6 Lab Classes and Objects

CSCU9T4: Managing Information

Chapter 6 Classes and Objects

CHAPTER 7 OBJECTS AND CLASSES

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Reviewing OO Concepts

Fundamental Concepts and Definitions

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Chapter 3 Objects and Classes

Python Class Definition. Java Class Definition. Python Class Use public class Student { Java Class Use. Python Class Definition 10/6/2016

CS 170 Java Programming 1. Week 12: Creating Your Own Types

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

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

Inheritance. Transitivity

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Lecture 7: Classes and Objects CS2301

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

STUDENT LESSON A5 Designing and Using Classes

Object-Oriented Programming in Processing

Principles of Object Oriented Programming. Lecture 4

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Object Orientation Fourth Story. Bok, Jong Soon

Reviewing OO Concepts

Transcription:

Computer Science II OO Programming Classes Scott C Johnson Rochester Institute of Technology

Outline Object-Oriented (OO) Programming Review Initial Implementation Constructors Other Standard Behaviors Unified Modeling Language (UML) Testing

OO Programming Review Data are objects Objects is a specific instantiation of a class Class defines the data components Think of this as a blueprint You can make many objects from a single blueprint Each object can have small variations In Python we used classes for thing like trees But we can use them for more complex things Unlike Python we can encapsulate things in Java Allows us to limit what others can see/use Private items are hidden from others

OO Programming Review Example: Simulation of cars driving on a race track Vehicle Contain a number of simple data values Speed Fuel level Color Steering direction Location And possibly many more Can also contain states Brake Accelerate Refuel Etc.. We can control these states So someone cannot set the speed directly We can stop a change from 0 to 60 in zero seconds!) Behaviors can validate the data before it is set

Initial Implementation Parts of a Class Instance variables also called fields Different for each object, or instance, of the class Note a type is given for each Each is private cannot be changed by others! They have been encapsulated

Initial Implementation Parts of a Class (continue) Behaviors also called methods or functions Also go in class scope Behaviors work the same for all instances of this class Example: accelerate Has access to class variables public so the outside world can use it void because it returns nothing Validates max speed has not been exceeded

Initial Implementation Parts of a Class (continue) Behaviors also called methods or functions Need outside access to a class variable? You must provide a behavior

Initial Implementation Parts of a Class (continue) Constructors Create new instances of the class Invoked when using the new keyword Example Vehicle v = new Vehicle(); It is optional to write a constructor Java will give you a default one It will have no parameters Will leave class variables in a default state Null for objects Zero for numeric types In general you want to write a constructor Except for very simple classes

Initial Implementation Parts of a Class (continue) Constructors (continue) Must have the same name as the class No return type Must be public Create a new instance:

Initial Implementation Parts of a Class (continue) Constructors (continue) Java allows multiple constructors It takes no arguments We set some default values with this constructor But this looks like a lot of effort duplicated...

Initial Implementation Parts of a Class (continue) Constructors (continue) Calling a constructor in a constructor Use this instead of the constructor name Better yet we can avoid magic numbers here final means it cannot be changed! static tells the compiler not to create a copy for each instance Only one per class since it cannot be changed

Other Standard Behavior Just like Python, there are certain behaviors common in all classes We will see late that all classes have these Whether we write them or not The tostring() method Produces a text representation for printing Has a required form Must be public String tostring() { } For our Vehicle class we could do this:

Other Standard Behavior Another is equality Java has twp separate ideas of equality Exact same object under two names ( == ) Given by Java Or two distinct objects with same internal state ( equals() ) Written as public boolean equals( Object o ) { } We must write this one! Any object can be passed in We need to decide if it is equivalent to our vehicle Notice we do not have to use all of the class variables!

Unified Modelling Language (UML) UML Graphical display of our program Displays classes and their relationships Does so in a standardized way You will get use to seeing this!

Testing Our Vehicle class does not do much on its own Only when we have another class to test it The class TestVehicle contains example usage Creates some vehicles Runs some functions on them This is the idea behind unit-testing A special class to test our class and it behaviors Very important in OO programming The code in CannonballRun is a more involved simulation To the code!!!

Questions?