OOP, null, Graphics. Brooks Townsend

Similar documents
Default Parameters and Shapes. Lecture 18

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

ITI Introduction to Computing II

ITI Introduction to Computing II

Topic 7: Algebraic Data Types

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

CS 251 Intermediate Programming Inheritance

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Max iteration: Min iteration: Max iteration: Min iteration: Max iteration: Min iteration:

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Chapter 14 Abstract Classes and Interfaces

Chapter 6: Inheritance

CSE 143 Lecture 20. Circle

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Making New instances of Classes

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

CS/ENGRD 2110 SPRING 2018

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Binghamton University. CS-140 Fall Chapter 9. Inheritance

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

Object-Oriented Programming Concepts

Lecture 2: Java & Javadoc

The Prototype Framework Part III: Better OOP

Advanced Placement Computer Science. Inheritance and Polymorphism

CLASSES AND OBJECTS. Fundamentals of Computer Science I

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Java Comparable interface

And Even More and More C++ Fundamentals of Computer Science

COMP 250. inheritance (cont.) interfaces abstract classes

Mouse / Keyboard Events & Anonymous Functions. Lecture 19

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

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

CMSC 132: Object-Oriented Programming II

CS-202 Introduction to Object Oriented Programming

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Module 01 Processing Recap

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CS1150 Principles of Computer Science Objects and Classes

JavaScript: Sort of a Big Deal,

Object Oriented Programming 2015/16. Final Exam June 28, 2016

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Inheritance and Polymorphism

Checklist (for Today) What we are reinforcing with the exercises this class. recognizing the various different arithmetic operators (in situ)

Ruby: Introduction, Basics

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

JAVA: A Primer. By: Amrita Rajagopal

Timing for Interfaces and Abstract Classes

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Object Oriented Programming

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Chapter 6 Introduction to Defining Classes

Module 01 Processing Recap. CS 106 Winter 2018

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Reviewing OO Concepts

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Use the scantron sheet to enter the answer to questions (pages 1-6)

Inheritance (Deitel chapter 9)

Syntax of Eiffel: a Brief Overview

Solutions to Quiz 1 (March 14, 2016)

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

Reusing Classes. Hendrik Speleers

JAVA GUI PROGRAMMING REVISION TOUR III

Reviewing OO Concepts

Java interface Lecture 15

PLT Fall Shoo. Language Reference Manual

Chapter 3 Data Types and Variables

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Inheritance, and Polymorphism.

JavaScript: Objects, Methods, Prototypes

Implementing non-static features

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Program Fundamentals

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

CISC 1600 Lecture 3.1 Introduction to Processing

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Java Primer. CITS2200 Data Structures and Algorithms. Topic 0

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Computer Science 210: Data Structures

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

3.1 Class Declaration

Compilers CS S-10 Object Oriented Extensions

Typescript on LLVM Language Reference Manual

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Lecture 14: more class, C++ streams

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Common Misunderstandings from Exam 1 Material

Transcription:

OOP, null, Graphics Brooks Townsend

Value types vs Reference Types Value Types Simple data types, ex: string, number, boolean Reference Types Complex data types, ex: arrays, classes/objects

Value types vs Reference Types let x: number = 15; let y: number = x; let d: string = is a number ; x = 30; print(x + d); print(y + d); class Foo { x: number = 30; let a: Foo = new Foo(); let b: Foo = a; b.x = 50; print(a.x); print(b.x);

Classes and Objects Classes and Objects A class defines a new Data Type (with specific properties) An object is an instance of a class Every object of a class has the same properties, but different values for those properties

null A value that is not a value Think back to linked lists class Node { data: string; next: Node null;

null Other programming languages have null pointers PS04 Linked List checking for null let lastnode: Node = head; while (lastnode.next!== null) { lastnode = lastnode.next; return lastnode;

Object Oriented Programming So far, all objects have been are collections of properties What really makes objects powerful are the methods (and constructor) of an object

Methods A method is a function defined in a class Everything is the same as a function, except you no longer need the function keyword. Ex: <methodname>(<param>: <ptype>): <return type> { //Code goes here

This Methods use the this keyword to refer to the object that it was called on Class Declaration class ReviewSession { reviewer: string; whoisteaching(): string { return this.reviewer; Main method function main(): void { let rs: ReviewSession; rs = new ReviewSession(); rs.reviewer = Brooks ; let rs2: ReviewSession; rs2 = new ReviewSession(); rs2.reviewer = Jeffrey ; print(rs.whoisteaching()); print(rs2.whoisteaching());

Constructors Special type of method that is called when an object is initialized. Constructors allow us to instantiate an object with initial values of its properties. constructor(<parameters>) { // Initialize variables / class setup here

class Something { p1: string; p2: string; constructor(p1: string, p2: string) { this.p1 = p1; this.p2 = p2; tostring(): string { return this.p1 + + this.p2; let s: Something = new Something( Go, Heels! ); print(s.tostring());

tostring() Notice on the last slide, we called tostring() on an object However, if a class implements a tostring() method it is automatically called if you attempt to print an instance of that class Implicitly called, print(object.tostring()) === print(object)

class Something { p1: string; p2: string; constructor(p1: string, p2: string) { this.p1 = p1; this.p2 = p2; tostring(): string { return this.p1 + this.p1 + this.p1; let s: Something = new Something( Go, Heels! ); print(s);

Assignment operators Some shorthand syntax that is interesting x = x + 1; x ++; x = x - 1; x ; x = x + somenumber; x += somenumber; x = x * somenumber; x *= somenumber; x = x / somenumber; x /= somenumber;

Modulo When you want to get the remainder of integer division 0 % 2 == 0 1 % 2 == 1 2 % 2 == 0 3 % 2 == 1 17 % 5 == 2 22 % 21 == 1 3 % 5 == 3

Optional Parameters Parameters that do not have to be specified in the creation of an object class Circle { radius: number = 0; color: string = black ; constructor(r: number, c?: string){ this.radius = r; if (c!== undefined) { this.color = c; let c: Circle; let d: Circle; c = new Circle(15); d = new Circle(20, blue ); print(c.color); print(d.color);

Shapes let svgtag: SVG = new SVG("artboard"); let group: Group = new Group(); let axes: Axes = new Axes(50, 50); group.add(axes.getshapes()); let rectangle: Rectangle = new Rectangle(50, 50, -25, -25); group.add(rectangle); svgtag.render(group);

Object composition But what is an axes? Why axes.getshapes();? export class Axes { width: number; height: number; constructor(width: number, height: number) { this.width = width; this.height = height; getshapes(): Group { let group: Group = new Group(); let x: Rectangle = new Rectangle(100, 0.1, -50, 0); let y: Rectangle = new Rectangle(0.1, 100, 0, -50); group.add(x); group.add(y); return group;

Chaining Can happen with methods, or with properties group.children[1].transform = group.children[1].transform.rotate(45); We chain into the group object here, through 4 properties / methods

Another chaining example class Foo { bar: Bar; class Bar { baz: Baz; coolmethod(): Baz { print( cool ); return this.baz; class Baz { s: string; let foo: Foo = new Foo(); let bar: Bar = new Bar(); let baz: Baz = new Baz(); baz.s = Will this work? bar.baz = baz; foo.bar = bar; print(foo.bar.coolmethod()); // Prints // cool // Will this work? Yes!" tostring(): string { return this.s + Yes! ;