Notes from the Boards Set BN19 Page

Similar documents
1 Getting started with Processing

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Using System.out.println()

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

AP Computer Science A

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

Lecture Notes for CS 150 Fall 2009; Version 0.5

Lecture 7. Strings & Stepwise Refinement

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Programming with Java

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

c) And last but not least, there are javadoc comments. See Weiss.

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

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

Fall 2017 CISC124 10/1/2017

Miscellaneous Stuff That Might Be Important.

Programming with Java

1 Getting started with Processing

COMP-202 Unit 5: Basics of Using Objects

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Chapter 1 Introduction to java

Basic Computer Programming (Processing)

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

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

Exceptions and Design

Program Fundamentals

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Midterms Save the Dates!

CS 101 Exam 1 Spring 200 Id Name

Unit 4: Classes and Objects Notes

Intro to Computer Science II

Variables One More (but not the last) Time with feeling

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

CS 11 java track: lecture 1

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

First Java Program - Output to the Screen

Operator overloading

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Defining Classes and Methods

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

QUIZ. What is wrong with this code that uses default arguments?

Java Foundations: Unit 3. Parts of a Java Program

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

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

Chapter 11: Create Your Own Objects

Building Java Programs. Introduction to Programming and Simple Java Programs

Arduino notes IDE. Serial commands. Arduino C language. Program structure. Arduino web site:

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

