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

Size: px
Start display at page:

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

Transcription

1 CP4044 Lecture 5 1 Classes Lecture 5 Object-Orientation Object-oriented design (OOD) Models real-world objects Models communication among objects Encapsulates data (attributes) and functions (behaviors) Information hiding Communication through well-defined interfaces Object-oriented language Used to implement OO designs Programming is called object-oriented programming (OOP) Java We will not be looking at OO in depth just enough to facilitate systems programming in Java CP4044 Lecture 5 2 State and Behaviour Attributes define the state of an object Called instance variables in Java Attributes have a name, type and value e.g. a car might have an attribute with the name bodycolour of type colour and has the value red Methods define the behaviour of an object Similar to functions or procedures in other programming languages Methods have names that should indicate their behaviour e.g. a car might have a method moveforward CP4044 Lecture 5 3 Classes Definition Every object belongs to a group of objects of the same type called the object s class. A class defines the types of states and behaviour belonging to a group of objects Examples My Ferrari Belongs to the car class Wolverhampton Wanderers Football club class MT008 Lecture theatre class CP4044 Lecture 5 4 Object vs. Classes Class A template that defines attributes and methods Written by programmer as part of program Cannot be altered during program execution Is named by a class name Object Must belong to some class Exists whilst the program is executing Must be declared and constructed by a program Has attributes with values and methods that can execute Class defines these Referenced by an identifier (variable name) CP4044 Lecture 5 5 Instance Variables Scope: Instance variables may be accessed by all the methods in the class Variables declared in methods are local to the method and cannot be seen outside it more on this next week. All variables in Java must be initialised before use When a class is instantiated (i.e. object is created) the default values of its variables are 0 or null This is not the case with local variables, as we shall CP4044 Lecture 5 6

