Programming II (CS300)

Similar documents
Programming II (CS300)

Programming II (CS300)

Programming II (CS300)

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

Chapter 6 Introduction to Defining Classes

CS1004: Intro to CS in Java, Spring 2005

Object-Oriented Programming Concepts

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

CS 302 Week 9. Jim Williams

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 4: Writing Classes

CHAPTER 7 OBJECTS AND CLASSES

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

ECE 122. Engineering Problem Solving with Java

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

2. Introducing Classes

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

CHAPTER 7 OBJECTS AND CLASSES

Anatomy of a Class Encapsulation Anatomy of a Method

SSE3052: Embedded Systems Practice

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

CSC Java Programming, Fall Java Data Types and Control Constructs

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

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

Introduction to Programming Using Java (98-388)

Object Oriented Programming

Implementing Subprograms

VARIABLES AND TYPES CITS1001

Assignment 1 due Monday at 11:59pm

Object-Oriented Programming

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

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

Object Oriented Programming

Chapter 3 Objects and Classes

Chapter 4 Defining Classes I

9 Working with the Java Class Library

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Java Review. Fundamentals of Computer Science

CSCE 156 Computer Science II

JAVA GUI PROGRAMMING REVISION TOUR III

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

Recursion 1. Recursion is the process of defining something in terms of itself.

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

The Object Concept. Object-Oriented Programming Spring 2015

CS 231 Data Structures and Algorithms, Fall 2016

OBJECT ORIENTED PROGRAMMING USING C++

Exercises Software Development I. 08 Objects II. Generating and Releasing Objects (Constructors/Destructors, this, Object cloning) December 3rd, 2014

CMSC 132: Object-Oriented Programming II

Abstract Data Types and Encapsulation Concepts

CMSC 4023 Chapter 11

An Introduction to C++

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

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Mobile Application Programming. Objective-C Classes

1 Shyam sir JAVA Notes

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

Lecture 7: Classes and Objects CS2301

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

StackVsHeap SPL/2010 SPL/20

CS 251 Intermediate Programming Methods and Classes

CS112 Lecture: Defining Instantiable Classes

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Classes. Classes as Code Libraries. Classes as Data Structures

Ticket Machine Project(s)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Java Primer 1: Types, Classes and Operators

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Handout 7. Defining Classes part 1. Instance variables and instance methods.

ITI Introduction to Computing II

Programming in the Large II: Objects and Classes (Part 1)

Object Reference and Memory Allocation. Questions:

Methods and Data (Savitch, Chapter 5)

ECOM 2324 COMPUTER PROGRAMMING II

Example: Fibonacci Numbers

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

Create a Java project named week9

CPS 506 Comparative Programming Languages. Programming Language

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

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

MIDTERM REVIEW. midterminformation.htm

Informatik II (D-ITET) Tutorial 6

CS 251 Intermediate Programming Methods and More

COP 3330 Final Exam Review

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

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.

ITI Introduction to Computing II

Programming II (CS300)

Unit 5: More on Classes/Objects Notes

Lesson 6 Introduction to Object-Oriented Programming

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

Software Development With Java CSCI

Object Oriented Design

Transcription:

1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018

Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management in Java Keep in Mind

Object-Oriented Programming Paradigm 3 Object-oriented (OO) design goals: Improvement over the procedural/structured programming paradigm Robustness Adaptability Reusability Emphasis on data rather than algorithms Data and associated operations are unified Grouping objects with common attributes, operations and semantics

Object-Oriented Programming Paradigm 4 Procedural/Structured Programming Paradigm Programs are designed around the operations Object-Oriented Programming Paradigm Programs are designed around the data Algorithmic problem decomposition Data-centric problem decomposition

Object-Oriented Programming Paradigm 5 Object-Oriented Thinking Everything in the world is an object Any system consists of a set of interacting objects Interactions among the objects inside and outside the system Flower Tree Student House

Object-Oriented Programming Paradigm 6 Object Oriented Design Principles Abstraction Encapsulation Modularity

Object-Oriented Programming Paradigm 7 Object The main actor in Object-oriented programming paradigm Instance of exactly one class Represents the properties of a single instance of a class Class A class represents a Data type (prototype or model) A class represents a description of the common properties of a set of objects A class can be viewed as a recipe on how to make or generate objects The class should be defined before creating any instance (object) of that class Properties Data attributes Methods Sequence of instructions that a class or an object follows to perform a task (functions/capabilities)

Objects and References 8 Object Concept An object is an instance of a Class An object is an encapsulation of Data An object has Identity (a unique reference) State (also called fields or variables) Each object has its own variables Behaviors All objects instances of the same class share the methods defined by the class

Objects and References 9 Class Concept A class is used as a template or pattern to create new objects A class defines the variables that hold the object s state the methods that describe the object s behaviors

Objects and References Abstraction Properties Entity Operations Object-Oriented Programming Class Data (State) Methods (Behaviors) 10 Object 1 Instantiate Instantiate Instantiate Object n Object2

