10-Sep-15 Classes. Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects”

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Fancy Parameters. Class parameters When you define a class, you typically give it parameters scala> class Point(val x: Double, val y: Double) defined.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Access to Names Namespaces, Scopes, Access privileges.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Programming Languages Translation  Lecture Objectives:  Be able to list and explain five features of the Java programming language.  Be able to explain.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Writing Scala Programs. Command Line There are three common operating systems: Windows (various flavors; I recommend Windows 7) UNIX or Linux (basically.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Functions and Methods. Definitions and types A function is a piece of code that takes arguments and returns a result A pure function is a function whose.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
OOP Basics Classes & Methods (c) IDMS/SQL News
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Sequences and for loops. Simple for loops A for loop is used to do something with every element of a sequence scala> for (i
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
The need for Programming Languages
Instant Scala.
Content Programming Overview The JVM A brief look at Structure
Overview of c# Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
CompSci 230 Software Construction
Programming Language Concepts (CIS 635)
Namespaces, Scopes, Access privileges
Introduction to javadoc
CSE341: Programming Languages Section 1
An Introduction to Java – Part I, language basics
CSE341: Programming Languages Section 1
Namespaces, Scopes, Access privileges
Tonga Institute of Higher Education
Introduction to javadoc
F II 2. Simple Java Programs Objectives
Introduction to Python
Presentation transcript:

10-Sep-15 Classes

Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects” Functional means that functions, like numbers, are values We’ll talk more about the functional aspects later Every value, such as 27.3 or "Hello ", is an object You can “talk” to objects, using dot notation: scala> "Hello".toLowerCase res12: String = hello scala> "Hello".startsWith("He") res13: Boolean = true Every program you write starts with an object you define A class (or type) is a description of a set of objects The type of "Hello" is String Most of your programs will use several classes and objects

Objects If you want just one object of a certain type (a singleton), you can define it like this: object NameOfObject { …code… } The code is executed the first time you refer to the object Variables ( val and var ) are declared Methods ( def ) are defined “Loose” code (not contained in a method) is executed You can refer to the fields (variables) and methods of the object using dot notation NameOfObject.variable NameOfObject.method ( arg1, …, argN ) Within the object definition, you don’t need to use dot notation to refer to the fields and methods of “this” object

Example object object USA { val numberOfContiguousStates = 48 val numberOfStates = numberOfContiguousStates + 2 var president = "George W. Bush" def isContiguous(state: String) = state != "Alaska" && state != "Hawaii" def isNotContiguous(state: String) = !isContiguous(state) } println("The USA has " = USA.numberOfStates + " states.") USA.president = "Barack Hussein Obama" val here = "Pennsylvania“ println(here + (if (isContiguous(here) " is " else " is not ") + " a continental state.")

Simple programs The simplest complete program consists of one object containing a main method The main method looks exactly like def main(args: Array[String]) { … code in main method… } A “Hello World” program looks like this: object SimpleProgram { def main(args: Array[String]) = { println("Hello from a Scala program.") } } To run the program, put it on a file with the extension.scala (for example, simple.scala ), then say scala simple.scala

Order of execution When you run a Scala program, things happen in approximately this order: 1.Scala figures out the types of everything, and gives default values to variables ( 0 for numbers, false for booleans, null for most other kinds of object) 2.Working from top to bottom, Scala evaluates all the val s and var s and defines all the methods ( def s) 3.If there is a main method, Scala executes (runs) it The detailed rules are quite complicated C and C++ require definitions to be in a certain order Scala gives you a lot of freedom in ordering your definitions and variables

About args: Array[String] Array[String] means “an array of Strings” When you run the program, you can give it some Strings as input Arrays are covered elsewhere in this course, so you don’t need to worry about them right now Just for completeness, here’s a simple example object SimpleProgramWithArguments { def main(args: Array[String]) { println("Here are my arguments:") for (word <- args) { println(word) } } }

Classes When you want more than one object of a given kind, you use a class Because these objects will differ in some respects, a class is usually defined with parameters: class NameOfClass ( parameters ) { …code… } The code is evaluated each time you create a new object Variables ( val and var ) are declared Methods ( def ) are defined (not called) “Loose” code (not contained in a method) is executed The type of each parameter must be specified Each parameter may be marked as val or var val means you can read the value from outside the class definition var means you can read or write the value from outside neither means you can only access the variable from inside the class

Example class class Planet(val name: String, val orderFromSun: Int, populationIn2011: Long) { def population(year: Int) = { require(year >= 2011) var pop = populationIn2011.toDouble for (y < to year) { pop = * pop } pop.toLong } } val earth = new Planet("Earth", 3, L) val mars = new Planet("Mars", 4, 0) val planet = earth println("Population of " + planet.name + " in 2050 will be " + planet.population(2050))

Example class with object class Planet is exactly as defined on previous slide Add this object: object SolarSystem { def main(args: Array[String]) { val earth = new Planet("Earth", 3, L) val mars = new Planet("Mars", 4, 0) val planet = earth println("Population of " + planet.name + " in 2050 will be " + planet.population(2050)) } } Now we have to run the main method explicitly: SolarSystem.main(Array())

Running from the command line If your program consists of just one object, you can run it by specifying the name of the file it is on: scala SolarSystem.scala It is conventional to give the file the same name as the object (plus the.scala extension), but this is not required If you just say scala without a file name, this starts the REPL If your program contains classes or additional objects, you have to compile it (with scalac ) and then run it (with scala ) dave$ scalac SolarSystem.scala dave$ scala SolarSystem Population of Earth in 2050 will be Macintosh:Scala_programs When you compile a program, you get some number of.class files, many with a $ in their name.class files are binary files, runnable on the Java Virtual Machine (JVM)

Arguments to a program Here’s a program from an earlier slide: object SimpleProgramWithArguments { def main(args: Array[String]) { println("Here are my arguments:") for (word <- args) { println(word) } } } When you run it, everything after the file name is treated as a space-separated array of Strings dave$ scala SimpleProgramWithArguments.scala This is CIS 591 Here are my arguments: This is CIS 591

13 The End