CS 2340 Objects and Design - Scala

Similar documents
Functional Objects. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

Introduction to Programming Using Java (98-388)

Functions, Closures and Control Abstraction

G Programming Languages - Fall 2012

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Computational Expression

CSC Java Programming, Fall Java Data Types and Control Constructs

CS 1331 Introduction to Object Oriented Programming

Java Primer 1: Types, Classes and Operators

Control Structures. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

...something useful to do with the JVM.

Scala. Fernando Medeiros Tomás Paim

Bibliography. Analyse et Conception Formelle. Lesson 5. Crash Course on Scala. Scala in a nutshell. Outline

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 16: Intro to Scala. Announcements. Squeak demo. Instructor: Dan Barowy

Introduction to Object-Oriented Programming

Object-oriented programming

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

UMBC CMSC 331 Final Exam

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS260 Intro to Java & Android 03.Java Language Basics

CHAPTER 7 OBJECTS AND CLASSES

15CS45 : OBJECT ORIENTED CONCEPTS

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

Introduction to Object-Oriented Programming

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Introduction to Object-Oriented Programming

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

Parsing Scheme (+ (* 2 3) 1) * 1

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

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

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

Introduction to Object-Oriented Programming

Classes Classes 2 / 35

Using Scala in CS241

Java Object Oriented Design. CSC207 Fall 2014

JAVA OPERATORS GENERAL

Programs as Data. The Scala language

University of Tartu Faculty of Mathematics and Computer Science Institute of Computer Science

Introduction to Java

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

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

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

Kotlin, Start? Start! (pluu) Android Developer GDG Korea Android Organizer

Chapter 2: Using Data

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CHAPTER 7 OBJECTS AND CLASSES

CS 2340 Objects and Design

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Program Fundamentals

Chapter 4 Defining Classes I

Classes Classes 2 / 36

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

CS 1302 Chapter 9 (Review) Object & Classes

Index COPYRIGHTED MATERIAL

13 th Windsor Regional Secondary School Computer Programming Competition

SECTION II: LANGUAGE BASICS

CS 11 java track: lecture 1

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

INTRODUCTION. SHORT INTRODUCTION TO SCALA INGEGNERIA DEL SOFTWARE Università degli Studi di Padova

CSC 1214: Object-Oriented Programming

Introduction to Object-Oriented Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS205: Scalable Software Systems

The SCAlable LAnguage

SCALA ARRAYS. Following picture represents array mylist. Here, mylist holds ten double values and the indices are from 0 to 9.

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

PIC 20A Number, Autoboxing, and Unboxing

CS558 Programming Languages

Produced by. Agile Software Development. Eamonn de Leastar

Computer Programming, I. Laboratory Manual. Final Exam Solution

Operators and Expressions

Key Java Simple Data Types

TypeScript. Types. CS144: Web Applications

Practically Functional. Daniel Spiewak

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

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

n n Official Scala website n Scala API n

Java Fundamentals (II)

Programming in Scala Second Edition

Produced by. Agile Software Development. Eamonn de Leastar

PROGRAMMING FUNDAMENTALS

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

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Advanced Object-Oriented Languages Scala

JAVA MOCK TEST JAVA MOCK TEST II

Clean Boundaries. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 10

CIS 110: Introduction to Computer Programming

Scala Style Guide Spring 2018

Lab5. Wooseok Kim

Object-Oriented Programming Concepts

Xtend Programming Language

Clean Formatting. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 1

Transcription:

CS 2340 Objects and Design - Scala Objects and Operators Christopher Simpkins chris.simpkins@gatech.edu Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 1 / 13

Classes A class is a blueprint for creating (or instantiating) objects An object is an instance of a class, created using new In Scala, every entity you can refer to is an object (or type - more later) Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 2 / 13

Person Example class Person { val firstname: String = John val lastname: String = Doe var sex: String = "male" override def tostring = { firstname + " " + lastname Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 3 / 13

Instance Variables Represent the state of the object Public by default, can specify private or protected - more later val instance variables are immutable var instance variables are mutable Must be initialized Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 4 / 13

Instance Variables Examples vars can be reassigned: val mrgarrison = new Person mrgarrison.sex = male mrgarrison.sex = female vals cannot be reassigned: val jackdoe = new Person jackdoe.firstname = Jack // won t compile - reassignment to val Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 5 / 13

Primary Constructor class Person(val firstname: String, val lastname: String, var sex: String = "male") { override def tostring = { firstname + " " + lastname Now persons can be created like this: val mrgarrison = new Person("Mr.", "Garrison") val grace = new Person("Grace", "Hopper", "female") Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 6 / 13

Methods Definition starts with def keyword Every method has a type Some methods don t have a type. Their type is Unit return statement is optional and discouraged - last value evaluated in a method is return value Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 7 / 13

Method Example class Foo { def ncopies(s: String, n: Int): String = { s*n def printncopies(s: String, n: Int) { // Notice no = print(ncopies(s, n)) Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 8 / 13

Applications An object (singleton) with a main method is runnable object App { def main(args: Array[String]) { println( Arguments are: ) for (arg <- args) { println(arg) Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 9 / 13

Basic Types and Operations Integral types: Byte, Short, Int, Long, and Char Numeric types: integral types plus Float and Double Scala s String uses java.lang.string directly (for now) Numeric types are value types, which means that they are implemented directly as Java primitives in bytecode but are decorated by rich wrappers in Scala which allow you to call methods on basic types. Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 10 / 13

Operators are Methods scala> 1 + 2 res2: Int = 3 scala> 1.+(2) res3: Double = 3.0 scala> (1).+(2) res4: Int = 3 Unary operators can be defined for +, -,!, and. The method name is, e.g., unary_! for unary!. scala>!true res3: Boolean = false scala> true.unary_! res4: Boolean = false Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 11 / 13

Basic Operations Precedence, bitwise operators like Java - read on your own Scala twist: any method that ends in a : character is invoked on its right operand, passing in the left operand. Example: :: (pronounced cons ) is right-associative. It is called on the List that is its right operand. scala> val ys = List(2, 3) ys: List[Int] = List(2, 3) scala> val zs = 1::ys zs: List[Int] = List(1, 2, 3) Note that operands are always evaluated left to right: scala> val as = List(4, 5) as: List[Int] = List(4, 5) scala> 1+2::as res7: List[Int] = List(3, 4, 5) Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 12 / 13

Object Equality == and equals test value equality, eq tests reference equality scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> val ys = xs ys: List[Int] = List(1, 2, 3) scala> val zs = List(1, 2, 3) zs: List[Int] = List(1, 2, 3) scala> xs == ys res8: Boolean = true scala> xs == zs res9: Boolean = true scala> xs eq ys res10: Boolean = true scala> xs eq zs res11: Boolean = false Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 13 / 13