Activity 9: Object-Oriented

Similar documents
Activity 9: Object-Oriented

STUDENT LESSON A10 The String Class

Activity 3: Data Types

Activity 1: Introduction

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

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

Activity 1: Introduction

Activity 4: Boolean Logic

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

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

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

Programming with Java

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

TEST (MODULE:- 1 and 2)

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

Activity 7: Arrays. Content Learning Objectives. Process Skill Goals

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Announcements. CompSci 100e Program Design and Analysis II

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Netbeans tutorial:

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Lecture 5. Assignments. Arithmetic Operations. Arithmetic Operations -Summary 1/24/18. Lab 3: Variables and operations. Read Sections

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Using Java Classes Fall 2018 Margaret Reid-Miller

Midterms Save the Dates!

Introduction to Programming Using Java (98-388)

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

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

Welcome to the Using Objects lab!

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Using Objects. a first example of an object classes and objects in Java

Computational Expression

More non-primitive types Lesson 06

Strings! Today! CSE String Methods!

Voice Application Specification. SBString

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CSC 222: Object-Oriented Programming. Spring 2017

Question: Total Points: Score:

Utilities (Part 2) Implementing static features

H212 Introduction to Software Systems Honors

Computer Science 145 Midterm 1 Fall 2016

appreciate the difference between a char and a string understand and use the String class methods

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

CSC 222: Object-Oriented Programming. Spring 2013

Activity 11: Designing Classes

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Chapter 8: Strings and Things

Intro to Computer Science II

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Bellwork Strings 11/25/13

B.V. Patel Institute of BMC & IT, UTU 2014

CS Week 5. Jim Williams, PhD

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

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

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington


Eng. Mohammed Abdualal

Lecture Notes for CS 150 Fall 2009; Version 0.5

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

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Welcome to the Using Objects lab!

Full file at

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

Midterms Save the Dates!

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

Strings, characters and character literals

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

Language Reference Manual

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

CS111: PROGRAMMING LANGUAGE II

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

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Java Strings Java, winter semester

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

Quiz 1. In class on Tuesday (April 14) Review learning goals (from lecture) Review lectures 1 4, PSA 1 You should be able to:

Eng. Mohammed Abdualal

Week 6: Review. Java is Case Sensitive

Creating Strings. String Length

Variables. Store information needed by the program

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

Class. Chapter 6: Data Abstraction. Example. Class

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

WARNING for Autumn 2004:

Computer Components. Software{ User Programs. Operating System. Hardware

Basic Computation. Chapter 2

Transcription:

Activity 9: Object-Oriented Internally, the library class java.lang.string stores an array of characters. It also provides a variety of useful methods for comparing, manipulating, and searching text in general. Content Learning Objectives After completing this activity, students should be able to: Explain how characters and strings are represented in memory. Predict the output of string methods, including boundary cases. Recognize common mistakes when working with the String API. Process Skill Goals During the activity, students should make progress toward: Working with all team members to reach consensus on hard questions. (Teamwork) Copyright 2017 Chris Mayfield and Helen Hu. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Model 1 Character Arrays The primitive type char is used to store a single character, which can be a letter, a number, or a symbol. In contrast, the reference type String encapsulates an array of characters. char letter; letter = 'A'; char[] array; array = new char[] {'c', 'a', 't'; String word; word = "dog"; Questions (15 min) Start time: 1. How is the syntax of character literals and string literals different? 2. What is the index of 'd' in the string above? What is the index of 'g'? In general, what is the index of the last character of a string? 3. Based on the diagram, what does it mean for a class to encapsulate data? How do you access data inside of a class? 4. Why can you use the String class in Java programs without having to import it first?

5. What is the value of a char variable? What is the value of an array variable? What is the value of a String variable? 6. Draw a memory diagram for the given code. (List the name of each variable next to a box containing its value.) String str; str = "Hi!"; char let; let = 'X'; int num; num = -1; double foo; foo = num; String hmm; hmm = str; 7. Recall that the == operator compares the value of two variables. What does it mean for two char variables to be ==? What does it mean for two String variables to be ==? 8. How could you determine whether two character arrays have the same contents? In other words, how does the Arrays.equals method work internally?

Model 2 String Methods Method Returns Description charat(int) char Returns the char value at the specified index of this string. indexof(string) int Returns the index within this string of the first occurrence of the specified substring. length() int Returns the length of this string. substring(int, int) String Returns a new string that is a substring of this string (from beginindex to endindex - 1). touppercase() String Returns a copy of this string with all the characters converted to upper case. Each method listed above is non-static. That is, they have an implicit parameter named this that is passed automatically. (Note: There are many other String methods not listed above.) Questions (20 min) Start time: 9. If str contains the string "hello world", then what is the return value of the following method calls? a) str.charat(8) d) str.substring(4, 7) b) str.indexof("wo") e) str.touppercase() c) str.length() 10. Explain what precedes the. (dot) operator in the expressions above. What does it have to do with the keyword this in Model 2? 11. How many arguments does each method call in #9 have? (Hint: None of them have zero.) a) b) d) e) c)

12. To compare strings, you must use either the String.equals or String.compareTo method. Predict the output of the following code. String name1 = "Mark"; String name2 = "Ma" + "rk"; String name3 = "Mary"; // compare name1 and name2 if (name1 == name2) { System.out.println("name1 and name2 are identical"); else { System.out.println("name1 and name2 are NOT identical"); // compare "Mark" and "Mark" if (name1.equals(name2)) { System.out.println("name1 and name2 are equal"); else { System.out.println("name1 and name2 are NOT equal"); // compare "Mark" and "Mary" if (name1.equals(name3)) { System.out.println("name1 and name3 are equal"); else { System.out.println("name1 and name3 are NOT equal"); 13. What is the difference between identical and equal in the previous question? 14. Discuss the stringmatch problem on the next page. What three String methods will you need to solve it? (If you have time during the activity, complete the method.) 15. Discuss the stringyak problem on the next page. What two String methods will you need to solve it? (If you have time during the activity, complete the method.)

[CodingBat] Given two strings, return the number of positions where they contain the same substring of length two. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. public int stringmatch(string a, String b) { [CodingBat] Suppose the string "yak" is unlucky. Given a string, return a version where all the "yak" are removed, but the 'a' can be any character. The "yak" strings will not overlap. stringyak("yakpak") "pak" stringyak("pakyak") "pak" stringyak("yak123ya") "123ya" public String stringyak(string str) {

Model 3 Common Mistakes Program A String greeting = "hello world"; greeting.touppercase(); System.out.println(greeting); Program B Scanner in = new Scanner(System.in); String line = in.nextline(); char letter = line.charat(1); System.out.println(letter); Questions (10 min) Start time: 16. Write the output of each program in the space under the table above. What is the logic error you see when you run Program A? 17. In Program A, what is returned by the string method? What happens to the return value? 18. Describe two different ways you can fix the logic error in #17. 19. In what cases will Program B throw an exception? What is the error message displayed? 20. Describe two different ways you can fix the run-time error in #19.