Objects and References 11 Example: Class definition and object instantiation public class Student { private String name; // Student s name. private double test1, test2, test3; // Grades on three tests. public double getaverage(){ // compute average test grade // code goes here (method implementation) } } // end of class Student

Objects and References 12 Object Instantiation (Example)

Objects and References 13 Object Instantiation (Example)

Objects and References 14 Object Instantiation (Example): References illustration 864 Instance1of Student 1000 1024 Instance2of Student std = 864 std std1 Instance1 of Student (@ 864) 3200 std1 = 1000 Instance2 of Student 5436 std2 = 1000 std2 (@ 1000) 5508 std3 = null X std3

Objects and References 15 Object Instantiation (Example)

Objects and References 16 Object Instantiation (Example) Object no longer accessed Garbage!

Objects and References 17 There are only eight primitive types in Java byte, short, int, long, float, double, char, and boolean. Any other type including String is a reference type All variables of a reference type (non-primitive type) are objects and are accessed via a reference In Java, fields of classes and objects that do not have an explicit initializer and elements of arrays are automatically initialized with the default value for their type false for boolean, 0 for all numerical types, null for all reference types

Objects and References 18 Object Behaviors Constructors Constructors are invoked only when the object is created Constructors initialize the state of an object upon creation Accessor Methods An accessor method is used to return the value of a private field Accessor or getter: the method name begins with the prefix get

Objects and References 19 Object Behaviors Accessor Methods An accessor method is used to return the value of a private field Accessor or getter: the method name begins with the prefix get Mutator Methods A mutator method is used to set a value of a private field A mutator method does not have a return type Mutator or setter: the method name begins with the prefix set Why it is recommended to use Accessors and Mutators to access to a private field? One of the ways to enforce encapsulation

Objects and References 20 Constructor (Example) public class Person { //Private fields private String firstname; private String lastname; private String address; // Default Constructor public Person(){ firstname = " "; lastname = " "; address = " "; } // Defined Constructor public Person(String firstname, String lastname, String address) { this.firstname = firstname; this.lastname = lastname; this.address = address; } } // Declare two variables of type Person Person person1, person2; //references to objects of type Person // Create two Person objects and store their references in person1 and person2 variables Person person1 = new Person(); Person person2 = new Person( Mouna, KACEM, Madison );

Objects and References 21 Accessors and Mutators Examples //Accessor for firstname public String getfirstname(){ return firstname; } //Accessor for lastname public String getlastname(){ return lastname; } //Mutator for firstname public void setfirstname(string firstname) { this.firstname = firstname; } //Mutator for address public void setaddress(string address){ this.address = address; }

Objects and References 22 Message Passing An Object-Oriented application is a set of cooperating objects that communicate with each other In Java, "message passing" is performed by invoking (calling) methods A message is sent to an object using the dot notation : objectreference.methodx(); objectreference.fieldy; When an object receives a message, it looks for the corresponding method (ie. with the same signature) in its class. Then, it invokes and executes that method.

Using Objects 23 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management in Java Keep in Mind

Memory Management in Java 24 What s Memory Management? Memory management is the process of managing the memory space (of the RAM) allocated to a program while it is running A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program (i.e. the JVM represents the Java application) Managing the memory space used by a JVM includes Allocating Java code, called methods and their local variables in the memory Allocating new created objects Removing unused objects

Memory Management in Java 25 Java Memory Model (1) The Java memory model specifies how the Java Virtual Machine (JVM) uses the computer memory (RAM) A computer allocates mainly two dynamic areas of memory for a program: Stack to store information about method calls When a piece of code calls a method, information about the call is placed on the stack. When the method returns, that information is popped off the stack Heap contains all objects created by the application Heap (memory Allocation) Code Static Memory Stack (method call) console.log() main() JVM

Memory Management in Java 26 Thread1 Stack Thread2 Stack Heap JVM Java Memory Model (2) Each thread running in the JVM has its own stack (Thread Stack) All threads running in the JVM share the heap

Memory Management in Java 27 Java Memory Model (3) A thread stack contains information about The methods called by a thread to reach the current point of execution (call stack) All local variables for each method being executed Local variables related to one thread are not visible to the all other threads including local variables of primitive data types (boolean, char, byte, short, int, long, float, double) If two threads are executing the exact same code (multi-thread applications), Each thread will create its own local variables of that code in its own thread stack Thread1 Stack Thread2 Stack method_f() method_a() Local variable1 Local variable1 Local variable2 Local variable2 Local variable3 method_g() method_b() Local variablex Local variabley Heap JVM

Memory Management in Java 28 Java Memory Model (2) The heap contains all objects created in the Java application Including the object version of primitives types (Boolean, Byte, Character, Double, Float, Integer, Long, Short, and Void) Thread1 Stack method_f() Local variable1 Local variable2 method_g() Local variablex Thread2 Stack method_a() Local variable1 Local variable2 Local variable3 method_b() Local variabley Objects on the heap are visible to all threads Object1 Heap Object3 Object4 Object2 Object5 JVM

Memory Management in Java 29 Garbage Collection Java Garbage Collector is responsible for freeing objects when they are no longer accessed Reclaims heap space No manual garbage collection in Java Fully automatic process which runs periodically and performs the real destruction of the objects

Keep in Mind 30 A reference is a variable that stores the memory address where an object is temporarily stored in the stack or the special reference null. An object can be referenced by multiple references == operator is used to compare two references It returns true if the two references refer to the same object = operator makes a reference variable refer to another object. operator allows the selection of object s method or access to its fields

Keep in Mind 31 Reference types For reference types and arrays, = is a reference assignment rather than an object copy. (It does not make a copy of object values. Instead, it copies addresses) For reference types and Strings, equals should be used instead of == to test if two objects have identical states In order to enforce encapsulation, use accessors and mutators to access to an object s private fields Garbage collection Automatic reclaiming of unreferenced memory