Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

Similar documents
COE318 Lecture Notes Week 3 (Sept 19, 2011)

COE318 Lecture Notes Week 4 (Sept 26, 2011)

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

CS 177 Week 15 Recitation Slides. Review

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Lecture 11: Intro to Classes

Computer Science II Data Structures

COE318 Lecture Notes Week 5 (Oct 3, 2011)

MIDTERM REVIEW. midterminformation.htm

Classes and Objects. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

Building Java Programs

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

Instance Members and Static Members

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

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

Introduction to Programming Using Java (98-388)

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

CS170 Introduction to Computer Science Midterm 2

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

JAVA MOCK TEST JAVA MOCK TEST II

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Chapter 6 Classes and Objects

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

3.1 Class Declaration

CIS 110: Introduction to Computer Programming

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

2. The object-oriented paradigm!

Lecture 06: Classes and Objects

Spring 2018 Mentoring 3: February 7, Switcheroo. The Golden Rule of Equals says:

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Java Identifiers, Data Types & Variables

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Data Structures and Programming with C++

Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock)

JUnit Summary. Unit Test 3/18/2009. Test methods Testing exceptions Common known states

JUnit Summary. Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

! Theoretical examples: ! Examples from Java: ! data type: A category of data values. " Example: integer, real number, string

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

VARIABLES AND TYPES CITS1001

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Building Java Programs

W09 CPSC 219 Midterm Exam Page 1 of 7

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

FAQ: Classes & Objects

Object-Oriented Programming

Computer Programming

CS349/SE382 A1 C Programming Tutorial

index.pdf January 21,

CS 251 Intermediate Programming Methods and Classes

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

CS 251 Intermediate Programming Methods and More

Exam 1 - (20 points)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Object-Oriented Principles and Practice / C++

ITI Introduction to Computing II

CS 520 Theory and Practice of Software Engineering Fall 2017

2. The object-oriented paradigm

ITI Introduction to Computing II

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

ITI Introduction to Computing II

CS 520 Theory and Practice of Software Engineering Fall 2017

ITEC1620 Object-Based Programming. Lecture 13

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

Midterm #2 Sample Questions Solutions

H212 Introduction to Software Systems Honors

Object- Oriented Analysis, Design and Programming

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

COE318 Lecture Notes Week 6 (Oct 10, 2011)

CS18000: Programming I

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

ITI Introduction to Computing II

An introduction to Java II

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

Java. Representing Data. Representing data. Primitive data types

CMPT 125: Lecture 3 Data and Expressions

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Class. A class is an unit of Java programs; that is, Java programs consist only of classes. School. Department. Student. Course.

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

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

Lecture 3: C Programm

6.092: Java for 6.170

Chapter-8 DATA TYPES. Introduction. Variable:

Intro to C: Pointers and Arrays

CHAPTER 7 OBJECTS AND CLASSES

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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

Conversions and Overloading : Overloading

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Transcription:

