Introduc)on to Classes and Objects. Dr. Bowen Hui Computer Science University of Bri)sh Columbia Okanagan

Size: px
Start display at page:

Download "Introduc)on to Classes and Objects. Dr. Bowen Hui Computer Science University of Bri)sh Columbia Okanagan"

Transcription

1 Introduc)on to Classes and Objects Dr. Bowen Hui Computer Science University of Bri)sh Columbia Okanagan

2 Previously Java constructs: Variables (int, double, String) Methods Condi)onal (if, if-else) Java library: Scanner Random Basic programs: Calculator Guess number game 2

3 More Interes)ng Programs Programs about Places People Pets? Not many things are int, double, String! Maybe composite of basic Java types? 3

4 More Interes)ng Programs Programs about Places People Pets? Not many things are int, double, String! Maybe composite of basic Java types? Need: bigger objects with interac)on abili)es 4

5 Example Abili)es Objects reac)ng to environment

6 Example Abili)es Objects reac)ng to user feedback 6

7 Example Abili)es Objects reac)ng to other objects Share Cookie 7

8 Today s Objec)ves 1. To be able to specify a generic object 2. To create objects 3. To have object remember informa)on 4. To have object react to environment/user 8

9 1. Specify Generic Object How? Blueprint (class) A generic descrip)on of those types of objects What goes into a blueprint? Traits (instance data) Things we want to keep track of about these objects Behaviours (methods) Things that these objects can do 9

10 1. Specify Generic Object How? Blueprint (class) A generic descrip)on of those types of objects What goes into a blueprint? Traits (instance data) Things we want to keep track of about these objects Behaviours (methods) Things that these objects can do 10

11 Example What is the blueprint for a dog? Example traits? Name, size, colour Hunger level, happiness Example behaviours? Eat, sleep, play, bark Shake a paw, roll over, sit 11

12 Example What is the blueprint for a dog? Example traits? Name, size, colour Hunger level, happiness Example behaviours? Eat, sleep, play, bark Shake a paw, roll over, sit 12

13 Example What is the blueprint for a dog? Example traits? Name, size, colour Hunger level, happiness Example behaviours? Eat, sleep, play, bark Shake a paw, roll over, sit 13

14 Defining Traits Type of informa)on Number? Phrase? Truth value? Other? may create your own later Declare as variables inside your class 14

15 Defining Behaviours Special method: constructor Main purpose: ini)alize traits Dog( String petname ) name = petname; no return type stomach = 0; isfull = false; // starts off empty Other behaviours: define as regular methods Example: void eatsnacks( int cookies ) stomach = stomach + cookies; if( stomach > 5 ) isfull = true; 15

16 Defining Behaviours Special method: constructor Main purpose: ini)alize traits Dog( String petname ) name = petname; no return type stomach = 0; isfull = false; // starts off empty Other behaviours: define as regular methods Example: void eatsnacks( int cookies ) stomach = stomach + cookies; if( stomach > 5 ) isfull = true; 16

17 Visual Representa)on Dog class: Data declarations Dog() bark() Method declarations eatsnacks() 17

18 Transla)ng into Java Generic dog Traits Behaviours Braces have to match Steps: 1. Create class template 2. Declare instance data 3. Define methods Capital lefer class ClassName // instance data // methods 18

19 Create Class Template class Dog // instance data // methods Dog() bark() eatsnacks() 19

20 Declare Instance Data class Dog // instance data Dog() // methods bark() eatsnacks() 20

21 Declare Instance Data class Dog // instance data Dog() // methods bark() Scope for instance data eatsnacks() 21

22 Define Methods class Dog // instance data // methods Dog( String petname ) name = petname; stomach = 0; isfull = false; Constructor must have the same name as the class Dog() bark() eatsnacks() 22

23 Define Methods class Dog // instance data // methods Dog( String petname ) name = petname; stomach = 0; isfull = false; How come the constructor can use name, stomach, isfull? Dog() bark() eatsnacks() 23

24 Define Methods class Dog // instance data // methods Dog( String petname ) name = petname; stomach = 0; isfull = false; Note: any method in the class can reference the class s instance data Dog() bark() eatsnacks() 24

25 Define Methods class Dog // instance data // methods Dog( String petname ) name = petname; stomach = 0; isfull = false; // next column // continue void bark() System.out.println( woof ); Dog() bark() eatsnacks()

