Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

Size: px
Start display at page:

Download "Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing"

Transcription

1 Inf1-OOP Using Data Types 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 11, 2014 Overview Image Processing String Processing Summary/Admin 1 Thanks to Sedgewick&Wayne for much of this content A Foundation for Programming Data Types Data type: a set of values and operations on those values. Primitive types: data types whose operations translate directly to machine instructions. objects functions and modules graphics, sound and image I/O arrays conditionals and loops create your own data types type value set operations int integers add, subtract, multiply, divide double floating-point numbers add, subtract, multiply, divide boolean truth values and, or, not Math primitive data types text I/O assignment statements But we want to write code that will process other types of data: Colors, pictures, strings, input streams,... Complex numbers, vectors, matrices, polynomials,... Points, polygons, charged particles, celestial bodies,...

2 User-defined data types Object An object has state, behaviour and identity. Java is a class-based object-oriented language. A class is a kind of data type that you can define yourself. An object is an instance of a class. The code in a Java system is organised into classes. The system runs by objects sending messages to one another, and reacting to receiving messages, possibly by sending other messages... the sender trusts the receiver to do the right thing (Most) variables refer to objects. State: the data that the object encapsulates the object may decide to modify its state in response to a message but immutable objects never do this Behaviour: the messages the object understands, and what it does in response exactly what an object does in response to a message may depend on its state Identity: two objects may currently have identical data, but be different objects e.g. o1 and o2 could be in the same state now, but not after you sent a message to o1, which affects o1 s state but not o2 s. Why use object orientation? OO has taken the world by storm. Why? For programming, OOP is nice, but not clearly better than FP, say. For software engineering, OO is definitely better use objects to model real-world things use classes to model domain concepts. These change more slowly than specific functional requirements, so what OO does is to put things together that change together as requirements evolve. Change is the thing that makes software engineering hard and interesting; OO helps manage it. for most applications, E&OE, don t tell PW I said so :-)

3 Constructors and Methods Static Methods vs. Instance Methods declare a variable (object name) String s; call a constructor to create an object s = new String("Hello, World!"); System.out.println( s.substring(0, 5) ); object name call an instance method that operates on the object's value Static Methods: Associated with a class. Identifying a method in a separate class requires name of the class: Math.abs(), PointDistance.distance(). Instance Methods: Associated with an object. Identifying an instance method requires an object name: s.substring() Colour Data Type Colour: a visual perception property in humans deriving from interaction of the eye with electromagnetic radiation. Set of values (RGB representation): possible values, which quantify the amount of red, green and blue, each on a scale of 0 to 255. R G B Colour Java Color API (part of) public class java.awt.color Color(int r, int g, int b) constructor int getred() red intensity int getgreen() green intensity int getblue() blue intensity Color brighter() brighter version of this colour Color darker() darker version of this colour String tostring() string representation of this colour boolean equals(object o) is o same as this Color? http: //docs.oracle.com/javase/7/docs/api/java/awt/color.html Standard format for expressing APIs. Will be used in coming weeks.

