Goals Understand how to create and compare Strings.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Java Programming Strings Chapter 7.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Some basic I/O.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
LAB 10.
Computer Programming Lab(4).
Computer Programming Lab(5).
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Java Programming: From the Ground Up
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
EXAM 1 REVIEW. days until the AP Computer Science test.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Java Java Translation Program Structure
Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
AP Java Java’s version of Repeat until.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
(Dreaded) Quiz 2 Next Monday.
CompSci 230 S Programming Techniques
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CSC111 Quick Revision.
Elementary Programming
Strings.
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Goals Understand how to create and compare Strings.
Primitive Types Vs. Reference Types, Strings, Enumerations
Java Methods Making Subprograms.
Starting Out with Java: From Control Structures through Objects
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Goals Understand how to create and compare Strings.
SELECTION STATEMENTS (2)
Java Variables, Types, and Math Getting Started
Java Methods Making Subprograms.
Java so far Week 7.
int [] scores = new int [10];
Truth tables: Ways to organize results of Boolean expressions.
Fundamentals 2.
AP Java Review If else.
More on Classes and Objects
Truth tables: Ways to organize results of Boolean expressions.
Java Fix a program that has if Online time for Monday’s Program
Java Methods Making Subprograms.
int [] scores = new int [10];
Goals Understand how to create and compare Strings.
Introduction to Java Applications
Truth tables: Ways to organize results of Boolean expressions.
AP Java String Review Learning Objectives
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Java 1/31/2017 Back to Objects.
Building Java Programs
Random Numbers while loop
Presentation transcript:

Goals Understand how to create and compare Strings. Strings and Things Goals Understand how to create and compare Strings.

Learning Strategy Note taking During the discussion, create an outline for today’s ideas Break it down by command Include an example Jot down questions in the side margin.

Variables in Programs Declare the variables inside the main subroutine. You can also declare variables where you need them. (Declare a looping variable at the loop) int numberOfStudents; String name; double x,y; boolean isFinished;

Use input.nextDouble(); for getting a double value Simple input and math import java.util.Scanner; // program uses class Scanner public class Addition { // main method begins execution of Java application public static void main( String args[] ) // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int number1; // first number to add int number2, sum; // second number to add String name; System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user System.out.print("Enter your name "); name = input.nextLine(); sum = number1 + number2; // add numbers System.out.printf( "Sum is %d\n", sum ); // display sum System.out.println("Thanks " + name);// Note: Uses the + for concatenating } // end method main } // end class Addition Use input.nextDouble(); for getting a double value

String Objects Unlike int, boolean and double, String is a class, and when you declare a String variable, you are declaring an object. Strings are constant, their values cannot be changed after they are created. Declarations String name = “Smith”; name is a pointer to an object that holds the name “Smith” and different methods. String river = “Mississippi”; river is a pointer to an object that holds the name “Missippi” and different methods. String name2; name2 is a pointer to an uninitialized object, to SOP is to crash. String name3 = null; name3 is a pointer to null. To SOP is to show NULL name3 = “Bob”; Moves the name3 to point to a String object with the name “Bob”.

We’ll take a little closer look at each of these. Some String Methods We’ll take a little closer look at each of these. .length(); Returns the length (number of characters) of the string, int size = name.length(); .equals(); // Inherited (extended) from the Object class if (name.equals(name2)) .compareTo();//Inherited (implemented) from Comparable if (name.compareTo(name2) < 0) .substring(); Returns a part of a string. name2 = name1.substring(3); name3 = name1.substring(2,2); .indexOf() name.indexOf(substring, beginIndex) int spot = name.indexOf(“Mr.”); int spot2 = name.indexOf(lastName, 3);

int size = name.length(); Returns the length of the strength. String name = “Smith”; name is a pointer to an object that holds the name “Smith” and different methods. Example int ans = name.length();

if name.equals(name2) Returns a true if name equals name2 String name = “Smith”; name is a pointer to an object that holds the name “Smith” and different methods. String name2 = “Jones”; if (name.equals(name2)) System.out.println(“The names are the sames”); What about… if (name == name2) Since name and name2 are both objects, this compares the addresses of the objects, not the values of the objects.

if name.compareTo(name2) This compares the values of the name object with the value of the name2 object with the following values returned. If name comes before name2, a negative value is returned. If name comes after name2, a positive value is returned. If name and name2 are an exact match, it returns 0.

Review What is a String? Name three String methods? What is different between null and nothing? How do you compare strings? String: a class for defining String objects. String objects hold words. String methods so far:length(), toUpperCase(), .equals(), .compareTo() Compare strings using the .equals) and the toUpper() methods.

Note: The first character is at position 0. name.substring(n) Copies from the nth position to the end. String name = “West Salem Titans”; String name2 = name.substring(5); Sets name2 to “Salem Titans”; String name3 = name.substring(n, m); Copies from the nth to the (m-1)th positions. name3 = name.substring(5, 10); Sets name3 to “Salem”

name.indexOf(substring, beginIndex) int ans = name.indexOf(substring); Returns the first index (position) of substring in the string. int ans = name.indexOf(substring, beginIndex); Returns the first index of substring in the string, starting at the beginIndex String name = “West Salem Titans”; int pos = name.indexOf(“e”); Sets pos to 1, the index of the first occurrence of “e”

public class FirstString { public static void main (String[] args) String mySentence = "this is a trial"; String anyString = " "; System.out.println(mySentence); System.out.println(anyString); mySentence +="Java is great."; anyString+= " West Salem High School"; mySentence = 6 + 6 + mySentence; mySentence = mySentence + 6 + 6;

if (mySentence.equals(anyString)) // mySentence.equalsIgnoreCase() for not case sensitive System.out.println("The same."); else System.out.println("Different."); if (mySentence.compareTo(anyString)<0) System.out.println("First less"); else if (mySentence.compareTo(anyString)==0) System.out.println("Equal"); System.out.println("First is more."); for (int pos = 0; pos<mySentence.length(); pos++) System.out.println(mySentence.substring(pos, pos+1)); System.out.println(mySentence); System.out.println(mySentence.substring(15)); System.out.println("s occurs in the #" + mySentence.indexOf('s') + " position");

Strings review Strings are Objects (Variables with methods) Use a capital ‘S’ when declaring as a String. Some common String methods.

Review Write a summary of today’s topics What have you learned? New methods What makes sense What doesn’t make sense Questions

String Program Options Complete any Two of the following. String Program Options Find last name Input first and last name into one String Output only the last name. Push: Support middle names Write a mad-lib program Have the user enter at least 4 pieces of information. Noun, Adverb (honestly), Adjective (messy), Verb, Geographical location, … The computer will generate the mad-lib English to Pig-Latin translation program. The rules of Pig Latin are If the word begins with a consonant -- such as ``string,'' ``Latin'' -- divide the word at the first vowel, swapping the front and back halves and append ``ay'' to the word -- ``ingstray,'' ``atinLay.'' If the word begins with a vowel -- such as ``am,'' ``are,'' ``i'' -- append ``yay'' to the word -- ``amyay,'' ``areyay,'' ``iyay.'' If the word has no vowels (other than 'y') -- such as ``my,'' ``thy'' -- append ``yay'' to it -- ``myyay,'' ``thyyay.'' Level 1, Translate a single word Level 2: Translate a sentence. Input a word, output whether or not it is a palindrome.