Lecture 7 Objects and Classes

Similar documents
Lecture 06: Classes and Objects

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

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

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

Lecture 08: Static Fields and Methods

Lecture 9: Lists. MIT-AITI Kenya 2005

Recitation 3 Class and Objects

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Lecture 10: Interfaces

VARIABLES AND TYPES CITS1001

Building Java Programs

CS 251 Intermediate Programming Methods and More

1.00 Lecture 6. Java Methods

Object-Oriented Programming Concepts

CS 251 Intermediate Programming Methods and Classes

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Principles of Object Oriented Programming. Lecture 4

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

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

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

Lecture 2: Java & Javadoc

Lecture 6 Introduction to Objects and Classes

1 Method Signatures and Overloading (3 minutes, 2 points)

Java Magistère BFA

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

ECE 122. Engineering Problem Solving with Java

ESc101 : Fundamental of Computing. Object Oriented programming

8/27/13. An intro to programming* *Java. Announcements. Professor Rodger. The Link. UTAs start TONIGHT!

Creating and Using Objects

Chapter 4 Defining Classes I

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

Week 3 Classes and Objects

C# Programming for Developers Course Labs Contents

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Introduction Course Information Object Oriented Concepts Operators & Control Statements Assignment 3. CS Java. Introduction to Java

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Review: Array Initializer Lists

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Implementing Classes

1.00 Lecture 8. Using An Existing Class, cont.

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

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

Learning the Java Language. 2.1 Object-Oriented Programming

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

STUDENT LESSON A5 Designing and Using Classes

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

JAVA GUI PROGRAMMING REVISION TOUR III

Imperative Languages!

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

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

PROGRAMMING FUNDAMENTALS

Object Oriented Programming

Lecture 1: Introduction to Java

Lecture 5: Methods CS2301

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Container Vs. Definition Classes. Container Class

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

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

MIT AITI Lecture 18 Collections - Part 1

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

1.00 Lecture 13. Inheritance

Last Name: Circle One: OCW Non-OCW

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

Implementing Classes (P1 2006/2007)

Introduction to Programming Using Java (98-388)

BM214E Object Oriented Programming Lecture 7

Inheritance and Subclasses

Overview. Lab 5 Methods and Parameters

Practice problem on defining and using Class types. Part 4.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

5. Defining Classes and Methods

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

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CS 302: Introduction to Programming in Java. Lecture 15

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

Object-Oriented Programming Concepts

An introduction to Java II

Industrial Programming

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

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

JAVA: A Primer. By: Amrita Rajagopal

BM214E Object Oriented Programming Lecture 8

Lesson 43.. ArrayList

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS112 Lecture: Defining Instantiable Classes

Problem Grade Total

CMSC 202. Exceptions

BM214E Object Oriented Programming Lecture 4

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

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Object Oriented Programming

Transcription:

Lecture 7 Objects and Classes An Introduction to Data Abstraction MIT AITI June 13th, 2005 1

What do we know so far? Primitives: int, double, boolean, String* Variables: Stores values of one type. Arrays: Store many of the same type. Control Structures: If-then, For Loops. Methods: Block of code that we can pass arguments to and run anytime. Is this all we need? 2

So what s the problem? Some data sticks together. String[] names int[] grades Methods start to get complicated. Methods can only return one type. Programmers don t want to think about all the underlying types. 3

Abstraction Objects are tools for abstraction. We abstract away details to deal with complex problems. Abstraction is a fundamental concept in computer science. There can be too much abstraction. The art is knowing which details to hide away and which to preserve. 4

What is an object? Objects have two parts: State: Properties of an object. Behavior: Things the object can do. Car Example: State: Color, engine size, automatic Behavior: Brake, accelerate, shift gear Person Example: State: Height, weight, gender, age Behavior: Eat, sleep, exercise, study 5

What is an Object? Figures removed for copyright reasons. See http://java.sun.com/docs/books/tutorial/java/concepts/object.html A Generic Object An Bicycle Object 6

Why use objects? Modularity: Once we define an object, we can reuse it for other applications. Information Hiding: Programmers don t need to know exactly how the object works. Just the interface. Example: Different cars can use the same parts. You don t need to know how an engine works in order to drive a car. 7

Our first Class: LightSwitch class LightSwitch { boolean on = true; The keyword class tells java that we re defining a new type of Object. Classes are a blueprint. Objects are instances of classes. Everything in Java (except primitives) are Objects and have a Class. 8

Classes Figures removed for copyright reasons. See "MyBike" and "YourBike" figures at http://java.sun.com/docs/books/tutorial/java/concepts/class.html A Bicycle Class Two instances of the Bicycle Class 9

Our first Class: LightSwitch class LightSwitch { boolean ison = true; What state do LightSwitches have? State stored in fields; here it s ison. Fields are accessed using: variablename.fieldname (We ll discuss other types of fields later.) What behavior do LightSwitches have? 10

Adding Behavior class LightSwitch { boolean ison = true; void flip() { this.ison =!this.ison; The this keyword means this particular object. Objects know themselves. this.ison accesses the ison field. What behavior does LightSwitch have now? 11

Using Objects public static void main(string[] args) { LightSwitch s = new LightSwitch(); System.out.println(s.isOn); s.flip(); System.out.println(s.isOn); The new keyword creates a new object. new must be followed by a constructor. We call methods like: variablename.methodname(arguments) What does this code output? 12

Constructors Constructors provide objects with the data they need to initialize themselves, like How to Assemble instructions. Objects have a default constructor that takes no arguments, like LightSwitch(). We can define our own constructors that take any number of arguments. Constructors have NO return type and must be named the same as the class: ClassName(argument signature) { body 13

Constructors class LightSwitch { boolean ison; void flip() { this.ison =!this.ison; LightSwitch(boolean startstate) { this.ison = startstate; The LightSwitch() constructor no longer works. How do we instantiate an object? 14

Multiple Constructors We can have multiple constructors. Constructors can call each other. LightSwitch() { LightSwitch(true); LightSwitch(boolean startstate){ this.ison = ison; 15

Pop Quiz What two properties do objects have? What is the difference between a class and an object? What is a field? What does the this keyword mean? What does the new keyword do? What is a constructor? 16

BankAccount Example class BankAccount { double balance; String name; BankAccount(String name, double openbalance){ this.name = name; this.balance = openbalance; // Continued next slide 17

BankAccount Example double deposit(double amount) { balance += amount; return balance; boolean withdraw(double amount) { if (amount < balance) { balance -= amount; return true; else return false; // End BankAccount Class 18

MIT OpenCourseWare http://ocw.mit.edu EC.S01 Internet Technology in Local and Global Communities Spring 2005-Summer 2005 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.