CS162: Introduction to Computer Science II

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

Introduction to Programming Using Java (98-388)

Chapter 4 Defining Classes I

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

ECE 122. Engineering Problem Solving with Java

CSC Java Programming, Fall Java Data Types and Control Constructs

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

JAVA GUI PROGRAMMING REVISION TOUR III

CHAPTER 7 OBJECTS AND CLASSES

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

VARIABLES AND TYPES CITS1001

CHAPTER 7 OBJECTS AND CLASSES

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

JAVA WRAPPER CLASSES

Chapter 4. Defining Classes I

Short Notes of CS201

CMSC131. Library Classes

CS201 - Introduction to Programming Glossary By

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

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Chapter 6 Introduction to Defining Classes

BM214E Object Oriented Programming Lecture 8

Java Primer 1: Types, Classes and Operators

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

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Operators and Expressions

CST141 Thinking in Objects Page 1

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Variables and Primitive Types

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

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

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Class object initialization block destructor Class object

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

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

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

PIC 20A Number, Autoboxing, and Unboxing

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

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

ISA 563 : Fundamentals of Systems Programming

CS 231 Data Structures and Algorithms, Fall 2016

CSCI 355 LAB #2 Spring 2004

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Lecture Set 4: More About Methods and More About Operators

Defining Classes and Methods

Goals of this Lecture

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

More About Objects and Methods

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS1004: Intro to CS in Java, Spring 2005

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

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Chapter 4: Writing Classes

Final CSE 131B Spring 2004

Another IS-A Relationship

Programming II (CS300)

Chapter 2. Procedural Programming

Binghamton University. CS-140 Fall Data Types in Java

Lecture 5: Methods CS2301

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

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

Motivation was to facilitate development of systems software, especially OS development.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 2: Using Data

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Chapter 1 Getting Started

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

CSCI 355 Lab #2 Spring 2007

ITI Introduction to Computing II

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

ESc101 : Fundamental of Computing

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lecture Set 4: More About Methods and More About Operators

Introduction to C++ with content from

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

CS558 Programming Languages

ITI Introduction to Computing II

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

CS260 Intro to Java & Android 03.Java Language Basics

Motivation was to facilitate development of systems software, especially OS development.

Lab5. Wooseok Kim

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

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

More About Objects and Methods

From C++ to Java. Duke CPS

Primitive Data and Objects

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

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

Transcription:

CS162: Introduction to Computer Science II Java Fundamentals 1 Primitive Types 2 1

Primitive types: Primitive types byte, short, int, long, float, double, char, boolean Example: int size = 42; size is a primitive variable, i.e., a variable that contains ti a data dt value of fthe declared dtype. 3 The eight primitive types in Java 4 2

Limitations All primitive types have limited ranges Exceeding the range will cause inaccurate results float and double are typically approximations don't use == to compare real types Use Math.abs(x-y) <= EPSILON where EPSILON is some small number 5 Operations on primitive types Variable holds value Assignment statements assign values E.G., int n = 10; Operations are defined as language primitives E.G., n += 5; Relational operators, etc., are defined as language primitives E.G., if (n <= 15)... 6 3

Result of logical operators AND OR NOT 7 DeMorgan s Laws!(x && y) ==!x!y!(x y) ==!x &&!y Short-circuit evaluation int x = 3; int y = 5; if ((x == y) && (y > 0))... if ((x < y) (y < 0))... 8 4

Short-circuit evaluation int x = 3; int y = 5; int z = x / y; if ((z!= 0) && ((y / z) < 5)) // OK if (((y / z) < 5) && (z!= 0)) // crash if DivideByZeroException // is not handled 9 Reference types All types that are not primitive are reference or class types Example: String greeting = "Howdy"; greeting is a reference variable, i.e., a variable that contain a reference to (address of) the memory location of the data. 10 5

Reference Types 11 Objects An object is a program entity that contains data performs certain actions The actions are called methods The actions of various objects interact to form a solution o for agiven problem 12 6

Classes A class defines the characteristics for all objects of its type A class gives a general description of what an object of the type is (instance data) what an object of the type can do (methods) 13 Objects / Classes An object is an instance of a class. A program may have many instances (objects) of one class. All instances of the same class have The same kinds of data The same methods 14 7

An Example of a Class etc. 15 Three instances of the Automobile class 16 8

Invoke a constructor Example: Instantiation Automobile bobscar = new Automobile( Sedan, 2000, 0.9,, 21405); Keyword new invokes a constructor for the Automobile class. Note: this example uses an overloaded constructor. The default constructor does not accept any parameters. 17 Object variables An object variable is used to store the address of (reference to) an object When a class is instantiated, the system creates an object variable, and stores the address of the new object therein. E.G., bobscar (previous slide) is an object variable. 18 9

Instantiation bobscar A memory cell that contains the memory address of the data for bobscar Sedan 2000 0.9 21405 19 Aliases It is possible for two variables to reference the same object. They are aliases Example: Automobile mycar = bobscar; bobscar Sedan 2000 mycar 09 0.9 If an object has two (or more) aliases, the object may be referenced and/or modified through any of those aliases. 21405 20 10