26 Define Methods class Dog // instance data // continue void bark() System.out.println( woof ); // methods Dog( String petname ) name = petname; stomach = 0; isfull = false; // next column void eatsnacks( int cookies ) stomach += cookies; if( stomach > 5 ) isfull = true; Dog class is completed Dog() bark() eatsnacks()

27 Today s Objec)ves 1. To be able to specify a generic object 2. To create objects 3. To have object remember informa)on 4. To have object react to environment/user 27

28 2. Create Objects Purpose of blueprint Describes a generic object of that class Need to create object Use reserved keyword: new Call constructor to specify traits of individual objects Do this inside main() Note: Behaviours don t change 28

29 Visual Representa)on new instance class template: Dog() bark() eatsnacks() object instance: Dog() bark() eatsnacks() Casper 0 false 29

30 Using new Recall variable defini)ons: int num = 5; Crea)ng new objects require different syntax Changes: Use new Call constructor method with unique info Example: Dog pet = new Dog( Casper ); Calls constructor method with a unique name 30

31 Transla)ng into Java Test class: main() Create unique objects Call its methods class TestClass public static void main() // create object // call its methods 31

32 Transla)ng into Java Test class: main() Create unique objects Call its methods class TestClass public static void main() // create object // call its methods Same as before Refers to another class in another file 32

33 Example Test Class class TestDog public static void main() Dog pet = new Dog( Casper ); pet.eatsnacks( 2 ); Use new 33

34 Example Test Class class TestDog public static void main() Dog pet = new Dog( Casper ); pet.eatsnacks( 2 ); Dog() bark() eatsnacks() Casper 0 false Pass in info unique to object upon crea)on 34

35 Example Test Class class TestDog public static void main() Dog pet = new Dog( Casper ); pet.eatsnacks( 2 ); Invoke object s method using dot operator Dog() bark() eatsnacks() Casper 2 false 35

36 Example Test Class class TestDog public static void main() Dog pet = new Dog( Casper ); pet.eatsnacks( 2 ); pet.eatsnacks( 5 ); Dog() bark() Casper 7 true eatsnacks() 36

37 Example Test Class class TestDog public static void main() Dog petboy = new Dog( Casper ); petboy.eatsnacks( 2 ); petboy.eatsnacks( 5 ); Dog petgirl = new Dog( Bitzy ); petgirl.eatsnacks( 2 ); Create as many Dog objects as needed 37

38 Example Test Class class TestDog public static void main() Dog petboy = new Dog( Casper ); petboy.eatsnacks( 2 ); petboy.eatsnacks( 5 ); Dog petgirl = new Dog( Bitzy ); petgirl.eatsnacks( 2 ); Could I have called both Dog objects with same variable name pet? 38

39 Example: One class template to create five Dog objects 39

40 Today s Objec)ves 1. To be able to specify a generic object 2. To create objects 3. To have object remember informa)on 4. To have object react to environment/user 40

41 3. Object Remembering Info Already know how to define instance data How instance data works: Each object maintains its own variables Gives object a state at any given )me When first created Casper 0 false Dog() bark() eatsnacks() 41

42 3. Object Remembering Info Already know how to define instance data How instance data works: Each object maintains its own variables Gives object a state at any given )me When first created Later Casper 0 false Casper 2 false Dog() Dog() bark() bark() eatsnacks() eatsnacks() 42

43 3. Object Remembering Info Already know how to define instance data How instance data works: Each object maintains its own variables Gives object a state at any given )me When first created Later Much later Casper 0 false Casper 2 false Casper 7 true Dog() Dog() Dog() bark() bark() bark() eatsnacks() eatsnacks() eatsnacks() 43

44 Objects Independence Instance data across objects do not interact When first created Casper 0 false Dog() bark() eatsnacks() Bitzy 0 false Dog() bark() eatsnacks() 44

45 Objects Independence Instance data across objects do not interact When first created Later Casper 0 false Casper 2 false Dog() Dog() bark() bark() eatsnacks() eatsnacks() Bitzy 0 false Bitzy 1 false Dog() Dog() bark() bark() eatsnacks() eatsnacks() 45

