What methods does the String class provide for ignoring case sensitive situations?

Similar documents
Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Lesson 7 Part 2 Flags

What two elements are usually present for calculating a total of a series of numbers?

Oct Decision Structures cont d

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

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

Basic Computation. Chapter 2

Basic Computation. Chapter 2

Logic & program control part 2: Simple selection structures

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Flow of Control. Chapter 3

Flow of Control. Chapter 3

CT 229 Fundamentals of Java Syntax

Topics. Chapter 5. Equality Operators

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

Conditional Programming

Chapter 3 Selection Statements

Flow of Control. Chapter

Chapter 4: Control Structures I

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Full file at

Chapter 5 Lab Methods

CP122 Computer Science I. Chapter 2: Data Types, Variables, and I/O

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 02: Using Data

Flow of Control. Chapter 3. Chapter 3 1

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

Chapter 3: Decision Structures

Chapter 2 ELEMENTARY PROGRAMMING

Programming with Java

Chapter 3: Decision Structures

St. Edmund Preparatory High School Brooklyn, NY

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Chapter 3: Decision Structures

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Full file at

Introduction to Computer Programming

Full file at

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

COMP 202 Java in one week

Full file at

Chapter 2: Using Data

MODULE 02: BASIC COMPUTATION IN JAVA

Software Design & Programming I

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Section 2.2 Your First Program in Java: Printing a Line of Text

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

LAB 12: ARRAYS (ONE DIMINSION)

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CSIS 10A Assignment 3 Due: 2/21 at midnight

Introduction to Computer Science Unit 2. Notes

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Chapter 2 Primitive Data Types and Operations

Task #1 The if Statement, Comparing Strings, and Flags

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Midterm Examination (MTA)

2.8. Decision Making: Equality and Relational Operators

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

COMP 202. Java in one week

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

CS 106 Introduction to Computer Science I

Conditional Execution

Java I/O and Control Structures

A+ Computer Science -

Java I/O and Control Structures Algorithms in everyday life

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Chapter 5 Lab Methods

2.2 - Making Decisions

Topics. The Development Process

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Visual C# Instructor s Manual Table of Contents

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

More about JOptionPane Dialog Boxes

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Chapter 2. Elementary Programming

Lesson 3: Accepting User Input and Using Different Methods for Output

Condensed Java. 12-Oct-15

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

Basics of Java Programming

Array. Prepared By - Rifat Shahriyar

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

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

4 WORKING WITH DATA TYPES AND OPERATIONS

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

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

What did we talk about last time? Examples switch statements

Decisions in Java IF Statements

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Transcription:

Nov. 20 What methods does the String class provide for ignoring case sensitive situations? What is a local variable? What is the span of a local variable? How many operands does a conditional operator take? What is it referred to as? What is its format? Ignoring Case in String Comparisons The equals and compareto methods perform case sensitive comparisons, which means that uppercase letters are not considered the same as their lowercase counterparts. In other words, "A" is not the same as "a". This can obviously lead to problems when you want to perform case insensitive comparisons. The String class provides the equalsignorecase and comparetoignorecase methods. These methods work like the equals and compareto methods, except the case of the characters in the strings is ignored. For example, the program in Code Listing 3-11 asks the user to enter the "secret word," which is similar to a password. The secret word is "PROSPERO", and the program performs a case-insensitive string comparison to determine whether the user has entered it. (SecretWord.java) import java.util.scanner; // Needed for the Scanner class /** * Thisprogram demonstrates a case insensitive string comparison. */ public class SecretWord public static void main (String [] args) String input; // To hold the user's input // Create a scanner object for keyboard input. Scanner keyboard = new Scanner(System.in);