MEMORIZE the Java App pick: public class { public static void main(string[] a) {

How to make a "hello world" program in Java with Eclipse *

CSE 143. Computer Programming II

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

Exploring Processing

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

CS 1301 Ch 8, Part A

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

2 rd class Department of Programming. OOP with Java Programming

Programming Language Concepts: Lecture 2

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Midterms Save the Dates!

Processing/Java Syntax. Classes, objects, arrays, and ArrayLists

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

Getting started with Java

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

3. Java - Language Constructs I

1007 Imperative Programming Part II

Introduction to Objects. James Brucker

Gettysburg College Department of Computer Science

Design Patterns: State, Bridge, Visitor

St. Edmund Preparatory High School Brooklyn, NY

Week 8: Operator overloading

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CST112 Variables Page 1

String is a Class; Quoted Text is an Object

Java+- Language Reference Manual

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

Computer Science AP 2017 Summer Assignment Mrs. McFarland

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CHAPTER INTRODUCTION

Designing Robust Classes

STRINGS AND STRINGBUILDERS. Spring 2019

Array. Prepared By - Rifat Shahriyar

Basic Programming Language Syntax

Getting started with Java

Lecture 4. Strings, Wrappers, & Containers

Transcription:

1 The Class, String There are five programs in the class code folder Set17. The first one, String1 is discussed below. The folder StringInput shows simple string input from the keyboard. Processing is really Java. It is referred to as a super set of Java because it is all of Java and more. Much of the java aspects are hidden from view by the Processing IDE. We will look at some of the hidden stuff next class meeting. The main organizing structure of Java and Processing is the class. One main reason for using a class structure is simply the size and complexity of the language. A way to consider this is to think about a library. It is composed of books lots and lots of books. But, what if every book in the library was pasted together into a single large book? That s right one book. Think of the ramifications: only one person could check it out at a time it would be difficult to find something specific its shear size and weight would make it very cumbersome and be difficult to work with in any way This same list and more is one reason that Java is broken up into smaller units or segments. These segments are called classes. The class list in the Java API has over 2000 entries. We have been programming inside a class since we wrote the first program on day 1. We will look at this next time. As a first look at a class, we will look at the String class of Java. Here are the high points: 1. Anything inside quotation marks ( ) is a String. Think of a string of pearls, a string of beads, a string of characters. Here are some examples of Strings: the empty string the space as a string hello world which is one of the most famous String 2. Strings are NOT arrays. They are references to pieces of memory called objects or in this discussion, String objects.

2 3. The characters in a String object are indexed like arrays beginning at zero (but they are not arrays). 4. There is a length field in the String object but it is private we cannot directly access it. To find the length of a String, we must use the length( ) function. 5. The last character in a String has an index of length( )-1 The processing API lists seven String functions (In Java, functions have a different name they are methods. sigh functions, methods, parameters, arguments, sigh again ). that may be useful to you. These are used in the classcodea program. The JAVA API has over 2000 functions (methods) listed for the class. All of these can be used by you in a Processing program because Processing is really Java. Here is a list of comparisons and contrasts between an array of char and a String; Category Array of char String declaration char [ ] c; String s; initialization char [ ] c = 'a', 'b'; String s = "hello"; access println( c[ 0 ] ); println( s.charat( 0 ) ); Changing values c[ 0 ] = 'x'; Illegal for Strings Size println( c.length ); println( s.length( ) ); Index of beginning character Index of last character println( c[ 0 ] ); println( s.charat( 0 ) ); println( c[ c.length - 1 ] ); println( s.charat( s.length( ) - 1 ) ); Equality == compiles, runs, and does not work properly == compiles, runs, and usually does not work properly if ( s.equals(s1) ) compiles, runs and works properly One question that is asked is how can the touppercase( ) and tolowercase( ) functions be used. One way to use them is when we are dealing with user input (discussed later in these notes).

3 If we ask the use to type either yes or no as a reply to a question the user can type any of the following to correctly respond: yes Yes YES yes yes YeS no No NO no Any of these are proper inputs. Yet we do not want to have to type: if ( s1.equals( "YES" ) s1.equals( "yes" ) s1.equals( "yes" ) s1.equals( "yes" ) s1.equals( "YeS" ) s1.equals( "yes" ) ) This is silly. What if we were asking for the capital of the state of Florida? We can use the case changing functions to simplify the code: if ( s.touppercase().equals("yes" ) ) The function touppercase() returns a new String and we can use the equals( ) function with the returned String. This does look a bit bizarre, but there is a simpler way to write this code: String temp = s.touppercase( ); if ( temp.equals("yes" ) ) Arrays of String References We can build arrays of any type of data so we can build an array of Strings. The syntax is the same: String [ ] labels = "On", "Off", "In", "Out" ;

4 String Input We have used single character input for weeks. We have done this in the keypressed( ) function. The advantage of this is that the program does not pause and wait for the input. If we have no animation ongoing when we want input, a pause in the action is fine. But, if there is animation occurring, a pause for input is not good. Processing provides no other direct way to get user input and we do not want to explore Java s ways of getting user input so we will keep using the keypressed() function to do our work. Here is part of the code in A_BoardNotes that gets input from the user: Code Comments void keypressed( ) if (keycode == DELETE keycode == BACKSPACE ) If we assume the user might not type things correctly and will want to correct their input, we have to account for the delete (Mac) or backspace (Windows) keys. These keys are CODED so we check the keycode value first. if ( s.length() >0 ) If the user typed one of these coded keys, we must first test the String to insure it is not empty. Our code will crash is we try to delete a character from an empty String. s = s.substring (0, s.length( )-1); We cannot alter a String so we have to make an new String without the last character. The substring( ) function does this. The details of the parameters are discussed below.

5 else s = s + key; If the user did not type the coded keys tested for above, this assumes they typed a character and a new String is made from the old String and the new letter using the concatenation operator + The parameters for the call of the substring() function need some explanation: s = s.substring (0, s.length( )-1); The last character in the String has an index of : s.length( )-1 If we want a new String created that does not have the last character, shouldn t we use the following for the second parameter: s = s.substring (0, s.length( )-2); This is one time that our logic fails us. When we specify a range of values, there are many functions that do not include the second value in the range. For example, if we call the random( ) function like this: float f = random( 10, 20 ); we get values between: 10.0 and 19.9999999 We do not get 20 in the range of returned values. In fact when we draw a line like this: line( 0, 0, 400, 400 ); The line is drawn from pixel ( 0, 0 ) to pixel ( 399, 399 ). In the parameter list for substring( ): s = s.substring (0, s.length( )-1); The new String that is returned has character from index 0 up to BUT NOT INCLUDING the character at the index of the second parameter.