Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

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

Object-Oriented Programming Concepts

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

by Pearson Education, Inc. All Rights Reserved. 2

Java Magistère BFA

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

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Programming II (CS300)

Programming II (CS300)

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

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

CLASSES AND OBJECTS IN JAVA

Implementing Classes (P1 2006/2007)

Lecture 06: Classes and Objects

Introduction to Programming Using Java (98-388)

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.

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

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

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

JAVA: A Primer. By: Amrita Rajagopal

Microsoft Visual Basic 2005: Reloaded

Principles of Object Oriented Programming. Lecture 4

Objects and Classes -- Introduction

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

Software and Programming 1

DCS/100: Procedural Programming

7. C++ Class and Object

Inheritance. COMP Week 12

Mobile Application Programming. Swift Classes

Object Oriented Software Development CIS Today: Object Oriented Analysis

Chapter 6 Introduction to Defining Classes

Chapter 4. Defining Classes I

Object Oriented Programming

Classes and Objects. CGS 3416 Spring 2018

Defining Classes and Methods

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

Object Oriented Programming. Solved MCQs - Part 2

EECS168 Exam 3 Review

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Chapter 4 Defining Classes I

Elementary Concepts of Object Class

Implementing Classes

CSCE 156 Computer Science II

A A B U n i v e r s i t y

Imperative Languages!

Chapter 8: Creating Your Own Type Classes

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CS 231 Data Structures and Algorithms, Fall 2016

5. Defining Classes and Methods

Object Oriented Programming. Week 1 Part 1 An introduction to Java, Objects and JUnit

5. Defining Classes and Methods

OBJECT INTERACTION CITS1001

Review sheet for Final Exam (List of objectives for this course)

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

1.00 Lecture 8. Using An Existing Class, cont.

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

Object Oriented Modeling

Mobile Application Programming. Swift Classes

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

Lecture 7: Classes and Objects CS2301

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Chapter 11: Create Your Own Objects

Object Reference and Memory Allocation. Questions:

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

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

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

CMSC 132: Object-Oriented Programming II

Fundamentals of Object Oriented Programming

CSE 113 A. Announcements - Lab

Thursday, February 16, More C++ and root

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

Programming overview

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

Java Primer 1: Types, Classes and Operators

MechEng SE3 Lecture 7 Domain Modelling

Inheritance, and Polymorphism.

CS2102, D15 Exam 1. Name:

Classes. Classes as Code Libraries. Classes as Data Structures

ICOM 4015 Advanced Programming Laboratory. Chapter 4 Introduction to Object Oriented Programming

About this exam review

Objects First with Java A Practical Introduction using BlueJ

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes

Defining Classes and Methods

What are the characteristics of Object Oriented programming language?

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

Computer Science AP 2017 Summer Assignment Mrs. McFarland

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

CHAPTER 7 OBJECTS AND CLASSES

Lab5. Wooseok Kim

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Lecture 6 Introduction to Objects and Classes

Computer Programming C++ Classes and Objects 6 th Lecture

2. The object-oriented paradigm!

Transcription:

Classes and Objects COMP1400/INFS1609 Week 8

Abstraction Object-oriented Programming Dividing the program into chunks at different levels of detail. Encapsulation Each chunk hides its implementation details from outsiders.

Public/private Each object has: a public interface that describes how it can be used a private implementation that describes how it works

Classes Objects have types called Classes. Classes represent all objects of a certain kind. All objects in a class share a common structure but have different details.

Example Car is a class. All cars have data: colour engine capacity, etc. and methods: drive park, etc.

Example My car is an object. It is an instance of the class Car. My car has data with specific values: colour = yellow engine capacity = 1.5 litre and methods: drive park, etc.

Static data and methods You usually need a specific instance of a class (an object) to access data and methods. Eg: you can t drive the class Car or ask what colour Car is. However some methods and data belong to all cars and can be run on the class. These are called static.

Java Class Library Java comes with a large collection of classes for common object types. You can browse the library online at: http://docs.oracle.com/javase/6/docs/api/ index.html?overview-summary.html

Random The Random class: http://docs.oracle.com/javase/6/docs/api/java/util/ Random.html Implements a random number generator.

JCL docs Things to notice in the docs for Random: The package - java.util.random The constructors The methods

Creating objects To use an object we must first create an instance of the class. We do this by calling one of the constructors. The constructor creates the object and initialises it so it is ready to use.

JCL in BlueJ Select Tools > Use Library Class...

Methods Each Random object has a number of methods:

Importing If we want to use a JCL class in our code, we need to import it first. Syntax: import java.util.random; import Full class name. keyword

Calling constructors To create an object in code we call the constructor as: new Random(); or: new Random(100); new keyword class parameters

Classes are types Classes are types and can be used in the same way as primitive types like int and double to create variables. int years = 100; Random rng = new Random(100);

State Objects have state -- internal private data which describe their current configuration. E.g. the state of a Car object would include: the amount of fuel it has left, the number of miles it has been driven, what gear it is in, etc...

Calling methods To call methods on an object: Random rng = new Random(100); int roll = rng.nextint(6); object dot method parameters

Accessing state An object s state is private (encapsulated). The class may provide public methods to access state and manipulate it in proscribed ways. Eg: rng.setseed(100);

Random seed The seed of a random number generator determines the values it creates. Two Random objects with the same seed produce the same sequence: Random rng1 = new Random(100); Random rng2 = new Random(100);

References When we create an object it is allocated as a block of data in memory. The value the constructor returns is a reference to that block. A reference is like an address. It is a piece of information that tells us where the object is.

Reference When we assign an object variable to another variable, we copy the reference not the object. Random rng1 = new Random(100); Random rng2 = rng1; // both now refer to the same // object

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50);

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random seed = 100

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 100

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 100 Random seed = 110

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 100 Random rng2 seed = 110

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 100 Random rng2 seed = 110

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 50 Random rng2 seed = 110

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 50 Random rng2 seed = 110 Garbage collected

Random rng1 = new Random(100); Random rng2 = new Random(110); rng2 = rng1; rng2.setseed(50); Random rng1 seed = 50 rng2 Garbage collected