© 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character.

Slides:



Advertisements
Similar presentations
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 17.
Advertisements

©2004 Brooks/Cole Chapter 7 Strings and Characters.
THE SOLAR SYSTEM By: Natalie Natale LRC 320 Final Project.
Chapter 2 Review Questions
By Keith Lehman  Mercury has no moons.  Mercury is 1/3 the size of Earth.  On Mercury, a day is 59 Earth days long.  Mercury is a rock planet.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Planet Order Create an easy way to remember the names of the planets in order from the Sun. Make up a silly sentence. Each word in the sentence should.
By: ? & ? Grade 6-1 The Planet’s chart PlanetPeriod of rotation Orbiting the sun Mercury59 days88 days Venus243 days225 days Earth23.9 hours days.
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
The Solar System. Mercury Mercury is the closest planet to the sun. Mercury is the closest planet to the sun. Mercury is the eighth largest planet. Mercury.
Planets of the Solar System Our Solar System has nine planets.
Mercury  The closest planet to the sun.  It has no moons.  It has 38% gravity.  It takes 88 days to orbit the sun.
The FRAME Routine Key Topic Main idea is about… So What? (What’s important to understand about this?) Essential details Main idea Essential details Main.
+ The sun is hot. The sun can kill you before you get there.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
Are Amazing Planets By Samy Abdelsalam Mercury Mercury has no moons. Mercury is the first planet from the sun. Mercury is 36 million kilometers) away.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Exploring the Solar System By the end of this session you will: have a better understanding of the sizes of and relationship between units of length have.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
How many moons does each planet have?. Saturn has 60 moons l=en&site=imghp&tbm=isch&source=hp&biw= 1366&bih=665&q=planets+in+the+solar+syste.
The Solar System By Devin Fields Fourth grade Mrs. Krause 2009.
Space and the planets.
Do Now  If you had to define what a “year” is, how would you define it?
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Comparing the Planet’s to the Sun
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
TOURING THE SOLAR SYSTEM Use the Internet to research this project and use the following websites as a place to start:
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
What makes the Universe?. Questions to Consider What is the Solar System? What is the Solar System? What does the Solar System consists of? What does.
ESSENTIAL QUESTION HOW CAN I DETERMINE THE PLACE OF A DIGIT IN A NUMBER? KNOW YOUR PLACE IN SPACE.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
The Planets of Our Solar System Mercury.
CLICK TO CONTINUE CLICK TO CONTINUE. The Sun Inner Solar System Inner Solar System Outer Solar System Outer Solar System.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Topic 2 Elementary Programming
char is for storing single characters
String class.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Chapter 2: Java Fundamentals
CS2011 Introduction to Programming I Strings
Outline Creating Objects The String Class The Random and Math Classes
Review of Java Fundamentals
Strings in Java Strings in Java are also a reference type.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved char and String char is for storing single characters primitive type constants: a printable character enclosed in single quotes ( ‘Y’ ‘y’ ‘#’ ‘7’ ) char widens to int, long, float or double operators: ++ and -- caution: (char)7 is not the same as ‘7’ String is for storing a sequence of characters built-in Java class constants: zero or more characters enclosed in double quotes a String object is ______________ operators: + caution: new not used with constant notation.

© 2006 Pearson Addison-Wesley. All rights reserved Examples variable declarations private char initial, digit; private String name, message, str; initial = ‘D’; digit = ‘0’; digit++; initial = (char)(initial + 3); System.out.println(digit); System.out.println(initial); name = “Riley”; str = “”; message = name + “ assigns programs.”; System.out.println( initial + “. “ + message ); System.out.println( “digit value is “ + digit );

© 2006 Pearson Addison-Wesley. All rights reserved Class Diagram String «constructor» + String( String )... «query» + char charAt( int ) + int length() + int substring(int) + int substring(int, int)... «translate» + String toLowerCase( ) + String toUpperCase( )... «infix operator» + String «constructor» + String( String )... «query» + char charAt( int ) + int length() + int substring(int) + int substring(int, int)... «translate» + String toLowerCase( ) + String toUpperCase( )... «infix operator» +

© 2006 Pearson Addison-Wesley. All rights reserved Examples variable declarations private String cheese, theory; cheese = “limburger”; System.out.println( cheese.substring(3) ); cheese = cheese.substring(6); SYstem.out.println( cheese ); cheese = “gouda”.substring(2); System.out.println( cheese ); theory = “quantum mechanics”; System.out.println( theory.substring(4,10)); System.out.println( theory ); theory = “hadrons”; System.out.println( cheese.substring(3,5) + “ “ + theory.charAt(0) + cheese.substring(4) + ‘t’ );

© 2006 Pearson Addison-Wesley. All rights reserved Differences Between Types Strings and primitive types are largely incompatible... You can add, subtract, multiply or divide most primitives, but not Strings Because they are a reference type, Strings are instantiated via new, but not primitives. String product = "75.2" * "3"; char letter = new char('D'); Even if a String stores a single character it is incompatible with char. char questionMark = "?"; String asterisk = '*'; You easily extract individual characters from Strings, but not primitives char decimalPoint = ("38.25").charAt(2); char nonSymbol = (38.25).charAt(2);

© 2006 Pearson Addison-Wesley. All rights reserved Type Conversion Expressions String  char use charAt char initial = ("7").charAt(0); 1) Instantiate Scanner passing the String to be converted 2) Call next Type () upon this Scanner object. String closeToPi = " "; Scanner converter = new Scanner(closeToPi); double approximationOfPi = converter.nextDouble(); concatenate to the empty string String realValue = ""; String logicalNegative = false + ""; Any primitive  String String  Any primitive excepting char which can be abbreviated... double approximationOfPi =

© 2006 Pearson Addison-Wesley. All rights reserved Enumerated Types An enumerated type is a user-defined type with its own constants. The constants are identifiers. Every enumerated type includes two conversion methods: ordinal() returns an int (0 for first, 1 for second, and so forth) name() returns an String from the identifier enum FlippingCoin {heads, tails}... flipper = FlippingCoin.tails; int flipNum = flipper.ordinal(); String flipName = flipper.name(); // assert: FlippingCoin flipper; flipper = FlippingCoin.tails;

© 2006 Pearson Addison-Wesley. All rights reserved Internal or External Declaration? public class Driver { private enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet; public Driver() {... favoritePlanet = earth;... } public class Driver { private enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet; public Driver() {... favoritePlanet = earth;... } public class Driver { private Planets favoritePlanet; public Driver() {... favoritePlanet = Planets.earth;... } public class Driver { private Planets favoritePlanet; public Driver() {... favoritePlanet = Planets.earth;... } Enumerated types may either be declared within a class or in a separate file...but not local to a method. public enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } public enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto }

© 2006 Pearson Addison-Wesley. All rights reserved Q: Why use enumerated types? public class Driver { private enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet; public Driver() {... favoritePlanet = earth;... } public class Driver { private enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet; public Driver() {... favoritePlanet = earth;... } public class Driver { private int favoritePlanet; private final int mercury = 0; private final int venus = 1; private final int earth = 2; private final int mars = 3; private final int jupiter = 4; private final int saturn = 5; private final int uranus = 6; private final int pluto = 7; public Driver() {... favoritePlanet = earth;... } public class Driver { private int favoritePlanet; private final int mercury = 0; private final int venus = 1; private final int earth = 2; private final int mars = 3; private final int jupiter = 4; private final int saturn = 5; private final int uranus = 6; private final int pluto = 7; public Driver() {... favoritePlanet = earth;... } Using enumerated...Using int... A: Safety favoritePlanet = -33; is permitted using the int version.