G51PRG: Introduction to Programming Second semester Lecture 2 Remedial classes Monday 3-5 in A32 Contact Yan Su (yxs) Ordinary labs start on Tuesday Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Lecture 2: Objects 2 Plan of the lecture Classes and s Constructors Creating s Comparing s Copying s Classes and Objects Classes are blueprints for s They contain information on which properties s have (fields) what they can do (methods) Lecture 2: Objects 3 Lecture 2: Objects 4 : Point class class Point { int x,y; public Point(int x, int y){ this.x = x; this.y = y; public double distance(point p){ return Math.sqrt(Math.pow((p.x-x),2) + Math.pow((p.y-y),2)); // distance = ((p.x-x) 2 + (p.y-y) 2 ) Lecture 2: Objects 5 : Person class class Person { String name; int age; Person(String name, int age){ this.name = name; this.age = age; void display() { System.out.println(name + ", " + age); Lecture 2: Objects 6 1

Constructors Usually a class has at least one method which is used to create instances of a class (s). It is called a constructor. Constructor has the same name as the class. A class can have several constructors with different sets of parameters. Usually they set all or some of the fields in the class to values passed as parameters. : two constructors class Point { int x,y; public Point(int x, int y){ this.x = x; this.y = y; public Point () { this.x = this.y = 0; Lecture 2: Objects 7 Lecture 2: Objects 8 : default constructor public Person(){ (would set default values null for String name and 0 for int age). Creating s Point p1; Declares a variable of type Point. Does not allocate memory. Empty erence (ers to null ). Point p1 = new Point(4,5); Allocates memory (with new ), executes the constructor. Now p1 is a erence which points to the new. Lecture 2: Objects 9 Lecture 2: Objects 10 Before we go further quiz: Point p1; System.out.println(p1.x); // what will happen if you compile this? Point p1 = null; System.out.println(p1.x); // what will happen if you compile this? // what wil happen when you execute this? What happens when s are constructed Need to understand the difference between s and basic types (boolean, char, byte, short, int, long, double, float). Lecture 2: Objects 11 Lecture 2: Objects 12 2

Basic types Basic type variables contain their value: int x = 4 x 4 Reference types Person, Point, String etc. are erence type variables. The value of a erence type variable is a erence to (an address of) the value or set of values represented by the variable. Lecture 2: Objects 13 Lecture 2: Objects 14 Creating s p1 Point Person person1 = new Person("Bill", 51); x 4 Note that Strings are also s (erence types). y 5 Lecture 2: Objects 15 Lecture 2: Objects 16 Person Why does this matter? person1 Person name String Bill Reference type and basic type variables behave differently with respect to assignment and comparison. If you don t know that you will often be surprised at what your programs do. age 51 Lecture 2: Objects 17 Lecture 2: Objects 18 3

Comparing s The following comparison: person1 == person2 returns true if the erences are to the same storage locations, and false otherwise. For example, Person person1 = new Person("Bill", 51); Person person2 = new Person("Bill", 51); System.out.println(person1 == person2); would print false, although the values of all fields in person1 and person2 are the same. Equality Suppose we want to find out whether 1 and 2 are equal (have the same values for all fields). Then we need to write a special method equals which compares s of a given class. For example, boolean equals (Person p) { return (this.name.equals(p.name) && this.age == p.age); Recall that String class has equals() method. Lecture 2: Objects 19 Lecture 2: Objects 20 Assignment person2 = person1; results in person2 erring to the same locations as person1. If we change the values of person1's fields, the same changes will happen to person2 (because they er to the same locations in memory). person1 person2 Lecture 2: Objects 21 int[] array1 = {1,2,3,4; int[] array2 = array1; array1[0] = 0; // instead of 1 System.out.println(array2[0]); // will print 0 although we only // changed the other array! This is because arrays are erence types. Lecture 2: Objects 22 int[] array1 = {1,2,3,4; array1[0] = 0; // instead of 1 array1 1 2 3 4 array1 0 2 3 4 int[] array2 = array1; array2 array1 1 2 3 4 array2 Lecture 2: Objects 23 Lecture 2: Objects 24 4

Making a separate copy Later in the course we will study cloning of s. You can write a copy method yourself, for example: public Person copy() { String newname = new String(this.name); String newage = this.age; return new Person(newName, newage); To sum up If x and y are variables of basic type, x = y means: "put the same value as y holds, in x's location" x == y means: "do x and y hold the same value?" If x and y are variables of / erence type x = y means: "get y to er to the same location in memory as x" x == y means: "do x and y have the same address?" Lecture 2: Objects 25 Lecture 2: Objects 26 Summary and further reading In this lecture, we recalled how to create s and how s are stored. It is important to understand that s are stored as erences, and how this affects comparison and assignment of s. The reason s are passed by erence, by the way, is that they can be very large and it is much more efficient to manipulate erences than to copy around huge amounts of data. Reading: Java Gently Chapter 8 and Sun Java tutorial http://java.sun.com/docs/books/tutorial/java/data/s.html Lecture 2: Objects 27 5