Chapter 2: Java Fundamentals Type conversion,String.

Slides:



Advertisements
Similar presentations
COMP 14 Introduction to Programming
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 2: Basic Elements of C++
Chapter 3: Introduction to Objects and Input/Output.
Review Session Ryan Ly (slides by Jai Madhok)
Chapter 2-3: Basic Elements of Java. Chapter Objectives Discover how to use arithmetic operators. Examine how a program evaluates arithmetic expressions.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
COMP 110 Introduction to Programming Mr. Joshua Stough August 29, 2007 Monday/Wednesday/Friday 3:00-4:15 Gardner Hall 307.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Review Session Jai Madhok
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Lecture 2 Objectives Learn about objects and reference variables.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
COMP 14 Introduction to Programming Mr. Joshua Stough January 24, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
28 Formatted Output.
Java Fundamentals 4.
Java Programming Lecture 2
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 6 GC 101 Strings.
TMF1414 Introduction to Programming
Java Programming: From Problem Analysis to Program Design, 4e
OUTPUT STATEMENTS GC 201.
IDENTIFIERS CSC 111.
Chapter 3: Introduction to Objects and Input/Output
Java Fundamentals 3 Type conversion ,String.
Chapter 2: Basic Elements of Java
Chapter 3: Introduction to Objects and Input/Output
Chapter 2: Java Fundamentals
Introduction to Java Applications
Java Fundamentals 3 Type conversion ,String.
Chapter 2 Primitive Data Types and Operations
CS 1054 Introduction to Programming in Java
Presentation transcript:

Chapter 2: Java Fundamentals Type conversion,String

Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly Used Escape Sequences

Variables – Cont. Float The default type of floating point numbers is double. The declaration : float rate = 15.5f ; without the f, the compiler will generate an error 3

Variables – Cont. Char When using the char data type, you enclose each character represented within single quotations marks. Ex: char c = ‘A’ char x = ‘&’ char space = ‘ ‘ Each character is represented by a value (‘A’ is represented by the value 65) 4

5

Type Conversion (Casting) Used: to change one data type to another. to avoid implicit type coercion. Syntax: (dataTypeName) expression Expression evaluated first, then the value is converted to dataTypeName Java Programming: From Problem Analysis to Program Design, Second Edition 6

Type Conversion (Casting) Examples: 1. (int)( ) = (int)(7.9) + (int)(6.7) = (double)(17) = (double)(8+3) = (double)11 = (double)(7) /2 = 7.0/2 = (double)(7/2) = (int)(7.8+(double)(15)/2) = (int)15.3 =15 Java Programming: From Problem Analysis to Program Design, Second Edition 7

Type Conversion (Casting) 8. (int)(‘A’) = (int)(‘8’) = (char)(65) = ‘A’ 11. (char) (56) = ‘8’ Java Programming: From Problem Analysis to Program Design, Second Edition 8

The class String Contains operations to manipulate strings. String: Sequence of zero or more characters. Enclosed in double quotation marks. Is processed as a single unit. Null or empty strings have no characters. “ “ Every character has a relative position, the first character is in position 0. Java Programming: From Problem Analysis to Program Design, Second Edition 9

10 The class String Java system automatically makes the class String available (i.e no need to import this class ) Example : Consider the following declaration : String sentence ; sentence = “programming with java”

The class String Length of the string is the number of characters in it. When determining the length of a string, blanks count. Example : “ “  has length = 0 “abc”  has length = 3, position of a = 0,b= 1, c= 2 “a boy”  has length = 5 Java Programming: From Problem Analysis to Program Design, Second Edition 11

Java Programming: From Problem Analysis to Program Design, Second Edition12 Some Commonly Used String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition13 Some Commonly Used String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition14 Some Commonly Used String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition15 Some Commonly Used String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition16 Examples on string methods String s1, s2, s3 ; s1 = “abcdefeg” ; System.out.println( s1.length() ); // 8 System.out.println(s1.charAt(3)); //d System.out.println(s1.indexOf(‘e’)); //4 System.out.println(s1.indexOf(“cd”)); //2 System.out.println(s1.toUpperCase()); //ABCDEFEG