46 Objects Independence Instance data across objects do not interact When first created Later Much later Casper 0 false Casper 2 false Casper 7 true Dog() Dog() Dog() bark() bark() bark() eatsnacks() eatsnacks() eatsnacks() Bitzy 0 false Bitzy 1 false Bitzy 2 false Dog() Dog() Dog() bark() bark() bark() eatsnacks() eatsnacks() eatsnacks() 46

47 Today s Objec)ves 1. To be able to specify a generic object 2. To create objects 3. To have object remember informa)on 4. To have object react to outside 47

48 4. Object Reac)ng to Surrounding Already know how to define methods How to get object to be responsive? Change state over )me Change state based on interac)on with user Call different methods as indicated 48

49 Responding to User When called its name, dog barks back How to add this behaviour? New method Call it in test class 49

50 Responding to User class Dog // continue from previous template void respond( String dogname ) if( dogname.equals( name ) ) bark(); else ignoreowner(); 50

51 Responding to User class TestDog Dog casper = new Dog( Casper ); // user interaction Scanner console = new Scanner( System.in ); String petname = console.nextline(); casper.respond( petname ); // try again console = new Scanner( System.in ); petname = console.nextline(); casper.respond( petname ); Respond to user Respond to user 51

52 Responding to User class TestDog Dog casper = new Dog( Casper ); // user interaction Scanner console = new Scanner( System.in ); String petname = console.nextline(); casper.respond( petname ); // try again console = new Scanner( System.in ); petname = console.nextline(); casper.respond( petname ); 52

53 Special Method: Accessor Outside world cannot see object s instance data Solu)on: use accessors to get data value back Also called gefers Any instance data can be retrieved this way Template: returntype getvar() return var; How to write accessor method for name? 53

54 Example: getname() class Dog // instance data // methods continue from before String getname() return name; 54

55 Using getname() class TestDog Dog casper = new Dog( Casper ); String petname = casper.getname(); System.out.println( Just created a dog named + petname ); 55

56 Using getname() class TestDog Dog casper = new Dog( Casper ); String petname = casper.getname(); System.out.println( Just created a dog named + petname ); 56

57 Special Method: Mutator Outside world cannot change object s instance data Solu)on: use mutators to change data value Also called sefers Any instance data can be changed this way Template: void setvar( vartype newvalue ) var = newvalue; How to write mutator method for name? 57

58 Example: setname() class Dog // instance data // methods continue from before void setname( String newname ) name = newname; 58

59 Using setname() class TestDog Dog casper = new Dog( Casper ); String petname = casper.getname(); System.out.println( Just created a dog named + petname ); casper.setname( Big Boy ); petname = casper.getname(); System.out.println( Dog named is now + petname ); 59

60 Using setname() class TestDog Dog casper = new Dog( Casper ); String petname = casper.getname(); System.out.println( Just created a dog named + petname ); casper.setname( Big Boy ); petname = casper.getname(); System.out.println( Dog name is now + petname ); 60

61 Modeling Other Applica)ons How might you define these classes? Houses Students Recall: lab s FeedMe game What are the classes and objects? 61

62 Recall: FeedMe Game What are the classes and objects? 62

63 Modeling Other Applica)ons How might you define these classes? Houses Students Recall: lab s FeedMe game What are the classes and objects? Dog class: one dog object Apple class: five falling apples 63

64 Student s Customized Lab Games 64

65 Student s Customized Lab Games 65

66 Summary A class is a blueprint to describe group of objects Instance data represents traits Methods represents behaviours Special methods: Constructor sets up a new object Accessor retrieves trait s data value Mutator changes trait s data value Individual objects created from a class An object is an instance of a class Use reserved word: new in test class Call object s methods using. dot operator An object s instance data do not interact with another s 66

67 Summary A class is a blueprint to describe group of objects Instance data represents traits Methods represents behaviours Special methods: Constructor sets up a new object Accessor retrieves trait s data value Mutator changes trait s data value Individual objects created from a class An object is an instance of a class Use reserved word: new in test class Call object s methods using. dot operator An object s instance data do not interact with another s 67

68 Summary A class is a blueprint to describe group of objects Instance data represents traits Methods represents behaviours Special methods: Constructor sets up a new object Accessor retrieves trait s data value Mutator changes trait s data value Individual objects created from a class An object is an instance of a class Use reserved word: new in test class Call object s methods using. dot operator An object s instance data do not interact with another s 68

