(Dreaded) Quiz 2 Next Monday.

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

Java Programming Strings Chapter 7.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
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!");
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
EXAM 1 REVIEW. days until the AP Computer Science test.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
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.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Java for Beginners University Greenwich Computing At School DASCO
Java String Methods - Codehs
Key Words / Reserved Words
CS 160 – Summer 16 Exam 1 Prep.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Strings.
Chapter 2 Elementary Programming
Building Java Programs
String class.
Multiple if-else boolean Data
Programming in Java Text Books :
Quiz Next Monday.
BIT115: Introduction to Programming
Advanced Programming in Java
Classes Variables That Are Not of a Built-in Type Are Objects
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
Goals Understand how to create and compare Strings.
Know for Quiz Everything through Last Week and Lab 7
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
String Methods.
Strings in Java.
Repetition Statements
(Dreaded) Quiz 2 Next Monday.
Presentation transcript:

(Dreaded) Quiz 2 Next Monday

Strings Revisited String Is a Type (Object) in JAVA Automatically imported in java.lang May Initialize or Assign with Double Quoted Values Can Add String to String with ‘+’ Operator CANNOT Compare using Equality Operators (uses .equals() method instead) Methods Available to Access single characters Get length of String Find a character or substring in String Convert to lower or upper case

Objects Objects: Types of Variables Made from Built-In Types Variables Can be Declared as an Object Type Objects Usually Have Methods/Functions Tied to Them Member functions Public functions Interface functions Methods String Is an Example of an Object Type StringVariable.length() Returns the Number of Characters in the String

Methods Also called Procedures, Functions Have Parenthesis (e.g., method() ) May be Passed Arguments, or Parameters Inside of Parenthesis Parameters are values to initialize new variables in method Parameters used to pass information to method

String Methods charAt(int index): return char at named index (starting at 0) equals(String str): return boolean indexOf(char): return int of first occurrence of char (index starts at 0) indexOf(String substr): return int of first occurrence of String substr length(): return int length of String substring(int start, int end): return String between start and end toLowerCase(), toUpperCase(): convert String

String myString = "My Dog Sandy",compareString; System.out.println("The fourth character is: " + myString.charAt(3)); if (myString.equals("my dog sandy")) System.out.println("Strings are equal"); else System.out.println("Strings are not equal"); compareString = myString + myString; System.out.println("Added strings are: " + compareString); Output: The fourth character is: D Strings are not equal Added strings are: My Dog SandyMy Dog Sandy

compareString = myString.toLowerCase(); System.out.println("String is: " + compareString); System.out.println("Char S is at location: " + myString.indexOf('S')); System.out.println("substring og is at location: " + myString.indexOf("og")); System.out.println("Length of string is: " + myString.length()); System.out.println("substring between location 3 and 5 is: " + myString.substring(3,5)); Output: String is: my dog sandy Char S is at location: 7 Substring og is at location: 4 Length of string is: 12 Substring between location 3 and 5 is: Do

import java.util.Scanner; public class MyProgram { public static void main( String [ ] args) Scanner scan = new Scanner(System.in); String lastname; boolean instructor = false; System.out.print(“Enter last name:”); lastname = scan.next(); if(lastname.equals(“hanrath”)) instructor = true; // privileged else instructor = false; // not privileged }

More on Input Input Failure Common *MUST* Be Part of Test Plan Pages 310-313 in Anderson Check Input for Type Desired If not, Notify User, and Get Rid of Line Use scan.hasNextInt(), scan.hasNextDouble(), scan.hasNextFloat()

Input Failure Example garbage = scan.nextLine(); import java.util.*; class InputFailureExample { public static void main(String[] args) int numE; String garbage; Scanner scan = new Scanner(System.in); System.out.print("Enter Integer: "); while (!scan.hasNextInt()) garbage = scan.nextLine(); System.out.println("Please try again."); } numE = scan.nextInt(); System.out.println("You entered: " + numE);

Know for Quiz 2 Everything through Exam 1 Loops: while, for, do-while switch-case Statement String Methods Input Failure Methods