COMP 110/L Lecture 7. Kyle Dewey

Similar documents
COMP 110/L Lecture 13. Kyle Dewey

COMP 110/L Lecture 5. Kyle Dewey

LECTURE 2 (Gaya College of Engineering)

COMP 110/L Lecture 20. Kyle Dewey

COMP 110/L Lecture 4. Kyle Dewey

COMP 110/L Lecture 24. Kyle Dewey

COMP 110/L Lecture 6. Kyle Dewey

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

BM214E Object Oriented Programming Lecture 7

Selected Questions from by Nageshwara Rao

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

COMP 110/L Lecture 19. Kyle Dewey

Introduction to Java Unit 1. Using BlueJ to Write Programs

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Arrays Classes & Methods, Inheritance

COMP 110/L Lecture 10. Kyle Dewey

Array. Prepared By - Rifat Shahriyar

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

boolean add(e e); // Makes the collection contain e. // Returns true if e not already there.

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

1.00 Tutorial 3. Methods, Classes Arrays & ArrayLists. September 26 & 27, 2005

- Thus there is a String class (a large class)

AP Computer Science Unit 1. Writing Programs Using BlueJ

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

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

In our classes, we've had

AP Computer Science Unit 1. Writing Programs Using BlueJ

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 4 Defining Classes I

Java Memory Management

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

COMP15111: Introduction to Architecture

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

Constructors for classes

Topic 6: Inner Classes

CP122 CS I. Iteration

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

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

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Classes and Methods: Classes

Write code to represent

Chapter 4. Defining Classes I

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

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Class, Variable, Constructor, Object, Method Questions

Lecture 3. Lecture

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

CS111: PROGRAMMING LANGUAGE II

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 231 Data Structures and Algorithms Fall 2018

Lecture 02, Fall 2018 Friday September 7

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Java and OOP. Part 2 Classes and objects

Interfaces, collections and comparisons

AP Computer Science Unit 1. Programs

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object-Oriented Concepts

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

CSCI 161 Introduction to Computer Science

Class and Functions. Reusable codes

Just Enough Eclipse What is Eclipse(TM)? Why is it important? What is this tutorial about?

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

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Lesson 36: for() Loops (W11D1)

Array. Array Declaration:

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Games Course, summer Introduction to Java. Frédéric Haziza

*Java has included a feature that simplifies the creation of

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Tecniche di Progettazione: Design Patterns

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Chapter 5. Inheritance

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Declaring and ini,alizing 2D arrays

CS 302 Week 9. Jim Williams

Select the ONE best answer to the question from the choices provided.

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

Introduction to Java. Handout-1d. cs402 - Spring

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Classes. Classes as Code Libraries. Classes as Data Structures

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Notes on Chapter Three

COMP102: Test. 18 April, 2005

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Transcription:

COMP 110/L Lecture 7 Kyle Dewey

Outline Introduction to objects Constructors and new Instance variables Instance methods static vs. non-static

Object-Oriented Programming

Basic Idea The world is composed of objects which interact with each other in well-defined ways

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water -Task: boil water

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water faucet object -I have a faucet object...

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water faucet object pot object -...as well as a pot object

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water Interaction: fill with water faucet object pot object -The faucet can fill the pot

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water Interaction: fill with water faucet object pot object -Now the pot is filled with water

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water pot object -Now the pot is filled with water

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water Interaction: Place on top of pot object stove object

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water stove object -The pot is now on top of the stove

Basic Idea The world is composed of objects which interact with each other in well-defined ways Example: boiling water Interaction: Turn on burner stove object -Self-interactions are permitted, and even common

Creating Objects In Java, we first need a class to make an object. A class serves as a blueprint/template for an object.

Creating Objects In Java, we first need a class to make an object. A class serves as a blueprint/template for an object. Stove Class

Creating Objects In Java, we first need a class to make an object. A class serves as a blueprint/template for an object. Stove Class Stove object Stove object Stove object -The same class can be used to make different stoves -These stoves can be different from each other, perhaps even radically different. It all depends on exactly how the class is defined.

public class Declares a class, and gives it public visibility (more on that later in the course) -This should sound familiar - you ve been using it this whole time!