69 Next Class Interac)on among objects Share Cookie 69

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 First half of course SoEware examples From English to Java Template for building small programs Exposure to Java

More information

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 Review Class template: class ClassName // ahributes // methods Method template: returntype methodname( vartype 1

More information

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 Administra>on Midterm 2 To return next class Assignment 3 Available on website Due next Thursday Today Sta>c modifier

More information

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan COSC 121: Computer Programming II Dr. Bowen Hui University of Bri?sh Columbia Okanagan 1 A1 Posted over the weekend Two ques?ons (s?ll long ques?ons) Review of main concepts from COSC 111 Prac?ce coding

More information

Java Basics. CSE 373, Fall 2015 Megan Hopp

Java Basics. CSE 373, Fall 2015 Megan Hopp Java Basics CSE 373, Fall 2015 Megan Hopp Definitions Object Class Method Objects have states and behaviors. Objects are instances of classes. A class can be defined as a template/ blueprint that describes

More information

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan COSC 121: Computer Programming II Dr. Bowen Hui University of Bri?sh Columbia Okanagan 1 Quick Review Inheritance models IS- A rela?onship Different from impor?ng classes Inherited classes can be organized

More information

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

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits? Objec,ves Object-oriented programming in Java Ø Encapsula,on Ø Access modifiers Ø Using others classes Ø Defining own classes Sept 16, 2016 Sprenkle - CSCI209 1 Review: Object-Oriented Programming What

More information

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan COSC 121: Computer Programming II Dr. Bowen Hui University of Bri?sh Columbia Okanagan 1 Quick Review Representa?ve example: Animal[] mypets = new Animal[4]; mypets[0] = new Dog(); mypets[1] = new Cat();

More information

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

8/27/13. An intro to programming* *Java. Announcements. Professor Rodger. The Link. UTAs start TONIGHT! An intro to programming* *Java Announcements Professor Rodger The Link UTAs start TONIGHT! 2 1 Announcements Office hours Check the website! 3 Homework Recitation prep due BEFORE recitation on Friday Setup

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-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I 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

More information

Ticket Machine Project(s)

