ECE 122. Engineering Problem Solving with Java

Similar documents
ECE 122. Engineering Problem Solving with Java

Packages & Random and Math Classes

Using Classes and Objects. Chapter

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects

Formatting Output & Enumerated Types & Wrapper Classes

We now start exploring some key elements of the Java programming language and ways of performing I/O

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

A variable is a name for a location in memory A variable must be declared

ECE 122. Engineering Problem Solving with Java

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Basic Introduction, Classes and Objects SEEM

Objects and Classes -- Introduction

Java Basic Introduction, Classes and Objects SEEM

Anatomy of a Class Encapsulation Anatomy of a Method

Using Classes and Objects

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Using Classes and Objects

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

ECE 122. Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

JAVA: A Primer. By: Amrita Rajagopal

1 Shyam sir JAVA Notes

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CS1004: Intro to CS in Java, Spring 2005

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Lab5. Wooseok Kim

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

CHAPTER 7 OBJECTS AND CLASSES

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

Lab5. Wooseok Kim

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

Classes and Objects Part 1

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

ECE 122. Engineering Problem Solving with Java

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Introduction to Computer Science I

printf( Please enter another number: ); scanf( %d, &num2);

CHAPTER 7 OBJECTS AND CLASSES

USING LIBRARY CLASSES

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Chapter 4: Writing Classes

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Defining Classes and Methods

Methods (Deitel chapter 6)

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

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Class object initialization block destructor Class object

PIC 20A The Basics of Java

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

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

Class Libraries and Packages

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

Chapter 3: Using Classes and Objects

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Methods (Deitel chapter 6)

COMP-202 Unit 5: Basics of Using Objects

The Dynamic Typing Interlude

COMP-202 Unit 8: Basics of Using Objects

Chapter 5 Names, Bindings, Type Checking, and Scopes

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Memory and C++ Pointers

CSE 307: Principles of Programming Languages

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Defining Classes and Methods

VARIABLES AND TYPES CITS1001

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CS112 Lecture: Primitive Types, Operators, Strings

9 Working with the Java Class Library

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CS 302: Introduction to Programming

Lecture #5 Kenneth W. Flynn RPI CS

ECE 122. Engineering Problem Solving with Java

Garbage Collection (1)

Computational Expression

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

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

STUDENT LESSON A5 Designing and Using Classes

Lecture Notes for CS 150 Fall 2009; Version 0.5

Intro. Scheme Basics. scm> 5 5. scm>

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

Object Reference and Memory Allocation. Questions:

Sample Copy. Not for Distribution.

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects

Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding referencing Requires understanding of how computer memory works Garbage collection Java automatically cleans up unused items Generating random numbers in Java programs Why is this important?

Objects in the computer Abstractions for the parts of a problem that matter Objects contain data items Data description: data value My car has a model year, an integer, with value 1999 Data values are stored in variables Variables storing the data values for a particular object are the object s instance variables The values of the object s instance variables form the object s state

Classes Class: a template for creating objects Bank account class String class Student class Every Java object is an instance of some class The class of an object determines its data description and functionality

English to Java dictionary Piece of data: value Kind of data: type Place to store a value: variable Structured group of variables: object Kind of object: class Object of a particular class: class instance

Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An object reference variable holds the address of an object The object itself must be created separately

Creating Objects Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); Creating an object is called instantiation An object is an instance of a particular class

Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods count = title.length() A method may return a value, which can be used in an assignment or expression A method invocation can be thought of as asking an object to perform a service

Reference Variables What happens when we create a string? String teststr = new String ("Hello."); 1. Locations are allocated in memory for the string 2. 8 bit characters fill up the location in the memory 3. Address of first character saved in another location called teststr So, we can say that teststr is a pointer to a series of characters in memory 2047 2048 2049 2050 2051 2052 2053 2054 2055 H e l l o.

Reference Variables 1544 1555 1556 1557 1558 1559 1560 1561 1562 teststr 2047 2048 2049 2050 2051 2052 2053 2054 2055 H e l l o.

References Primitive variable contains the value itself, but an object variable contains the address of the object An object reference can be thought of as a pointer to the location of the object Rather than dealing with arbitrary addresses, we often depict a reference graphically num1 38 name1 "Steve Jobs"

Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: Before: num1 38 num2 96 num2 = num1; After: num1 38 num2 38

Reference Assignment For object references, assignment copies the address: Before: name1 name2 "Steve Jobs" "Steve Wozniak" name2 = name1; After: name1 name2 "Steve Jobs"

Aliases Two or more references that refer to the same object are called aliases of each other One object can be accessed using multiple reference variables Aliases can be useful, but should be managed carefully Changing an object through one reference changes it for all of its aliases There is really only one object

Classes A class can contain data declarations and method declarations int acctnumber; float acctbalance; Data declarations Method declarations

Bank Account Example acct1 72354 acctnumber balance 102.56 acct2 69713 acctnumber balance 40.00

Garbage Collection If object no longer has any valid references to it It can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically Returns an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection

The String Class Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; This is special syntax that works only for strings Each string literal (enclosed in double quotes) represents a String object title = new String( Java Software Solutions ); alternative

String Methods Once a String object has been created, neither its value nor its length can be changed Thus we say that an object of the String class is immutable Several methods of the String class return new String objects They are modified versions of the original

String Class

String Indexes It is occasionally helpful to refer to a particular character within a string This can be done by specifying the character's numeric index The indexes begin at zero in each string In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4

Class Libraries A class library is a collection of classes that we can use when developing programs The Java standard class library is part of any Java development environment Its classes are not part of the Java language, but we rely on them heavily Various classes we've already used (System, EasyIn, String) are part of the Java standard class library Other class libraries can be obtained through third party vendors, or you can create them yourself

Packages The classes of the Java standard class library are organized into packages Some of the packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing

The import Declaration All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*; That's why we didn't have to import the System or String classes explicitly in earlier programs

Random Numbers Streams of numbers that have been produced by a process that is believed to be random Truly random numbers. One example might be the spinning of a roulette wheel. Most truly random activities involve some sort of physical process which is difficult to replicate Unfortunately, computers are not very effective at generating truly random numbers Use pseudorandom number generator

The Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers A Random object performs complicated calculations Based on a seed value to produce a stream of seemingly random values

java.util.random The java.util.random class a more controlled way to generate random numbers. If we set the same seed, we get the same random sequence. Random generator = new Random(); Random generator2 = new Random(seed); long seed;

java.util.random Methods: int k = generator.nextint (); All 2 32 possible int values are produced with (approximately) equal probability int k = generator.nextint (n); 0 k < n double x = generator.nextdouble (); 0 x < 1

Summary Very important to understand the difference between objects and classes The new keyword creates (instantiates) objects in memory. A reference (pointer) is used to indicate the location of the object Note that there may be multiple references to the same object Strings are immutable. They cannot be lengthened once they are instantiated Random number are useful. Java provides a class which generates pseudorandom numbers