String Methods: length substring

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

Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Strings CIS 362. What is a string? In Visual Basic fullName.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
I Power Int 2 Computing Software Development High Level Language Constructs.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Logic (continuation) Boolean Logic and Bit Operations.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
CSC 212 Object-Oriented Programming and Java Part 2.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
What is Binary Code? Computers use a special code of their own to express the digital information they process. It's called the binary code because it.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CSII Final Review. How many bits are in a byte? 8.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
Java: Base Types All information has a type or class designation
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Choosing Data Types Database Administration Fundamentals
Relational Operator and Operations
Objects as a programming concept
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Java: Base Types All information has a type or class designation
Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Methods Attributes Method Modifiers ‘static’
Variables and Primative Types
Selenium WebDriver Web Test Tool Training
Creating Your OwnClasses
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
public class BankAccount{
Overloading and Constructors
MSIS 655 Advanced Business Applications Programming
An Introduction to Java – Part II
The Building Blocks Classes: Java class library, over 1,800 classes:
Building Java Programs
C++ Data Types Data Type
Recap Week 2 and 3.
Dr. Sampath Jayarathna Cal Poly Pomona
Chap 2. Identifiers, Keywords, and Types
Just Enough Java 17-May-19.
Presentation transcript:

String Methods: length substring

To use scientific notation, use the letter e followed by a power of 10 To use scientific notation, use the letter e followed by a power of 10. For instance, 3.65e+9 means 3.65 * 109 or 3,650,000,000. 3.65e-9 means 3.65 * 10-9 or .00000000365 A byte is typically 8 bits. A bit is a binary digit, taking a logical value of either "1" or "0" (also referred to as "true" or "false" respectively). Binary digits are a basic unit of information storage.

Strings are not primitive data types Strings are not primitive data types. They are actually a class with methods and constructors.

Examples of length method public int getNameLength(String name) { int nameLength; nameLength = name.length(); return nameLength; } Another way to write the same method: return name.length(); The first example uses a local variable.

Example of length method public void printStudentId(String id) { if (id.length() > 0) System.out.println("Student id: " + id); } else System.out.println("The student has not been assigned an id.");

Example of substring method public String getAreaCode(String phoneNumber) { String areaCode; areaCode = phoneNumber.substring(0, 3); return areaCode; } Another way to write the same method: return phoneNumber.substring(0, 3);

Exercise Add conditional statements to the constructor of Student to print an error message if either the length of the fullName parameter is less than four characters or the length of the studentID parameter is less than three characters. The constructor should still use those parameters to set the name and id fields even if the error message is printed.

Exercise Modify the getLoginName method of student so that it always generates a login name, even if either of the name and id fields is not strictly long enough. For strings shorter than the required length, use the whole string.