public class Declares a class, and gives it public visibility (more on that later in the course) public class Table {...

Constructors

Constructors Code executed upon object creation Effectively create the object Looks like a method, but no return type (not even void) and has the same name as the class

Constructors Code executed upon object creation Effectively create the object Looks like a method, but no return type (not even void) and has the same name as the class public class Table { public Table() { System.out.println( Creating table... );

Constructors Code executed upon object creation Effectively create the object Looks like a method, but no return type (not even void) and has the same name as the class public class Table { public Table() { Constructor System.out.println( Creating table... );

Executing Constructors new executes a given constructor, creating a new object in the process.

Executing Constructors new executes a given constructor, creating a new object in the process. Table t = new Table();

Example: Table.java

Constructor Parameters Just like methods, constructors can take parameters

Constructor Parameters Just like methods, constructors can take parameters public class ConsParam { public ConsParam(String str) { System.out.println(str);

Constructor Parameters Just like methods, constructors can take parameters public class ConsParam { public ConsParam(String str) { System.out.println(str); ConsParam p = new ConsParam( hi );

Example: ConsParam.java

Instance Variables

Instance Variables Declared in the class. Each object created from a class (hereafter referred to as an instance) has its own instance variables.

Instance Variables Declared in the class. Each object created from a class (hereafter referred to as an instance) has its own instance variables. public class HasInstance { int myint; // instance variable...

Instance Variables Declared in the class. Each object created from a class (hereafter referred to as an instance) has its own instance variables. public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint;

public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint; -Shift up the code to make some room

public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint; HasInstance a = new HasInstance(7); -Later on you execute this statement...

public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint; HasInstance a = new HasInstance(7); HasInstance b = new HasInstance(8); -Followed by this statement...

public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint; HasInstance a = new HasInstance(7); HasInstance b = new HasInstance(8); HasInstance a: myint: 7 -In memory, you d see that a has its own value of myint, and that is 7

-Similarly, b has its own value of myint, and that is 8 -Key point: while there is one class, there have been two objects made from this class, and each object has its own values for the instance variable. The instance variables belong to the objects, not the class. public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint; HasInstance a = new HasInstance(7); HasInstance b = new HasInstance(8); HasInstance a: HasInstance b: myint: 7 myint: 8

Example: HasInstance.java

Instance Methods

Instance Methods Define which interactions can occur between objects Declared in the class Specific to objects created from the class (instances), and operate over instance variables.

public class HasInstance { int myint; // instance variable public HasInstance(int setint) { myint = setint; -To show an example, let s take the HasInstance definition from before...

public class HasInstance2 { int myint; // instance variable public HasInstance2(int setint) { myint = setint; public void printint() { System.out.println(myInt); -...and now we add the printint instance method -The name of the class has also been changed, just so we can have both examples in two separate files (namely HasInstance.java and HasInstance2.java)

Example: HasInstance2.java

static Associates something with the class itself, as opposed to individual objects created from the class.

static Associates something with the class itself, as opposed to individual objects created from the class. public class MyClass { public static void main(string[] args) {... -You ve been defining main and all your methods this way the entire time -Java forces all source code to be in classes, so this is unavoidable. However, we haven t really gotten into proper objects yet.

static vs. non-static With static: associated with the class. Without static: associated with objects created from the class.

static vs. non-static With static: associated with the class. Without static: associated with objects created from the class. public class MyClass { public static void main(string[] args) {...

static vs. non-static With static: associated with the class. Without static: associated with objects created from the class. public class MyClass { public static void With class MyClass main(string[] args) {...

static vs. non-static With static: associated with the class. Without static: associated with objects created from the class. public class MyClass { public static void With class MyClass main(string[] args) {... public class MyClassTest { @Test public void sometest() {...

static vs. non-static With static: associated with the class. Without static: associated with objects created from the class. public class MyClass { public static void With class MyClass main(string[] args) {... With objects created from MyClassTest public class MyClassTest { @Test public void sometest() {...

Stove Example in Java Water.java Faucet.java Pot.java Stove.java BoilingWater.java