Ticket Machine Project(s) Ticket Machine Project(s) Understanding the basic contents of classes Produced by: Dr. Siobhán Drohan (based on Chapter 2, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

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

Classes and Objects. CGS 3416 Spring 2018

Classes and Objects. CGS 3416 Spring 2018 Classes and Objects CGS 3416 Spring 2018 Classes and Objects An object is an encapsulation of data along with functions that act upon that data. It attempts to mirror the real world, where objects have

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

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

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Gaddis_516907_Java 4/10/07 2:10 PM Page 51 Chapter 6 Lab Classes and Objects Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Lab Objectives Chapter 6 Lab Classes and Objects Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that

More information

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed Interfaces 2 ANIMAL picture food sleep() roam() makenoise() eat() 3 ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

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

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

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

Lesson 12: OOP #2, Accessor Methods (W03D4) Lesson 12: OOP #2, Accessor Methods (W03D4) Balboa High School Michael Ferraro September 3, 2015 1 / 29 Do Now In your driver class from last class, create another new Person object with these characteristics:

More information

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

More information

Express Yourself. Writing Your Own Classes

Express Yourself. Writing Your Own Classes Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

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

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

Today s Agenda. Quick Review

Today s Agenda. Quick Review Today s Agenda TA Information Homework 1, Due on 6/17 Quick Review Finish Objects and Classes Understanding class definitions 1 Quick Review What is OOP? How is OOP different from procedural programming?

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

Defining Classes and Methods

Defining Classes and Methods Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Class and Method Definitions: Outline Class Files and Separate Compilation Instance Variables Methods The Keyword this Local Variables

More information

Array Basics: Outline

Array Basics: Outline Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and

More information

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar Upon completing the course, you will understand Create, compile, and run Java programs Primitive data types Java control flow Operator Methods

More information

Recitation 09/12/2007. CS 180 Department of Computer Science, Purdue University

Recitation 09/12/2007. CS 180 Department of Computer Science, Purdue University Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University Announcements & Reminders Project 1 grades out Remember to collect them at the end of the recitation Solution up on the web

More information

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Objects and Classes (contd.) Course Lecture Slides 19 May 2010 Ganesh Viswanathan Objects and Classes Credits: Adapted from CIS3023 lecture

More information

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling) Understanding class definitions Looking inside classes (based on lecture slides by Barnes and Kölling) Main Concepts fields constructors methods parameters assignment statements Ticket Machines (an external

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

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof Object Oriented Programming SCJ2153 Class and Object Associate Prof. Dr. Norazah Yusof Classes Java program consists of classes. Class is a template for creating objects. Class normally consists of 3 components:

More information

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

Clarifying Roles. Jonathan Worthington German Perl Workshop 2007

Clarifying Roles. Jonathan Worthington German Perl Workshop 2007 Clarifying Roles Jonathan Worthington German Perl Workshop 2007 The Perl 6 Object Model The Perl 6 object model attempts to improve on the Perl 5 one Nicer, more declarative syntax One way to do things,

More information

Classes Basic Overview

Classes Basic Overview Final Review!!! Classes and Objects Program Statements (Arithmetic Operations) Program Flow String In-depth java.io (Input/Output) java.util (Utilities) Exceptions Classes Basic Overview A class is a container

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

Selection Statements and operators

Selection Statements and operators Selection Statements and operators CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

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

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

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

Darrell Bethea May 20, 2011

Darrell Bethea May 20, 2011 Darrell Bethea May 20, 2011 Program 2 due Monday Midterm in one week Moved to SN014 (next door) Will cover up to yesterday (Ch. 1-4) Future office hours moved to FB331 2 3 Briefly go over Strings and Loops

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

Methods. Methods. Mysteries Revealed

Methods. Methods. Mysteries Revealed Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information

Understanding class definitions

Understanding class definitions Objects First With Java A Practical Introduction Using BlueJ Understanding class definitions Looking inside classes 2.1 Looking inside classes basic elements of class definitions fields constructors methods

More information

Array Basics: Outline

Array Basics: Outline Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and

More information

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1 Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

CSCI 161 Introduction to Computer Science

CSCI 161 Introduction to Computer Science CSCI 161 Introduction to Computer Science Department of Mathematics and Computer Science Lecture 2b A First Look at Class Design Last Time... We saw: How fields (instance variables) are declared How methods

More information

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

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

Creating Your Own Classes

Creating Your Own Classes Creating Your Own Classes 1 Objectives At the end of the lesson, the student should be able to: Create their own classes Declare properties (fields) and methods for their classes Use the this reference

More information

Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 12/2/2015

Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 12/2/2015 CISC 1600/1610 Computer Science I Classes Professor Daniel Leeds dleeds@fordham.edu JMH 328A Data types Single pieces of information one integer int one symbol char one truth value bool Multiple pieces

More information

Classes & Objects CMSC 202

Classes & Objects CMSC 202 Classes & Objects CMSC 202 Programming & Abstrac9on All programming languages provide some form of abstrac'on Also called informa'on hiding Separa9ng how one uses a program and how the program has been

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

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

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

( &% class MyClass { }

( &% class MyClass { } Recall! $! "" # ' ' )' %&! ( &% class MyClass { $ Individual things that differentiate one object from another Determine the appearance, state or qualities of objects Represents any variables needed for

More information

AP COMPUTER SCIENCE A

AP COMPUTER SCIENCE A AP COMPUTER SCIENCE A CLASSES AND OBJECTS (1) Sep 11 2017 Week 4 http://apcs.cold.rocks 1 One More Class public static int a=1; System.out.println(a); 1 public static int a=2; http://apcs.cold.rocks 2

More information

Writing a Fraction Class

Writing a Fraction Class Writing a Fraction Class So far we have worked with floa0ng-point numbers but computers store binary values, so not all real numbers can be represented precisely In applica0ons where the precision of real

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump

More information

Week 3 Classes and Objects

Week 3 Classes and Objects Week 3 Classes and Objects written by Alexandros Evangelidis, adapted from J. Gardiner et al. 13 October 2015 1 Last Week Last week, we looked at some of the different types available in Java, and the

More information

Classes and objects. Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates

Classes and objects. Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates Classes and objects Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates Fundamentals of Computer Science Keith Vertanen Copyright 2013 A founda4on for programming any program you might want to

More information

CS 2530 INTERMEDIATE COMPUTING

CS 2530 INTERMEDIATE COMPUTING CS 2530 INTERMEDIATE COMPUTING Spring 2018 1-24-2018 Michael J. Holmes University of Northern Iowa Today s topic: Writing classes. 1 Die Objects A die has physical attributes: a specific Number of Sides

More information

Java Session. Day 2. Reference: Head First Java

Java Session. Day 2. Reference: Head First Java Java Session Day 2 shrishty_bcs11@nitc.ac.in Reference: Head First Java Encapsulation This hides the data!! How do we do it? By simply using public private access modifiers. 1. Mark the instance variables

More information

Unit 5: More on Classes/Objects Notes

Unit 5: More on Classes/Objects Notes Unit 5: More on Classes/Objects Notes AP CS A The Difference between Primitive and Object/Reference Data Types First, remember the definition of a variable. A variable is a. So, an obvious question is:

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

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

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects CS256 Computer Science I Kevin Sahr, PhD Lecture 11: Objects 1 Programs as Models remember: we write programs to solve realworld problems programs act as models of the real-world problem to be solved one

More information

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016 COMP 250 Lecture 32 polymorphism Nov. 25, 2016 1 Recall example from lecture 30 class String serialnumber Person owner void bark() {print woof } : my = new (); my.bark();?????? extends extends class void

More information

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10B Class Design By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public

More information

Recommended Group Brainstorm (NO computers during this time)

Recommended Group Brainstorm (NO computers during this time) Recommended Group Brainstorm (NO computers during this time) Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to

More information

Lecture 6 Introduction to Objects and Classes

Lecture 6 Introduction to Objects and Classes Lecture 6 Introduction to Objects and Classes Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors

More information

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,

More information

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++ Session 6 - Inheritance in C++ The most important slide of the lecture Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Why use inheritance?

More information

7. C++ Class and Object

7. C++ Class and Object 7. C++ Class and Object 7.1 Class: The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member

More information

CS 1302 Chapter 9 (Review) Object & Classes

CS 1302 Chapter 9 (Review) Object & Classes CS 1302 Chapter 9 (Review) Object & Classes Reference Sections 9.2-9.5, 9.7-9.14 9.2 Defining Classes for Objects 1. A class is a blueprint (or template) for creating objects. A class defines the state

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

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists Do-It-Yourself ArrayLists Scope Static Readings This Week: Ch 8.3-8.8 and into Ch 9.1-9.3 (Ch

More information

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR) Object Oriented Programming Part II of II Steve Ryder Session 8352 JSR Systems (JSR) sryder@jsrsys.com New Terms in this Section API Access Modifier Package Constructor 2 Polymorphism Three steps of object

More information

EEE-425 Programming Languages (2013) 1

EEE-425 Programming Languages (2013) 1 2 Learn about class concepts How to create a class from which objects can be instantiated Learn about instance variables and methods How to declare objects How to organize your classes Learn about public

More information

Static, Final & Memory Management

Static, Final & Memory Management Static, Final & Memory Management The static keyword What if you want to have only one piece of storage regardless of how many objects are created or even no objects are created? What if you need a method

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Introduction to Objects. James Brucker

Introduction to Objects. James Brucker Introduction to Objects James Brucker What is an Object? An object is a program element that encapsulates both data and behavior. An object contains both data and methods that operate on the data. Objects

More information

Create a Java project named week10

Create a Java project named week10 Objectives of today s lab: Through this lab, students will examine how casting works in Java and learn about Abstract Class and in Java with examples. Create a Java project named week10 Create a package

More information

A formal design process, part 2

A formal design process, part 2 Principles of So3ware Construc9on: Objects, Design, and Concurrency Designing (sub-) systems A formal design process, part 2 Josh Bloch Charlie Garrod School of Computer Science 1 Administrivia Midterm

More information

Declaring and ini,alizing 2D arrays

Declaring and ini,alizing 2D arrays Declaring and ini,alizing 2D arrays 4 2D Arrays (Savitch, Chapter 7.5) TOPICS Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe Game // se2ng up a 2D array final int M=3, N=4;

More information

method method public class Temperature { public static void main(string[] args) { // your code here } new

method method public class Temperature { public static void main(string[] args) { // your code here } new Methods Defining Classes and Methods (Savitch, Chapter 5) TOPICS Java methods Java objects Static keyword Parameter passing Constructors A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information