COE318 Lecture Notes Week 3 (Sept 19, 2011)

Similar documents
COE318 Lecture Notes Week 4 (Sept 26, 2011)

COE318 Lecture Notes Week 3 (Week of Sept 15, 2014)

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

COE318 Lecture Notes Week 5 (Oct 3, 2011)

COE318 Lecture Notes Week 6 (Oct 10, 2011)

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

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

3.1 Class Declaration

Introduction to Programming Using Java (98-388)

Updates. Office B116E Office Hours : Friday 1 p.m. 2 p.m. Also by appointment Office Hours Purpose:

COE318 Lecture Notes Week 8 (Oct 24, 2011)

7. C++ Class and Object

VARIABLES AND TYPES CITS1001

Programming II (CS300)

Java Classes & Primitive Types

Java Classes & Primitive Types

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

JAVA GUI PROGRAMMING REVISION TOUR III

Chapter 6 Introduction to Defining Classes

CE221 Programming in C++ Part 1 Introduction

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

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

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

C10: Garbage Collection and Constructors

Java Identifiers, Data Types & Variables

Programming for Mobile Computing

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Programming Language Concepts: Lecture 2

Programming II (CS300)

CHAPTER 7 OBJECTS AND CLASSES

Programming II (CS300)

Programming Languages and Techniques (CIS120)

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

Computer Science II Data Structures

Object-Oriented Programming Concepts

Chapter 4 Java Language Fundamentals

Fall 2017 CISC124 9/16/2017

Programming Languages and Techniques (CIS120)

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Chapter 6 Classes and Objects

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

Lecture 3. Lecture

Scala. Introduction. Scala

Assumptions. History

C11: Garbage Collection and Constructors

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

2. The object-oriented paradigm!

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

ECE 122. Engineering Problem Solving with Java

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Domain-Driven Design Activity

Programming Languages and Techniques (CIS120)

JAVA MOCK TEST JAVA MOCK TEST II

Programming Languages and Techniques (CIS120)

Computational Expression

coe318 Lab 1 Introduction to Netbeans and Java

Compiling Techniques

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Java Application Development

CHAPTER 7 OBJECTS AND CLASSES

BBM 102 Introduction to Programming II Spring 2017

C++ (classes) Hwansoo Han

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

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Objects and Classes: Working with the State and Behavior of Objects

Chapter 5 Names, Bindings, Type Checking, and Scopes

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

EECS1710. Announcements. Labtests have been returned Term Test #01 marking is in progress. Next Labtest: Thu Oct 23/Fri Oct 24

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Programming Languages and Techniques (CIS120)

Short Notes of CS201

THE CONCEPT OF OBJECT

Lecture 06: Classes and Objects

PROGRAMMING FUNDAMENTALS

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

CSC207 Week 2. Larry Zhang

TaxiBot New attributes Variables Math! TaxiBot

Programming Languages and Techniques (CIS120)

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS 520 Theory and Practice of Software Engineering Fall 2017

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

An Introduction to C++

CS201 - Introduction to Programming Glossary By

Recommended Group Brainstorm (NO computers during this time)

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

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

CS360 Lecture 5 Object-Oriented Concepts

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

coe318 Lab 2 ComplexNumber objects

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Transcription:

COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Sept 19, 2011) Topics Announcements TurningRobot example PersonAge example Announcements The submit command now works. Quiz: Monday October 3, 2011 Generating javadocs from netbeans is broken (on all platforms). I have reported this problem to netbeans. (Generating javadocs does work if you have a package which we have not yet examined in this course.) To submit from home install the secure shell software on your machine as described in the Department's Network Users Guide. Then: Create a zip file (important note to Windows users: ensure that it is compressed to a.zip file not an.rar file!) Copy the zip file to the Departmental network. Open a remote terminal to the Departmental machine and run the submit command. (You must run this command in the same folder that you copied your zip file to.) Review What is a class/object? The class file source code provides the template or blueprint for creating and using objects of the class type and consists of: instance variables: These correspond to the characteristics of the object. They are declared giving each a name and data type. They are usually private. Instance variables can be used in

COE318 Lecture Notes: Week 3 2 of 8 any method or constructor code. Constructors: They are used to perform any initialization needed when creating an object. Although they look like methods, they are different in that they have no return type and must have the same name as the class. Methods: These provide behavior to objects. They must hava a return type (void if nothing is returned) If they are part of the class design, they must be public. Helper methods or others that are not part of the public design should be private. Other details The? Operator Format: <boolean-expression>? <A> : <B> where A and B are expressions of the same type. The value of the expression is A if the boolean-expression is true and B if it is false. Example:.class and.java files int i, j, max; max = i > j? i : j; The jave source code language for a class must have the same name as the class and the extension.java. For example, the source code for the Robot class is in a file called Robot.java. (Note that case is important! Class names should begin with an uppercase letter.) When the source code is compiled, the compilation result is placed in a file with the same name as the class but with the extension.class instead of.java. Example: the TurningRobot class In this example, the robot responds tot the commands left, right, up and down Ii.e. It has methods left() etc. As before. However, a TurningRobot can only move forwards; it cannot move sideways or backwards. But it can turn 90 counter clockwise. For example, if a TurningRobot is moving right and is then asked to move left it has to rotate