2 CP4044 Lecture 5 7 Creating Objects An object is an instance of a class An object is constructed or instantiated from a class Instantiation is done with the new operator An object reference is used to refer to an object Declaration of a TV object reference called mytv Construction and assignment of a TV object TV mytv; mytv = new TV(); new Operator TV constructor Parameters Used for passing data to a method Return type Defines the type of data to be passed from the method. Keyword void is used here if methods returns no data. Anatomy of a Method int calculatearea(int w, int h) { int area = w * h; return area; Return statement Used for passing data from the method. Omitted for void methods CP4044 Lecture 5 8 Parameters values Are used to transfer data to a method Return value Contains the data passed from the method. Here it is copied into a variable Calling a Method int calculatearea(int w, int h) { int area = w * h; return area; int area; area = calculatearea(2, 5); System.out.println("Area =" + area); A non-void method can be called anywhere an expression of the same type is permitted. e.g. from within calculations CP4044 Lecture 5 9 Overloaded Methods It is possible to have more than one method with the same name. Called overloading methods Must have different parameter types Must have the same return type double calculatecost(double price) { double taxrate = 1.175; return price * taxrate; Method uses default tax rate double calculatecost(double price, double taxrate) { return price * taxrate; User specifies tax rate CP4044 Lecture 5 10 Accessing Methods and Attributes An object reference acts as an alias for an object Object references can be used to access instance variables and methods Object-reference.variable-name Object-reference.method-name Examples: mytv.channel = 1; Sets the channel instance variable of the mytv object to the value 1 mytv.press2(); Calls the press2 method of the mytv object CP4044 Lecture 5 11 Types of Variables & Scope Instance variables Defined inside a class, but outside all methods Used to store the state of an object Can be accessed from any method in the class Automatically initialised - given default values Parameter variables Defined in a method header Used to pass data to a method Can only be accessed from the method they are defined in Initialised by values from the caller Local variables Defined inside a method Used for storing intermediate/temporary values Can only be accessed from the method they are defined in Must be explicitly initialised before they can be used CP4044 Lecture 5 12

3 CP4044 Lecture 5 13 Resolving Ambiguity The this reference points to the current object. this.name therefore references the instance variable called name belonging to the current object. Constructors Constructors Similar to methods and invoked when an object is created Used to perform additional initialisation Constructor has same name as class No return type Can be overloaded This line copies the value from the parameter variable to the instance variable. class Person { String name; void setname(string name) { this.name = name; Default Constructor Constructor without parameters Invoked if no other constructor is specified when object is created Automatically created by Java if not explicitly declared CP4044 Lecture 5 14 Default Constructors Constructors are like methods: But have no return type (not even void) Must have exactly the same name as the class Can be overloaded Default constructor has no parameters Used to give initial values to instance variables If you do not supply a default constructor, the compiler will generate one for you. But its behaviour might not be what you want Parameterised Constructors Parameterised constructors require the caller to supply parameters Parameters are used to initialise instance variables Saves the caller calling update methods just after the default constructor This is what you have to do in some semi-oo languages such as VB 6. Constructor uses similar syntax to update methods The this reference is used to resolve ambiguity Can have various different overloaded versions of constructors for complex classes Allow the user to specify some parameters, but use defaults for others. CP4044 Lecture 5 15 CP4044 Lecture 5 16 House Class House Class Instance variables class House { int number; String street; class House { int number; String street; int getnumber() { return number; Default constructor Parameterised constructor 4 House() { 5 number = 0; 6 street = "not known"; 7 8 House(int number, 9 String street) { 10 this.number = number; 11 this.street = street; House() { number = 0; street = "not known"; House(int number, String street) { this.number = number; this.street = street; void setnumber(int number){ this.number = number; 19 String getstreet() { 20 return street; void setstreet(string street){ 23 this.street = street; CP4044 Lecture 5 17 CP4044 Lecture 5 18

4 CP4044 Lecture 5 19 Data Integrity Problems We cannot trust users of our classes to use them in the way we intended. The internal state of an object get damaged We need to maintain the integrity of the data TV ison : boolean channel : int TV() TV kevstv; kevstv = new TV(); kevstv.channel = 2; Data Hiding Ensure integrity of the data Other classes cannot directly access the data, thus cannot corrupt it. Hide internal workings of class Provides a minimal interface. Reduces complexity when using the class or visualising a whole system. Others can treat class as a black box. They don t need to know how it works, just assume it meets specifications. TV kevstv; kevstv = new TV(); kevstv.channel = -768; We need to make sure the channel is between 1 and 3 CP4044 Lecture 5 20 Public and Private Members Instance variables and methods are preceded with public or private keywords. Defines the interface between the class and other classes. i.e. the means of communication private class members May only be accessed from the class itself. Public and Private Members public class Person { private String name; public Person () { this.name = "unknown"; public void setname(string name) { this.name = name; public String getname() { return name; public class members May be accessed from any class. invalid Person p1 = new Person(); p1.name = "Billy"; valid Person p1 = new Person(); p1.setname("billy"); CP4044 Lecture 5 21 CP4044 Lecture 5 22 Protected Members In some examples you will see protected in place of public or private. This is something we are not ready to cover yet. For now assume protected does the same thing as private. This is effectively what it does for the types of programs we are looking at on this module. Rules for Data Hiding Don t allow direct external access to instance variables. Make all instance variables private. Provide public methods to access data. Validate data before assigning to instance variables. Provide other methods on a need to know basis. Only make public the methods useful outside the class. Keep the interface as simple as possible. CP4044 Lecture 5 23 CP4044 Lecture 5 24

5 CP4044 Lecture 5 25 Rational Number Class Worked Example Development of a Rational Class For storing fractions, e.g. 1/7 Cannot be stored accurately as floating point numbers Store as a pair of integers Class provides methods for operating on rational numbers CP4044 Lecture 5 26 Rational Number Operators Test Harness public class RatDemo1 { RatNumber k = new RatNumber(3, 4); k.show(); Object construction Method call We should reduce results to their lowest terms (50, 100) (1,2) CP4044 Lecture 5 27 CP4044 Lecture 5 28 Rational Number Class public class RatNumber { int num; int den; public RatNumber(int x, int y) { num = x; den = y; Instance variables constructor Results C:\>javac RatNumber.java C:\>javac RatDemo1.java C:\>java RatDemo1 (3/4) public String tostring() { return "(" + num + "/" + den + ")"; Method CP4044 Lecture 5 29 CP4044 Lecture 5 30

6 CP4044 Lecture 5 31 New Test Harness public class RatDemo { RatNumber k, l, m; k = new RatNumber(3, 4); l = new RatNumber(5, 6); m = k.add(l); System.out.println( m.tostring() ); Call by value Add Method Implementation public RatNumber add(ratnumber a) { result = new RatNumber(0,0); result.num = num * a.den + a.num * den; result.den = den * a.den; return result; (38/24) CP4044 Lecture 5 32 Alternative Add Method Implementation public RatNumber add(ratnumber a) { return new RatNumber( num * a.den + a.num * den, den * a.den); CP4044 Lecture 5 33 Adding an Integer to a Rational public RatNumber add(int a) { return new RatNumber(num + a * den, den); public class RatDemo { RatNumber k, l, m; k = new RatNumber(3, 4); l = new RatNumber(5, 6); m = K.add(L); System.out.println( m ); m = m.add(2); System.out.println( m ); (38/24) (86/24) CP4044 Lecture 5 34 Simplification private RatNumber simplify(ratnumber a) { int gcd; gcd = euclid(a.num, a.den); a.num /= gcd; a.den /= gcd; return a; private int euclid(int a, int b) { if (b == 0) return a; else return euclid(b, a % b); CP4044 Lecture 5 35 Euclid is a Recursive Method private int euclid(int a, int b) { if (b == 0) return a; euclid(27,45) else return euclid(45,27) return euclid(b, a % b); euclid(45,27) return euclid(27,18) euclid(27,18) return euclid(18,9) euclid(18,9) return euclid(9,0) euclid(9,0) return 9 CP4044 Lecture 5 36

7 CP4044 Lecture 5 37 Simplification public RatNumber add(ratnumber a) { return simplify (new RatNumber( num * a.den + a.num * den, den * a.den)); public RatNumber add(int a) { return simplify ( new RatNumber(num + a * den, den)); Array of Rationals public class RatDemo { RatNumber[] nums; nums = new RatNumber[10]; (3/6) (4/7) (5/8) for (int i = 0; i < 10; i++) { (3/9) int j = i % 3; (4/10) 0.4 nums[i] = new RatNumber(j + 3, i + 6); (5/11) for (int i = 0; i < 10; i++) System.out.println(nums[i].toString() (3/12) 0.25 (4/13) (5/14) "\t"+nums[i].todouble() );(3/15) 0.2 CP4044 Lecture 5 38 Sorting import java.util.arrays; Arrays.sort( array, comparator ); public interface Comparator<T> { public abstract int compare( T obj1, T obj2 ); public abstract boolean equals(object obj); CP4044 Lecture 5 39 Sorting import java.util.comparator; public class AscOrder implements Comparator<RatNumber> { // Method to compare RatNumber objects. public int compare(ratnumber a, RatNumber b) { return (a.num * b.den)-(a.den * b.num); Define the method compare( a, b ) to return +ve int if a > b -ve int if a < b 0 if a equals b CP4044 Lecture 5 40 Sorted RatNumber Objects Arrays.sort(nums, new AscOrder); System.out.println("Ascending order"); for(int i=0;i<10;i++) System.out.println(nums[i]+"\t"+nums[i].toDecimal(); Ascending order: (3/15) 0.2 (3/12) 0.25 (4/13) (3/9) (5/14) (4/10) 0.4 (5/11) (3/6) 0.5 (4/7) (5/8) CP4044 Lecture 5 41 Summary Object orientation State and behavior Data hiding public and private member variables Instantiating a class - objects User-defined classes Constructors, instance variables, methods Developing a class - RatNumber Using a Test Harness to develop a class Sorting arrays CP4044 Lecture 5 42

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

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

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes 1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist

More information

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

1B1b Classes in Java Part I

1B1b Classes in Java Part I 1B1b Classes in Java Part I Agenda Defining simple classes. Instance variables and methods. Objects. Object references. 1 2 Reading You should be reading: Part I chapters 6,9,10 And browsing: Part IV chapter

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

MIDTERM REVIEW. midterminformation.htm

MIDTERM REVIEW.   midterminformation.htm MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture

More information

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

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables

More information

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 15 Class Relationships Outline Problem: How can I create and store complex objects? Review of static methods Consider static variables What about objects

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Objects and Classes: Working with the State and Behavior of Objects

Objects and Classes: Working with the State and Behavior of Objects Objects and Classes: Working with the State and Behavior of Objects 1 The Core Object-Oriented Programming Concepts CLASS TYPE FACTORY OBJECT DATA IDENTIFIER Classes contain data members types of variables

More information

Announcements/Follow-ups

Announcements/Follow-ups Announcements/Follow-ups Midterm #2 Friday Everything up to and including today Review section tomorrow Study set # 6 online answers posted later today P5 due next Tuesday A good way to study Style omit

More information

Fundamental Java Methods

Fundamental Java Methods Object-oriented Programming Fundamental Java Methods Fundamental Java Methods These methods are frequently needed in Java classes. You can find a discussion of each one in Java textbooks, such as Big Java.

More information

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

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

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7 Administration Classes CS 99 Summer 2000 Michael Clarkson Lecture 7 Lab 7 due tomorrow Question: Lab 6.equals( SquareRoot )? Lab 8 posted today Prelim 2 in six days! Covers two weeks of material: lectures

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming A programming

More information

What is an algorithm?

What is an algorithm? Announcements CS 142 Objects/Classes in C++ Program 7 has been assigned - due Sunday, April 19 th by 11:55pm 4/16/2015 CS 142: Object-Oriented Programming 2 Definitions A class is a struct plus some associated

More information

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #23: OO Design, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia HW#5 due Tuesday And if you re cheating on (or letting others see your) HW#5

More information

What is an algorithm?

What is an algorithm? Announcements CS 142 Objects/Classes in C++ Program 7 has been assigned - due Sunday, Nov. 23 rd by 11:55pm 2 Definitions A class is a struct plus some associated functions that act upon variables of that

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Sung Hee Park Department of Mathematics and Computer Science Virginia State University September 18, 2012 The Object-Oriented Paradigm

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

Java and OOP. Part 2 Classes and objects

Java and OOP. Part 2 Classes and objects Java and OOP Part 2 Classes and objects 1 Objects OOP programs make and use objects An object has data members (fields) An object has methods The program can tell an object to execute some of its methods

More information

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

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 3: C# language basics II Lecture Contents 2 C# basics Methods Arrays Methods 3 A method: groups a sequence of statement takes input, performs actions, and

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 5 Anatomy of a Class Outline Problem: How do I build and use a class? Need to understand constructors A few more tools to add to our toolbox Formatting

More information

CS 1316 Exam 1 Summer 2009

CS 1316 Exam 1 Summer 2009 1 / 8 Your Name: I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Georgia Tech community. CS 1316 Exam 1 Summer 2009 Section/Problem

More information

Classes Classes 2 / 35

Classes Classes 2 / 35 Classes 1 / 35 Classes Classes 2 / 35 Anatomy of a Class By the end of next lecture, you ll understand everything in this class definition. package edu. gatech. cs1331. card ; import java. util. Arrays

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

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

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology 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

More information

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017 CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : An interface defines the list of fields

More information

CMSC 341. Nilanjan Banerjee

CMSC 341. Nilanjan Banerjee CMSC 341 Nilanjan Banerjee http://www.csee.umbc.edu/~nilanb/teaching/341/ Announcements Just when you thought Shawn was going to teach this course! On a serious note: register on Piazza I like my classes

More information

BM214E Object Oriented Programming Lecture 8

BM214E Object Oriented Programming Lecture 8 BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does

More information

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

The Notion of a Class and Some Other Key Ideas (contd.) Questions: The Notion of a Class and Some Other Key Ideas (contd.) Questions: 1 1. WHO IS BIGGER? MR. BIGGER OR MR. BIGGER S LITTLE BABY? Which is bigger? A class or a class s little baby (meaning its subclass)?

More information

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17 CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 3: C# language basics II Lecture Contents 2 C# basics Methods Arrays 1 Methods : Method Declaration: Header 3 A method declaration begins with a method header

More information

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI 621213 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Sub code: CS2203 SEM: III Sub Name: Object Oriented Programming Year: II UNIT-I PART-A 1. What is

More information

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

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 1 Problem Ralph owns the Trinidad Fruit Stand that sells its fruit on the street, and he wants to use a computer

More information

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program

More information

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

Java Magistère BFA

Java Magistère BFA Java 101 - Magistère BFA Lesson 2 Stéphane Airiau Université Paris-Dauphine Lesson 2 (Stéphane Airiau) Java 1 Object Oriented Programming in Java Lesson 2 (Stéphane Airiau) Java 2 Objects and Classes An

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

More information

Abstract Classes and Interfaces

Abstract Classes and Interfaces Abstract Classes and Interfaces Reading: Reges and Stepp: 9.5 9.6 CSC216: Programming Concepts Sarah Heckman 1 Abstract Classes A Java class that cannot be instantiated, but instead serves as a superclass

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

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

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects ÿþýûú Overview CSE 142 Anatomy of an object: constructors, methods, and instance variables Quick Review Creating, naming, and using objects Creating (empty) classes and instances of them Today Details

More information

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers This Week More on Java classes School of Computer Science University of St Andrews Graham Kirby Alan Dearle Constructors Modifiers cdn.videogum.com/img/thumbnails/photos/commenter.jpg http://www.rodgersleask.co.uk/images/sc2.jpg

More information

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:

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: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

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.

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. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Creating and Using Objects

Creating and Using Objects Creating and Using Objects 1 Fill in the blanks Object behaviour is described by, and object state is described by. Fill in the blanks Object behaviour is described by methods, and object state is described

More information

CS 1331 Exam 1 ANSWER KEY

CS 1331 Exam 1 ANSWER KEY CS 1331 Exam 1 Fall 2016 ANSWER KEY Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score. Signing signifies you are aware of and in

More information

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 2 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda a quick look back (Monday s class) assignments a1 is due on Monday a2 will be available on Monday and is due the following

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Instance Variables if Statements Readings This Week s Reading: Review Ch 1-4 (that were previously assigned) (Reminder: Readings

More information

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others COMP-202 Objects, Part III Lecture Outline Static Member Variables Parameter Passing Scopes Encapsulation Overloaded Methods Foundations of Object-Orientation 2 Static Member Variables So far, member variables

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits

More information

Software Systems Development Unit AS1: Introduction to Object Oriented Development

Software Systems Development Unit AS1: Introduction to Object Oriented Development New Specification Centre Number 71 Candidate Number ADVANCED SUBSIDIARY (AS) General Certificate of Education 2014 Software Systems Development Unit AS1: Introduction to Object Oriented Development [A1S11]

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i

More information

Objects as a programming concept

Objects as a programming concept Objects as a programming concept IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4:

More information

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name: CS-140 Fall 2018 Test 2 Version A Nov. 12, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, or F if the statement is false. (a) X T F : A class in Java contains fields, and

More information

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score. CS 1331 Exam 1 Fall 2016 Name (print clearly): GT account (gpburdell1, msmith3, etc): Section (e.g., B1): Signature: Failure to properly fill in the information on this page will result in a deduction

More information