4 Albers Squares Albers Squares blue grey % java AlbersSquares Homage to the Square, Josef Albers (series painted ) Using Colours in Java Using Colours in Java import java.awt.color; access the Color library public class AlbersSquares { public static void main(string[] args) { int r1 = Integer.parseInt(args[0]); int g1 = Integer.parseInt(args[1]); int b1 = Integer.parseInt(args[2]); Color c1 = new Color(r1, g1, b1); int r2 = Integer.parseInt(args[3]); int g2 = Integer.parseInt(args[4]); int b2 = Integer.parseInt(args[5]); Color c2 = new Color(r2, g2, b2); StdDraw.setPenColor(c1); StdDraw.filledSquare(.25,.5,.2); StdDraw.setPenColor(c2); StdDraw.filledSquare(.25,.5,.1); first colour second colour first square Color c1 = new Color(r1, g1, b1): Call the Color constructor to create a new color object c1 with arguments for RGB. Example makes use of StdDraw library filledsquare(double x, double y, double r): Draw a filled square of side length 2r, centered on (x, y). StdDraw.setPenColor(c2); StdDraw.filledSquare(.75,.5,.2); StdDraw.setPenColor(c1); StdDraw.filledSquare(.75,.5,.1); second square

5 Monochrome Luminance Colour Compatibility Monochrome Luminance: effective brightness of a colour. In TV transmission, calculated as the weighted sum of R, G and B. NTSC formula for luminance: Y = (0.299 R) + (0.587 G) + (0.114 B) import java.awt.color; public class Luminance { public static double luminance(color color) { int r = color.getred(); int g = color.getgreen(); int b = color.getblue(); return 0.299*r *g *b; Q: Which font colours will be most readable on given background colours on computer and mobile phone screens? A: Rule of thumb: luminance should be Grayscale Grayscale: when all three R, G and B values are the same, the resulting colour is on grayscale, from 0 (black) to 255 (white). Conversion: Coloured images can be converted to grayscale using luminance to determine the value. public static Color togrey(color color) { int y = (int) (Math.round(lum(color))); Color gray = new Color(y, y, y); return grey; round double to nearest int red green blue this colour: grayscale version: black: Color Data Type The Color data type is a way of abstracting away from the physical reality of colour. Separates the task of specifying and manipulating colours from the task of reproducing colours in various mediums (print, web, photos,... ). The Color data type implements one particular model of colour, namely RGB (three 8-bit integers). Y = ( ) + ( ) + ( ) =

6 Color Data Type OOP Context for Colour Possible memory representation: The method luminance() takes a parameter of type Color, which in turn allows it to call methods that are part of the Color API; e.g., Color.getRed(). The method togrey() returns a value of type Color, which in effect allows it to return a bundle of three integer values (i.e., the RGB values of a particular colour). Color is called reference type (vs. primitive type). D0 D1 D2 D3 D4 D5 D6 D7 D magenta A0 D0 memory address ("pointer") B0 D6 gray Object reference is analogous to variable name: We can manipulate the value that it holds. We can pass it to a method, or return it from one. String Data Type (part of) Typical String Processing Code String Data Type: basis for text processing Set of Values: sequence of Unicode characters public class String String(String s) create a string with same value as s char charat(int i) character at index i String concat(string t) this string with t appended int compareto(string t) compare lexicographically with t boolean endswith(string post) does string end with post? boolean equals(object t) is t a String equal to this one? int indexof(string p) index of first occurrence of p int indexof(string p, int i) as indexof, starting search at index i int length() return length of string String replaceall(string a, String b) result of changing all as to bs String[] split(string delim) result of splitting string at delim boolean startswith(string pre) does string start with pre? String substring(int i, int j) from index i to index j 1 inclusive is the string a palindrome? extract filenames and extensions from a command-line argument print all lines from standard input containing the string info print all ac.uk URLs in text file on standard input public static boolean ispalindrome(string s) { int N = s.length(); for (int i = 0; i < N / 2; i++) { if (s.charat(i)!= s.charat(n i)) return false; return true; String s = args[0]; int dot = s.indexof("."); String base = s.substring(0, dot); String extension = s.substring(dot + 1, s.length()); while (!StdIn.isEmpty()) { String s = StdIn.readLine(); if (s.contains("info")) System.out.println(s); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.startswith(" && s.endswith("ac.uk")) System.out.println(s);

7 Gene Finding Gene Finding: Algorithm Pregenomic era: Sequencing the human genome [Human Genome Project, 2003]. Postgenomic era: Analyse the data and understand structure. Genome: Represent genome as a string over {A, C, T, G alphabet. Gene: A substring of genome that represents a functional unit. Preceded by ATG (start codon). Multiples of three nucleotides (codons other than start/stop). Succeeded by TAG, TAA or TGA (stop codons). Goal: find all genes A T A G A T G C A T A G C G C A T A G C T A G A T G T G C T A G C start gene stop gene Algorithm: Scan left-to-right through genome. If start codon, then set beg to index i. If stop codon and substring is multiple of 3: output gene reset beg to -1 i start stop beg gene remaining portion of input 0-1 ATAGATGCATAGCGCATAGCTAGATGTGCTA 1 TAG -1 ATAGATGCATAGCGCATAGCTAGATGTGCTA 4 ATG 4 ATAGATGCATAGCGCATAGCTAGATGTGCTA 9 TAG 4 ATAGATGCATAGCGCATAGCTAGATGTGCTA 16 TAG 4 CATAGCGCA ATAGATGCATAGCGCATAGCTAGATGTGCTA 20 TAG -1 ATAGATGCATAGCGCATAGCTAGATGTGCTA 23 ATG 23 ATAGATGCATAGCGCATAGCTAGATGTGCTA 29 TAG 23 TGC ATAGATGCATAGCGCATAGCTAGATGTGCTA Strings and Equality OOP Framework for Strings Possible memory representation of a string genome = "AACAAGTTTACAAGC" equals(): are the characters inside a String object the same? ==: do the two object references refer to the same instance? In all the coursework for this course, you should be using equals() for checking whether two strings are the same D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE A0 A1 A A C A A G T T T A C A A G C D0 15 s = genome.substring(1, 5); t = genome.substring(9, 13); s memory address genome t B0 B1 B2 B3 D1 4 D9 4 length (s == t) is false (s.equals(t)) is true s and t are different strings that share the same value "ACAA"

8 Summary Reading An object is an instance of a class, which is a kind of data type often represents a real-world thing has state, behaviour and identity. A variable can have a primitive type e.g., boolean, int, double; or a reference type: any class, e.g. String, Picture, Color, any array type. Zakhour pp (Classes, objects tells you how to build your own (next lecture), as well as use them (this lecture)) pp ([Characters and] Strings) Sedgewick & Wayne: Data Types Read Section 3.1 (pp )

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing Inf1-OOP Using Data Types 1 Ewan Klein, Perdita Stevens School of Informatics January 10, 2013 Overview Image Processing String Processing Summary/Admin 1 Thanks to Sedgewick&Wayne for much of this content

More information

Inf1-OP. Classes and Objects. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. November 23, School of Informatics

Inf1-OP. Classes and Objects. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. November 23, School of Informatics Inf1-OP Classes and Objects Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics November 23, 2017 Software engineering as managing change The computer doesn

More information

Software engineering as managing change. Inf1-OP. Data representation. How can we make change easier and cheaper?

Software engineering as managing change. Inf1-OP. Data representation. How can we make change easier and cheaper? Software engineering as managing change Inf1-OP Classes and Objects Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 13, 2017 The computer doesn

More information

Using data types. Overview. Data types. Why custom data types? Using data types. A slight diversion: Methods. Data type. Data type

Using data types. Overview. Data types. Why custom data types? Using data types. A slight diversion: Methods. Data type. Data type Using data types Using data types Overview What they are Constructors and methods Application Programming Interface (API) Image processing Text processing A slight diversion: Incrementing and decrementing

More information

9. Abstract Data Types

9. Abstract Data Types COMPUTER SCIENCE COMPUTER SCIENCE Overview Color Image processing String processing Section 3.1 http://introcs.cs.princeton.edu CS.9.A.ADTs.Overview Abstract data types Object-oriented programming (OOP)

More information

9. Abstract Data Types

9. Abstract Data Types COMPUTER SCIENCE S E D G E W I C K / W A Y N E 9. Abstract Data Types Section 3.1 http://introcs.cs.princeton.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 9. Abstract Data Types Overview Color Image

More information

9. Abstract Data Types

9. Abstract Data Types COMPUTER SCIENCE S E D G E W I C K / W A Y N E 9. Abstract Data Types Section 3.1 http://introcs.cs.princeton.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 9. Abstract Data Types Overview Color Image

More information

3.1 Using Data Types. Data Type. Constructors and Methods. Objects. Data type. Set of values and operations on those values.

3.1 Using Data Types. Data Type. Constructors and Methods. Objects. Data type. Set of values and operations on those values. Data Types 3.1 Using Data Types Data type. Set of values and operations on those values. Primitive types. Ops directly translate to machine instructions. Data Type boolean int double Set of Values true,

More information

3.1 Data Types. Abstract Data Types. Objects. Object. Holds a data type value; variable name refers to object.

3.1 Data Types. Abstract Data Types. Objects. Object. Holds a data type value; variable name refers to object. 3.1 Data Types any program you might want to write objects create your own data types functions and modules graphics, sound, and image I/O arrays conditionals and loops Math primitive data types text I/O

More information

Using data types Fundamentals of Computer Science Keith Vertanen Copyright 2013

Using data types Fundamentals of Computer Science Keith Vertanen Copyright 2013 Using data types Fundamentals of Computer Science Keith Vertanen Copyright 213 Using data types Overview What they are Constructors and methods Applica;on Programming Interface (API) Image processing Text

More information

A Foundation for Programming

A Foundation for Programming 3.1 Using Data Types Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2008 11/20/09 11/20/09 A Foundation for Programming any program you might

More information

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1. Overview Inf1-OOP Creating Data Types 1 Circle Class Object Default Perdita Stevens, adapting earlier version by Ewan Klein Format Strings School of Informatics January 11, 2015 HotelRoom Class More on

More information

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1 Tutorials Inf1-OP Conditionals and Loops 1 Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein Start this week please let the ITO know if you need to switch tutorial groups. Labs

More information

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

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics Inf1-OOP OOP Exam Review Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 16, 2015 Overview Overview of examinable material: Lectures Topics S&W sections Week 1 Compilation,

More information

Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming

Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming Learning Outcomes for this week Inf1-OP Conditionals and Loops 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 9, 2016 Use if and if-else statements to execute a

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Inf1-OOP. Encapsulation and Collections. Perdita Stevens, adapting earlier version by Ewan Klein. March 2, School of Informatics

Inf1-OOP. Encapsulation and Collections. Perdita Stevens, adapting earlier version by Ewan Klein. March 2, School of Informatics Inf1-OOP Encapsulation and Collections Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 2, 2015 Encapsulation Accessing Data Immutability Enhanced for loop Collections

More information

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics Inf1-OP Inf1-OP Exam Review Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 20, 2017 Overview Overview of examinable material: Lectures Week 1

More information

Admin. CS 112 Introduction to Programming. Exercise: Gene Finding. Language Organizing Structure. Gene Finding. Foundational Programming Concepts

Admin. CS 112 Introduction to Programming. Exercise: Gene Finding. Language Organizing Structure. Gene Finding. Foundational Programming Concepts Admin CS 112 Introduction to Programming q PS6 (Sukoku) questions? q Planning of remaining of the semester User-Defined Data Types Yang (Richard) Yang Computer Science Department Yale University 308A Watson,

More information

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java Organisational issues Inf1-OOP Course Overview Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 11, 2014 Why Java? Hello, World! in Java Built-in Types Integers Floating-Point

More information

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

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types Classes and objects A foundation for programming any program you might want to write objects functions and modules build even bigger programs and reuse code http://www.flickr.com/photos/vermegrigio/5923415248/

More information

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CLASSES AND OBJECTS. Fundamentals of Computer Science I CLASSES AND OBJECTS Fundamentals of Computer Science I Outline Primitive types Creating your own data types Classes Objects Instance variables Instance methods Constructors Arrays of objects A Foundation

More information

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens Organizational Issues Inf1-OOP Course Overview Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Why Java? Hello, World! in Java Built-in Types Integers Floating-Point Numbers Type Conversion

More information

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CLASSES AND OBJECTS. Fundamentals of Computer Science I CLASSES AND OBJECTS Fundamentals of Computer Science I Outline Primitive types Creating your own data types Classes Objects Instance variables Instance methods Constructors Arrays of objects A Foundation

More information

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method Appendix 3 Java - String charat() Method This method returns the character located at the String's specified index. The string indexes start from zero. public char charat(int index) index -- Index of the

More information

Inf1-OOP. Encapsulation. Object Oriented Programming. Encapsulation Accessing Data Immutability. Encapsulation and Collections.

Inf1-OOP. Encapsulation. Object Oriented Programming. Encapsulation Accessing Data Immutability. Encapsulation and Collections. Inf1-OOP Encapsulation and Collections Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Encapsulation Accessing Data Immutability Enhanced for loop Collections ArrayList Maps Summary/Admin

More information

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

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling

More information

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin Inf1-OOP Arrays 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin January

More information

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College Strings AP Computer Science Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unported License

More information

More on variables and methods

More on variables and methods More on variables and methods Robots Learning to Program with Java Byron Weber Becker chapter 7 Announcements (Oct 12) Reading for Monday Ch 7.4-7.5 Program#5 out Character Data String is a java class

More information

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics Inf1-OOP Arrays 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 11, 2015 1 Thanks to Sedgewick&Wayne for much of this content Arrays Introduction to Arrays Arrays

More information

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings. CS2110, Recita.on 1 Arguments to method main, Packages, Wrapper Classes, Characters, Strings Demo: Create application To create a new project that has a method called main with a body that contains the

More information

Creating Strings. String Length

Creating Strings. String Length Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

String is a Class; Quoted Text is an Object

String is a Class; Quoted Text is an Object String is a Class; Quoted Text is an Object String s = "abc d"; Indexed characters: 01234 abc d s.length() is 5 s.charat(2) is 'c' s.substring(2) is "c d" s.substring(1,3) is "bc" @3e9cff "abc d" length()

More information

"Hello" " This " + "is String " + "concatenation"

Hello  This  + is String  + concatenation Strings About Strings Strings are objects, but there is a special syntax for writing String literals: "Hello" Strings, unlike most other objects, have a defined operation (as opposed to a method): " This

More information

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters Overloaded Methods Sending Messages Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Initialization & Cleanup 2 Overloaded Constructors Sending Parameters accessor method 3 4 Sending Parameters

More information

Lecture 4. Strings, Wrappers, & Containers

Lecture 4. Strings, Wrappers, & Containers Lecture 4 Strings, Wrappers, & Containers Announcements for This Lecture Readings pp. 175 181 Sections 2.5, 3.1.2-3.1.3 Also Section 5.2 PLive (optional): Lesson 2-5 Lessons 5-1 and 5-2 Assignments Assignment

More information

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance Admin CS 112 Introduction to Programming q Class project Inheritance Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu 2 Recap: OOP Analysis

More information

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended JAVA Classes Class definition complete definition [public] [abstract] [final] class Name [extends Parent] [impelements ListOfInterfaces] {... // class body public public class abstract no instance can

More information

A Foundation for Programming

A Foundation for Programming 2.1 Functions A Foundation for Programming any program you might want to write objects functions and modules build bigger programs and reuse code graphics, sound, and image I/O arrays conditionals and

More information

AWT COLOR CLASS. Introduction. Class declaration. Field

AWT COLOR CLASS. Introduction. Class declaration. Field http://www.tutorialspoint.com/awt/awt_color.htm AWT COLOR CLASS Copyright tutorialspoint.com Introduction The Color class states colors in the default srgb color space or colors in arbitrary color spaces

More information

Unit 4: Classes and Objects Notes

Unit 4: Classes and Objects Notes Unit 4: Classes and Objects Notes AP CS A Another Data Type. So far, we have used two types of primitive variables: ints and doubles. Another data type is the boolean data type. Variables of type boolean

More information

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics Inf1-OP Collections Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 9, 2016 Rigidity of arrays Length of array is fixed at creation time. Can t be expanded. Can t

More information

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon Who and what can help? Inf1-OP Lecturer: Timothy Hospedales TA: Natalia Zon Course Overview Web: http://www.inf.ed.ac.uk/teaching/ courses/inf1/op/ Timothy Hospedales, adapting earlier version by Perdita

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination Thursday, December 11, 2008 Examiners: Mathieu Petitpas [Section 1] 14:00

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of January 21, 2013 Abstract Review of object-oriented programming concepts: Implementing

More information

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Java By Abstraction - Test-B (Chapters 1-6)

Java By Abstraction - Test-B (Chapters 1-6) Java By Abstraction - Test-B (Chapters 1-6) Last Name First Name Do not write below this line B (60%) A (40%) TOTAL String Methods (invoke on a string s) boolean equals(string t) Returns true if s and

More information

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive

More information

CS 101 Exam 1 Spring 200 Id Name

CS 101 Exam 1 Spring 200  Id Name This exam is open text book and closed notes. Different questions have different points associated with them with later occurring questions having more worth than the beginning questions. Because your

More information

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture. CSE1710 Click to edit Master Week text 12, styles Lecture 23 Second level Third level Fourth level Fifth level Fall 2014! Thursday, Nov 27, 2014 1 Big Picture Today is last day covering Ch 6 Tuesday, Dec

More information

3.2 Creating Data Types

3.2 Creating Data Types 1 3.2 Creating Data Types Data Types Data type. Set of values and operations on those values. Basic types. Data Type boolean int String Set of Values true, false -2 31 to 2 31-1 sequence of Unicode characters

More information

1.5 Input and Output. Introduction to Computer Science Sedgewick and Wayne Copyright 2007

1.5 Input and Output. Introduction to Computer Science Sedgewick and Wayne Copyright 2007 1.5 Input and Output Introduction to Computer Science Sedgewick and Wayne Copyright 2007 http://introcs.cs.princeton.edu A Foundation for Programming any program you might want to write objects functions

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Computer Science. 9. Creating Data Types. Computer Science COMPUTER SCIENCE. Section 3.2.

Computer Science. 9. Creating Data Types. Computer Science COMPUTER SCIENCE. Section 3.2. COMPUTER SCIENCE S E D G E W I C K / W A Y N E PA R T I : P R O G R A M M I N G I N J AVA Computer Science Computer Science An Interdisciplinary Approach Section 3.2 ROBERT SEDGEWICK K E V I N WAY N E

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Object-Oriented Programming Classes, Objects, Variables

Object-Oriented Programming Classes, Objects, Variables Object-Oriented Programming Classes, Objects, Variables Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) Object-Oriented ProgrammingClasses, Objects, Variables Inf1 ::

More information

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections Rigidity of arrays Inf1-OP Collections Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics Length of array is fixed at creation time. Can t be expanded. Can

More information

DATA TYPES AND EXPRESSIONS

DATA TYPES AND EXPRESSIONS DATA TYPES AND EXPRESSIONS Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment Mathematical

More information

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture. CSE1710 Click to edit Master Week text 12, styles Lecture 22 Second level Third level Fourth level Fifth level Fall 2014 Tuesday, Nov 25, 2014 1 Big Picture Tuesday, Dec 02 is designated as a study day.

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

More information

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs. Java SE11 Development Java is the most widely-used development language in the world today. It allows programmers to create objects that can interact with other objects to solve a problem. Explore Java

More information

10. Creating Data Types

10. Creating Data Types COMPUTER SCIENCE S E D G E W I C K / W A Y N E 10. Creating Data Types Section 3.2 http://introcs.cs.princeton.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 10. Creating Data Types Overview Point

More information

CISC 1115 (Science Section) Brooklyn College Professor Langsam. Assignment #5

CISC 1115 (Science Section) Brooklyn College Professor Langsam. Assignment #5 CISC 1115 (Science Section) Brooklyn College Professor Langsam Assignment #5 An image is made up of individual points, known as pixels. Thus if we have an image with a resolution of 100 x 100, each pixel

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass Encapsulation Again Inheritance Encapsulation and Inheritance Inf1-OOP Inheritance and Interfaces Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 9, 2015 The Object

More information

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics Inf1-OP Course Overview Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Administrative Stuff Who to contact for help? Lecturer: Volker

More information

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

More information

COS 126 Exam 2 Review Part 1

COS 126 Exam 2 Review Part 1 COS 126 Exam 2 Review Part 1 Programming Exam 2 Part 1 (ADT) Q. Can you implement a simple abstract data type? Example (Fall 2016). Part 1. Implement a data type ColorHSB.java for HSB colors. This time,

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Intro to Computer Science II

Intro to Computer Science II Intro to Computer Science II CS112-2012S-04 Strings David Galles Department of Computer Science University of San Francisco 04-0: Types in Java Primative Types Hold simple values Can be stored on the stack

More information

Lab 14 & 15: String Handling

Lab 14 & 15: String Handling Lab 14 & 15: String Handling Prof. Navrati Saxena TA: Rochak Sachan String Handling 9/11/2012 22 String Handling Java implements strings as objects of type String. Once a String object has been created,

More information

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All This chapter discusses class String, from the java.lang package. These classes provide the foundation for string and character manipulation

More information

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University Mathematical Functions, Characters, and Strings CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Static methods Remember the main method header? public static void

More information

1.1 Your First Program

1.1 Your First Program 1.1 Your First Program Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 5/20/2013 9:37:22 AM Why Programming? Why programming? Need

More information

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions Birkbeck (University of London) Software and Programming 1 In-class Test 1.1 9 Feb 2017 Student Name Student Number Answer all questions 1. Consider the following sequence of Java statements: int m = 3;

More information

A Founda4on for Programming

A Founda4on for Programming 3.1 Objects LOGO STYLE GUIDE Schools within the University Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 3/16/14 11:29 PM A Founda4on

More information

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a Spring 2017 Name: TUID: Page Points Score 1 28 2 18 3 12 4 12 5 15 6 15 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. i Some API Reminders

More information

Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2 Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2011 PLC 2011, Lecture 2, 6 January 2011 Classes and

More information

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

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Advanced Java This chapter discusses class String, class StringBuilder and class Character from the java.lang package. These classes provide

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 Name: This exam consists of 6 problems on the following 7 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited Arrays and Strings Arrays - homogeneous indexed collections Selection sort as array example Introduction to complexity analysis String class, revisited 1 Arrays Homogeneous collections of elements of same

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name: Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name: CS Login Name: NetID (email): @wisc.edu Circle your Lecture: Lec001

More information

Inf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics

Inf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics Inf1-OP Collections Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 6, 2017 Rigidity of arrays Length of array is fixed at creation time. Can

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Strings! Today! CSE String Methods!

Strings! Today! CSE String Methods! Strings CSE 1710 Lecture 14, 15 String Handling We have covered three chunks of material: Week 1: String Literals pp. 22-23; Fig 1.12; PT 1.8 Week 6: The String Class Section 6.1.1, pp. 219-220 The Masquerade

More information

Research Group. 2: More types, Methods, Conditionals

Research Group. 2: More types, Methods, Conditionals Research Group 2: More types, Methods, Conditionals Outline Lecture 1 Review More types Methods Conditionals 2 Types Kinds of values that can be stored and manipulated. boolean: Truth value (true or false).

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View 1/16/12 A Foundation for Programming CS 112 Introduction to Programming (Spring 2012) any program you might want to write Lecture #4: Built-in Types of Data objects Zhong Shao methods and classes graphics,

More information

3.2 Creating Data Types

3.2 Creating Data Types Data Types 3.2 Creating Data Types Data type. Set of values and operations on those values. Basic types. Data Type boolean int String Set of Values true, false -2 31 to 2 31-1 sequence of Unicode characters

More information