Invoking methods in a program Instead of calling a function to do something to an object (imperative programming), tell the object to perform one of its actions (objectbased programming). Examples: bobscar.accelerate(5); int currentspeed = bobscar.getspeed(); if (bobscar.getfuellevel() < 0.1) bobscar.decelerate(10); 21 Invoking methods in a program Valued methods return a single value, and should be used in an expression. Return type of method must be assignmentcompatible in the context of the call void methods do not return a value, and should be used as a single statement. Examples: bobscar.accelerate(5); int currentspeed = bobscar.getspeed(); if (bobscar.getfuellevel() < 0.1) bobscar.decelerate(10); 22 11

Arguments and Parameters Invocation of a method must have same number of arguments as formal parameters in the declaration of the method. Arguments are associated in order with formal parameters Argument types must be assignmentcompatible with the associated formal parameter types 23 Arguments and Parameters The formal parameter in the method becomes an alias for the argument passed to it, and is treated as a local variable. The formal parameter is discarded when the method terminates. Example: Suppose that a matchspeed method is defined as follows: public void matchspeed(automobile other) { int diff = getspeed() - other.getspeed(); if(diff > 0) other.accelerate(diff); else other.decelerate(-diff); 24 } 12

Before calling matchspeed bobscar Sedan suescar SUV 2000 2001 0.9 0.45 35 21405 9864 25 Call matchspeed bobscar.matchspeed(suescar); suescar is associated with other other is an alias of suescar. bobscar Sedan 2000 0.9 21405 suescar SUV 2001 other 0.45 35 9864 26 13

After executing other.accelerate(diff); suescar.speed is changed bobscar Sedan 2000 0.9 21405 suescar SUV 2001 other 0.45 9864 27 After matchspeed terminates other no longer exists bobscar Sedan 2000 0.9 21405 suescar SUV 2001 other 0.45 9864 28 14

Pass by value Passing Arguments For primitive type, parameter initialized to value of argument in call Pass by reference For a class type, formal parameter is initialized to the address of the object in the call An argument cannot be changed by being passed to a method! An object referenced by an argument can be changed by a method. 29 Alternate implementation of matchspeed * public void matchspeed(automobile other) { othercar = new Automobile(other.getModel(), other.getyear(), other.getfuellevel(), getspeed(), other.getmileage()); } * WRONG! 30 15

Before calling matchspeed * bobscar Sedan suescar SUV 2000 2001 0.9 0.45 35 21405 9864 31 Call matchspeed * bobscar.matchspeed(suescar); suescar is associated with other other is a parameter, an alias of suescar othercar is a local variable with the same instance values as other bobscar Sedan 2000 0.9 21405 suescar SUV 2001 other 0.45 35 9864 32 16

After executing the statement other = new Automobile( ); othe refers to a new memory location suescar SUV 2001 other 0.45 35 9864 bobscar Sedan othercar SUV 2000 2001 0.9 0.45 21405 9864 33 After matchspeed * terminates other no longer exists othercar and the object it referenced no longer exist suescar.speed is unchanged Memory allocated by new is garbage suescar SUV 2001 other 0.45 35 9864 bobscar Sedan 2000 0.9 21405 othercar SUV 2001 0.45 9864 34 17

Wrapper Classes 35 Wrapper Classes Sometimes, you need to treat primitive types like objects For example, you might want to turn the integer 42 into the string 42 To do this, you need to: Create a Wrapper class for the integer Call the tostring() method for that wrapper class 36 18

Wrapper Classes To treat primitive types as objects, you must use wrapper classes Primitive Class byte boolean char double float int long short Wrapper Class Byte Boolean Character Double Float Integer Long Short 37 Wrapper Class Example int size = 42; Integer sizewrapper = new Integer(size); String sizestring = sizewrapper.tostring(); size = 42 Integer value = 42 38 19

Wrapper Class Functions There are lots of other functions you can call on a wrapper class: doublevalue() returns the value as a double intvalue() returns the value as an integer To see the complete list, check out the Java 5.0 API 39 Auto-boxing In Java 5.0, conversion between primitive types and the corresponding wrapper classes is automatic This is called auto-boxing Integer i = 42; // auto-boxing // same as Integer i = new Integer(42) int x = i; // auto-unboxing // same as int x = i.intvalue(); 40 20

Fancy Auto-boxing You can even make auto-boxing work inside arithmetic expressions eg. Double e = d + 1; // d is a Double Let s look at the steps that this involves: 1. Auto-unbox d into a double 2. Add 1 3. Auto-box the result into a new Double 4. Store a reference to the newly created wrapper object in e 41 A Note About Wrappers Storing wrapper numbers is quite inefficient This is because you often only want to store the raw value but the wrapper class stores other stuff in addition to the value If you care about efficiency, use the primitive type where possible 42 21