COE318 Lecture Notes: Week 3 3 of 8 180 to make its forward direction left. Like a Robot object, a TurningRobot has to know its position (the x and y instance variables). In addition, it has to know its current direction. New Java features used: the switch statement local variables (dir in printdirection) helper private methods public class TurningRobot { //Instance variables private int direction = 0; private int x = 0; private int y = 0; public void right() { setdirection(0); x++; step(); public void up() { setdirection(1); y++; step(); public void left() { setdirection(2); x--; step(); public void down() { setdirection(3); y--; step(); private void setdirection(int desired) { while (desired!= direction) { turn();

COE318 Lecture Notes: Week 3 4 of 8 private void turn() { direction++; if (direction == 4) { direction = 0; printdirection(); dir); ", " + private void printdirection() { String dir; switch (direction) { case 0: dir = "right"; break; case 1: dir = "up"; break; case 2: dir = "left"; break; case 3: dir = "down"; break; default: dir = "???"; System.out.println("Turned to move " + private void step() { System.out.println("Stepping to (" + x + y + ")"); Example: the AgePerson class

COE318 Lecture Notes: Week 3 5 of 8 This example shows how a AgePerson object can be immutable. (Age changes with time, of course, but age does not have to be an instance variable; if it were, the object would be mutable.) The trick is to use Date of Birth as an instance variable. The getage() method then calculates the age at the present time. Note: You are not responsible for knowing about the Calendar or GregorianCalendar classes. import java.util.calendar; import java.util.gregoriancalendar; public class PersonAge { private Calendar dob; private String name; public PersonAge(String n, Calendar d) { name = n; dob = d; public String getname() { return name; public int getage() { Calendar today = new GregorianCalendar(); int yearnow = today.get(calendar.year); int birthyear = dob.get(calendar.year); int age = yearnow - birthyear; if (today.get(calendar.day_of_year) < dob.get(calendar.day_of_year)) { age--; return age; Data types There are 2 general data types:

COE318 Lecture Notes: Week 3 6 of 8 1. primitive, and 2. reference Primitive data types are int, float, double, short, long, byte, boolean and char. When a primitive data type is declared, the memory required to hold the data is allocated and that memory contains the value of the data. For example, the declaration byte i; allocates one byte of memory for the variable i. The assignment statement i = 5; would set that memory location's value to 5. Every class (and there are thousands built into the java library) is also a data type. But it is a reference data type. The reference points to an area in memory where the actual object of the type resides. The most common built-in reference data type is String. When a reference data type is declared, memory is set aside to hold the reference. This is always 4 bytes no matter how big the actual data object is. The reference is instantiated by assigning it to an object (which may need to be created) of the correct data type. For example: Robot robbie; robbie = new Robot(); An actual Robot object is created with new Robot() (which invokes the Robot class's constructor) and it resides somewhere in memory (and uses many more than 4 bytes!). The reference value robbie points to the starting address of the actual Robot object. The place in memory where objects exist is called the heap. The Java Virtual Machine (JVM) keeps track of how many references there are to objects in the heap. When the reference count becomes zero, it is no longer possible to access the object. It has become garbage. The JVM periodically runs a Garbage Collector which frees up the space occupied by garbage objects. Where variable names are used Variable names can be used for instance variables, parameter names and local variables. Instance variables are declared in a class body. They can be accessed by any method of the class. They are usually public. Parameter names are the arbitrary names given to the passed parameters of methods or constructors. Parameters only exist during the execution of the method or constructor and cannot be seen by other methods. (If 2 methods have the same name for a parameter, no interference results.)

COE318 Lecture Notes: Week 3 7 of 8 If a parameter name and an instance variable name are the same, the name all by itself refers to the paramter. To access the instance variable of the same name, use this.name. Local variables are declared and used inside methods. They only exist during the execution of the method. To be even more specific, on entry to a method, space for all the local variables is allocated on the stack. Questions 1. Given the two classes, A and B, shown below identify identify each as mutable or immutable. Explain. public class A { private final int p; public int q; public A(int p, int q) { this.p = p; this.q = q; public int getp() { return p; public int getq() { return q; public class B { private final int x; private int y; public B(int x, int y) { this.x = x; this.y = y;

COE318 Lecture Notes: Week 3 8 of 8 /** * @return the x */ public int getx() { return x; /** * @return the y */ public int gety() { return y;