Activity 9: Object-Oriented

Similar documents
Activity 9: Object-Oriented

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

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

STUDENT LESSON A10 The String Class

Activity 3: Data Types

Activity 1: Introduction

Strings. Strings and their methods. 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

Activity 1: Introduction

Programming with Java

Activity 4: Boolean Logic

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

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

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

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

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

TEST (MODULE:- 1 and 2)

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

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

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

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://

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

Question: Total Points: Score:

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CSC 222: Object-Oriented Programming. Spring 2017

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

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

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:

Midterms Save the Dates!

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

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

CSC 222: Object-Oriented Programming. Spring 2013

Using Java Classes Fall 2018 Margaret Reid-Miller

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

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

Chapter 8: Strings and Things

Class. Chapter 6: Data Abstraction. Example. Class

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

Voice Application Specification. SBString

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

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

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

Welcome to the Using Objects lab!

Introduction to Programming Using Java (98-388)

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

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

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

Lecture Notes for CS 150 Fall 2009; Version 0.5

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

Computational Expression

More non-primitive types Lesson 06

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Strings! Today! CSE String Methods!

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

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

Full file at

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

CS111: PROGRAMMING LANGUAGE II

Utilities (Part 2) Implementing static features

H212 Introduction to Software Systems Honors

Computer Science 145 Midterm 1 Fall 2016

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Strings, characters and character literals

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

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

Eng. Mohammed Abdualal

Activity 11: Designing Classes

Welcome to the Using Objects lab!

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

Chief Reader Report on Student Responses:

Intro to Computer Science II

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

Bellwork Strings 11/25/13

Midterms Save the Dates!

Variables. Store information needed by the program

Full file at

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

In Java there are three types of data values:

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

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

Review: Python Transi,on Warning

Language Reference Manual

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

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

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


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

Eng. Mohammed Abdualal

More on variables and methods

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

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

Creating Strings. String Length

9 Working with the Java Class Library

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

Basic Computation. Chapter 2

Week 6: Review. Java is Case Sensitive

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 (excluding 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 an object to encapsulate data? How do you access the char[] inside of a String object? 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? To call a static method, you write ClassName.methodName, for example: Math.abs(-5) To call a non-static method, you write objectname.methodname, for example: str.charat(8) Methods can be designed either way. Most String methods are non-static, because that makes the code easier to read. From the charat method s point of view, str is this object. Static (str passed explicitly) String.charAt(str, 8) // wrong Non-Static (str passed implicitly) str.charat(8)

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 = new String("Mark"); String name2 = new String("Mark"); // 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"); 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 static 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("pikyik") "pik" stringyak("yak123ya") "123ya" public static 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. 21. Explain why these errors occur based on what you learned in Model 2 and Model 1.