Java Programming: From Problem Analysis to Program Design, Second Edition17 Examples on string methods System.out.println(s1.substring(1, 4)); //bcd System.out.println(s1 + “xyz”); // abcdefegxyz System.out.println( s1.replace(‘d’,’D’)); // abcDefeg System.out.println(s1.charAt(4) ); // e System.out.println(s1.indexOf(‘b’)); // 1 System.out.println(s1.indexOf(‘e’,5)); // 6

Java Programming: From Problem Analysis to Program Design, Second Edition 18 Input Reading a Single Character if ch is a char variable. To input A into ch, you can use the following statement: ch = console.next().charAt(0);

Java Programming: From Problem Analysis to Program Design, Second Edition19 Parsing Numeric Strings  Integer, Float, and Double are classes designed to convert a numeric string into a number.  These classes are called wrapper classes.  parseInt is a method of the class Integer, which converts a numeric integer string into a value of the type int.  parseFloat is a method of the class Float and is used to convert a numeric decimal string into an equivalent value of the type float.  parseDouble is a method of the class Double, which is used to convert a numeric decimal string into an equivalent value of the type double.

Java Programming: From Problem Analysis to Program Design, Second Edition20 Parsing Numeric Strings  A string consisting of only integers or decimal numbers is called a numeric string.  To convert a string consisting of an integer to a value of the type int, we use the following expression: Integer.parseInt(strExpression) Example: Integer.parseInt("6723") = 6723 Integer.parseInt("-823") = -823

Java Programming: From Problem Analysis to Program Design, Second Edition21 Parsing Numeric Strings  To convert a string consisting of a decimal number to a value of the type float, we use the following expression: Float.parseFloat(strExpression) Example: Float.parseFloat("34.56") = Float.parseFloat(" ") =  To convert a string consisting of a decimal number to a value of the type double, we use the following expression: Double.parseDouble(strExpression) Example: Double.parseDouble("345.78") = Double.parseDouble(" ") =

Java Programming: From Problem Analysis to Program Design, Second Edition22 Formatting Output with printf The syntax to use the method printf to produce output on the standard output device is: System.out.printf(formatString); or System.out.printf(formatString,argumentList) ; formatString is a string specifying the format of the output and argumentList is a list of arguments. argumentList is a list of arguments that consists of constant values, variables, or expressions. If there is more than one argument in argumentList, the arguments are separated with commas.

Java Programming: From Problem Analysis to Program Design, Second Edition23 Formatting Output with printf System.out.printf("Hello there!"); Consists of only the format string and the statement: System.out.printf("There are %.2f inches in %d centimeters.%n", centimeters / 2.54, centimeters); Consists of both the format string and argumentList. %.2f and %d are called format specifiers. By default, there is a one-to-one correspondence between format specifiers and the arguments in argumentList. The first format specifier, %.2f, is matched with the first argument, which is the expression centimeters / 2.54.

Java Programming: From Problem Analysis to Program Design, Second Edition24 Formatting Output with printf The second format specifier, %d, is matched with the second argument, which is centimeters. The format specifier %n positions the insertion point at the beginning of the next line. If centimeters = 150  150/2.54 = The o/p would be : There are inches in 150 centimeters The output of a printf statement is right-justified by default. To force the output to be left-justified, negative column widths may be used.

Java Programming: From Problem Analysis to Program Design, Second Edition25 Example1 public class Example3_6 { public static void main (String[] args) { int num = 763; double x = ; String str = "Java Program."; System.out.println(" "); System.out.printf ( "%5d%7.2f%15s%n", num, x, str); System.out.printf ("%15s%6d%9.2f %n", str, num, x); }

Java Programming: From Problem Analysis to Program Design, Second Edition26 Example1 Sample run : Java Program. Java Program

Java Programming: From Problem Analysis to Program Design, Second Edition27 Example2 public class Example3_7 { public static void main (String[] args) { int num = 763; double x = ; String str = "Java Program."; System.out.println(" "); System.out.printf("%-5d%-7.2f%-15s ***%n", num, x, str); System.out.printf("%-15s%-6d%- 9.2f ***%n", str, num, x); }

Java Programming: From Problem Analysis to Program Design, Second Edition28 Example2 Sample Run : Java Program. *** Java Program ***

Java Programming: From Problem Analysis to Program Design, Second Edition29 Formatting Output with printf

Java Programming: From Problem Analysis to Program Design, Second Edition 30 Commonly Used Escape Sequences