// Prompt the user to enter the secret word. System.out.print("Enter the secret word: "); input = keyboard.nextline(); // Determine whether the user entered the secret word. if (input.equalsignorecase("prospero")) System.out.println("Congratulations! You Know the " + "secret word!"); System.out.println("Sorry, that is NOT the " + " secret word!"); PROGRAM OUTPUT Enter the secret word: FERDINAND Sorry, that is NOT the secret word! Enter the secret word: Prospero Congratulations! You Know the secret word! The comparetoignorecase method works exactly like the compareto method, except the case of the characters in the strings being compared is ignored. ~ Checkpoint 3,20 Assume the variable name references a String object. Write an if statement that displays "Do I know you?" if the String object contains "Timothy". 3,21 Assume the variables name1 and name2 reference two different String objects, containing different strings. Write code that displays the strings referenced by these variables in alphabetical order. 3,22 Modify the statement you wrote in response to Checkpoint 3.20 so it performs a case insensitive comparison. More about Variable Declaration and Scope

- CONCEPT: The scope of a variable is limited to the block in which it is declared. Recall from Chapter 2 that a local variable is a variable that is declared inside a method. Java allows you to create local variables just about anywhere in a method. For example, look at the program in Code Listing 3-12. The main method declares two String reference variables: firstname and lastname. Notice that the declarations of these variables appear near the code that first uses the variables. (VariableScope.java) import javax.swing.joptionpane; // Needed for JOptionPane /** * This program demonstrates how variable may be declared * in various locations throughout a program. */ public class VariableScope public static void main(string[] args) // Get the user's first name String firstname; firstname = JOptionPane.showInputDialog("Enter your " + "first name."); // Get the user' last name String lastname; lastname = JOptionPane.showInputDialog("Enter your " + "last name."); JOptionPane.showMessageDialog(null, "Hello " + firstname + " " + lastname); System.exit(0);

Although it is a common practice to declare all of a method's local variables at the beginning of the method, it is possible to declare them at later points. Sometimes programmers declare certain variables near the part of the program where they are used in order to make their purpose more evident. Recall from Chapter 2 that a variable's scope is the part of the program where the variable's name may be used. A local variable's scope always starts at the variable's declaration, and ends at the closing brace of the block of code in which it is declared. In Code Listing 3-12, the firstname variable is visible only to the code in lines 13 through 24. The lastname variable is visible only to the code in lines 18 through 24. NOTE: When a program is running and it enters the section of code that constitutes a variable's scope, it is said that the variable "comes into scope." This simply means the variable is now visible and the program may reference it. Likewise, when a variable "leaves scope" it may not be used. The Conditional Operator (Optional) CONCEPT: You can use the conditional operator to create short expressions that work like if- statements. The conditional operator is powerful and unique. Because it takes three operands, it is considered a ternary operator. The conditional operator provides a shorthand method of expressing a simple if- statement. The operator consists of the question mark (?) and the colon (:). You use the operator to write a conditional expression, in the following format: BooleanExpression? Value1: Value2; The BooleanExpression is like the boolean expression in the parentheses of an if statement. If the BooleanExpression is true, then the value of the conditional expression is Value1. Otherwise, the value of the conditional expression is Value2. Here is an example of a statement using the conditional operator: y = x < 0? 10: 20; This preceding statement performs the same operation as the following if- statement: if (x < 0) y = 10; y = 20;

The conditional operator gives you the ability to pack decision-making power into a concise line of code. With a little imagination it can be applied to many other programming problems. For instance, consider the following statement: System.out.println("Your grade is: " + (score < 60? "Fail." : "Pass.")); Converted to an if- statement, it would be written as follows: if (score < 60) System.out.println("Your grade is: Fail."); System.out.println("Your grade is: Pass."); NOTE: The parentheses are placed around the conditional expression because the + operator has higher precedence than the?: operator. Without the parentheses, the + operator would concatenate the value in score with the string "Your grade is: ". Checkpoint 3.23 Rewrite the following if- statements as statements that use the conditional operator. a) if (x > y) z = 1; z = 20; b) if (temp> 45) population = base * 10; population = base * 2; c) if (hours > 40) wages *= 1.5; wages *= 1; d) if (result >= 0) System.out.println("The result is positive.)"; System.out.println("The result is negative.");