CSC207 Week 2. Larry Zhang

Similar documents
CSC207 Week 3. Larry Zhang

Introduction to Programming Using Java (98-388)

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Object-Oriented Programming Concepts

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

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

CS 231 Data Structures and Algorithms, Fall 2016

Defining Classes and Methods

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

CS121/IS223. Object Reference Variables. Dr Olly Gotel

VARIABLES AND TYPES CITS1001

type conversion polymorphism (intro only) Class class

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

Java Object Oriented Design. CSC207 Fall 2014

Computational Expression

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

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.

1 Shyam sir JAVA Notes

BM214E Object Oriented Programming Lecture 4

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Java Primer 1: Types, Classes and Operators

CSC Java Programming, Fall Java Data Types and Control Constructs

CSE 431S Type Checking. Washington University Spring 2013

Programming II (CS300)

Defining Classes and Methods

Java Fundamentals (II)

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

Synchronization SPL/2010 SPL/20 1

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

CS1004: Intro to CS in Java, Spring 2005

Java Identifiers, Data Types & Variables

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Assumptions. History

Fall 2017 CISC124 9/16/2017

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

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

Chapter 4 Defining Classes I

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

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

Chapter 6 Introduction to Defining Classes

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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

5. Defining Classes and Methods

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

5. Defining Classes and Methods

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

Java Fall 2018 Margaret Reid-Miller

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

EECS168 Exam 3 Review

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

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

CPS122 Lecture: From Python to Java

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

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

JAVA: A Primer. By: Amrita Rajagopal

Datatypes, Variables, and Operations

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

About this exam review

Chapter 2. Elementary Programming

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

ECE 122 Engineering Problem Solving with Java

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

Lecture #23: Conversion and Type Inference

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

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

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

Object Orientated Analysis and Design. Benjamin Kenwright

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Object-oriented programming in...

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

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

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Topic 9: Type Checking

Topic 9: Type Checking

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

CSI33 Data Structures

CST141 Thinking in Objects Page 1

Programming Languages and Techniques (CIS120)

CS11 Java. Fall Lecture 1

1B1b Classes in Java Part I

Chapter 6 Classes and Objects

CS 170 Java Programming 1. Week 12: Creating Your Own Types

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

Preview from Notesale.co.uk Page 3 of 36

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Array. Prepared By - Rifat Shahriyar

Object Oriented Programming

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

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Transcription:

CSC207 Week 2 Larry Zhang 1

Today s Outline Finish up non-oo examples, Array, HashMap OO programming Unit Test: JUnit (if time permits) Javadoc (if time permits) 2

Recap Java code is compiled into bytecode which runs in JVM. Java uses static typing (main difference from Python) All variables must be declared with a type before being used. The type of the variable CANNOT be changed after being declared. Primitive types: byte, short, int, long, float, double char boolean Class types (all other types, provided by Java or user defined) e.g., String, Integer, Double, Balloon, Person... 3

Auto-Conversion Rules can-auto-convert directions: byte short int long float double char int and above boolean no other types Any other conversion that do not following the can-auto-convert directions must be explicitly casted. e.g., float x = (float)2.07; long x = 207; short y = (short)x; short x = 207; long y = x; // auto-converted, no need for casting 4

Understanding the Auto-Conversion Rules NEVER just memorize the rules! Understand the reasoning of behind the rules! Reasoning #1 (technical): Converting from shorter to longer types just involves adding 0 s to the original number, without loss of information e.g., converting byte x = 7 to a short int is adding eight 0 s at the higher bits to the original number: 0000 0111 (byte) 0000 0000 0000 0111 (short) Converting from longer to shorter types requires truncating the original number, may lose information e.g., convert short x = 259 to a byte is removing the highest eight 0 s from the original number: 0000 0001 0000 0011 (short value 259) 0000 0011 (byte value 3) 5

Understanding the Auto-Conversion Rules Reasoning #2 (conceptual and more general, not just for primitive types): If Type A is-a special case (subtype) of Type B, then it is okay to convert from A to B. Example: Cat is a special case of Animal, so it is fine to convert an instance of Cat into type Animal. This is a cat implies this is an animal Conversely, converting an instance of Animal into type Cat might not be fine. This is an animal does not imply this is a cat. 6

Sidenote: IS-A, HAS-A, RESPONDS-TO In Object-Oriented Design (OOD), we often use the above three phrases to describe the relationships between different classes. IS-A: the subtype relationship Example: A car is a vehicle. HAS-A: the composition relationship Example: A car has a wheel. RESPONDS-TO: communication (message passing) between objects. Example: A person P calls a car C s C.openDoor() method, C responds to P. These phrases are part of the Universal Modelling Language (UML). 7

DEMO Non-OO Code Examples Cont. ControlsFunctions.java (if statements, loops, functions, Array, HashMap) 8

Object Oriented Programming 9

Object Oriented Programming in Java We have done OO programming in Python, so you know what it s all about. So let s focus on the differences between Python and Java. Overall, Java is a more rigorous language than Python in terms of expressing the structure of OO design. 10

Constructor In Python, it is a method named init In Java, it is a method with the same name as the class. has no return type, not even void. can have multiple constructors with different type signatures. the one with the matching signature will be called this is like Python s self 11

Access Modifiers In Java, each variable and method of a class has a clearly declared access modifier which defines from where you can access this variable/method. This is access control is enforced by the compiler (violation will cause compiler error). Python doesn t have this. A package is a namespace which contains several classes which are supposed to be in the same package, e.g., java.lang.math, java.utils, or user-defined csc207 Read more: http://docs.oracle.com/javase/tutorial/java/concepts/p ackage.html 12

Encapsulation How do we decide the access modifiers of the variables and methods? Lazy idea: Let everything be public. What could go wrong? Better Idea: Only make public the methods that provide the interface to the users of the class. class TimHortons: variables: coffeemachine beans oven bread lights methods: orderfood() ordercoffee() paymoney() grindbeans() toastmuffin() addmilk() addsugar() getfood() turnofflights() 13

keyword: static With static, it is a function, which is called by name only; it is independent of any object. Without static, it is a method, which is called by a name that is associated to an object. public class Person { } // function public static int plusone(int x) { return x + 1; } public class Person { } private int age; // method public void becomeolder() { this.age += 1; } 14

keyword static used on variables It is a variable which belongs to the class and not to object (instance) Static variables are initialized only once, at the start of the execution. A single copy to be shared by all instances of the class public class Person { public static int npeople = 0; // doesn t have to be public // Constructor public Person() { npeople++; // keeps track of # of Person s constructed System.out.println(nPeople + objects created ); } } // outside the class, static variables can be accessed as follows System.out.println(Person.nPeople); 15

DEMO Translate Balloon.py to Balloon.java UseBalloon.java 16

Java is pass-by-value For primitive-typed variables, the VALUE of the variable (not the reference of the variable) is passed to a function. For class-typed variables, the VALUE of the reference to the is passed to function. 17

Example 1 18

Example 2 19

Reading: More on Pass-by-Value http://javadude.com/articles/passbyvalue.htm 20

UML Unified Modeling Language (UML) allows us to express the design of a program before writing any code. It is language-independent. An extremely expressive language. We ll use only a small part of the language, Class Diagrams, to represent basic OO design. 21

22

23

References Java OO: http://docs.oracle.com/javase/tutorial/java/javaoo/index.html Access Modifiers: http://docs.oracle.com/javase/tutorial/java/javaoo/accesscontrol.html JUnit http://junit.org/ Javadoc http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html Kahoot quiz can be found by search CSC207 on Kahoot.com 24