char ch = astring; //where astring is a String..illegal char ch = A ; //illegal

Similar documents
CS 106A, Lecture 9 Problem-Solving with Strings

CS 106A, Lecture 9 Problem-Solving with Strings

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

CS-201 Introduction to Programming with Java

AP CS Fall Semester Final

DATA TYPES AND EXPRESSIONS

Eng. Mohammed Abdualal

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Datatypes, Variables, and Operations

Oct Decision Structures cont d

Module 4: Characters, Strings, and Mathematical Functions

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CIS October 19, 2017

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Zheng-Liang Lu Java Programming 45 / 79

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Branching and Boolean Expressions

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Building Java Programs

Full file at

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Branching and Boolean Expressions

Chapter 4. Mathematical Functions, Characters, and Strings

Chapter 2 ELEMENTARY PROGRAMMING

6 Character Classification and Utilities Module (chars.hhf)

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Notes from the Boards Set BN19 Page

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

String is a collection of alphanumeric & special characters and escape sequences enclosed in (Double quotes).

AP Computer Science A Unit 2. Exercises

Chapter 7: Iterations

(Refer Slide Time: 00:23)

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Programming with Java

MODULE 02: BASIC COMPUTATION IN JAVA

c) And last but not least, there are javadoc comments. See Weiss.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

COMPUTER APPLICATIONS

Place your name tag here

Topics. Chapter 5. Equality Operators

B.V. Patel Institute of BMC & IT, UTU 2014

Java Notes. 10th ICSE. Saravanan Ganesh

ICSE Class 10 Computer Applications ( Java ) 2010 Solved Question...

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

AP Computer Science A

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

The C++ Language. Arizona State University 1

CIS October 16, 2018

Chapter 2 Primitive Data Types and Operations

More on variables and methods

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Chapter Goals. Chapter 5 - Iteration. Calculating the Growth of an Investment

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Exercise 1.1 Hello world

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Chapter 2: Using Data

Chapter 2: Using Data

Question: Total Points: Score:

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Introduction to Programming, Aug-Dec 2006

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

An overview of Java, Data types and variables

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Java characters Lecture 8

Question: Total Points: Score:

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

A first look at string processing. Python

Java Programming Language. 0 A history

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 2 Elementary Programming

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

JAVA Programming Fundamentals

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Basic Computation. Chapter 2

A variable is a name for a location in memory A variable must be declared

CMPT 125: Lecture 3 Data and Expressions

Chapter 4: Computer Codes. In this chapter you will learn about:

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Chapter 2 Primitive Data Types and Operations. Objectives

Operators in java Operator operands.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Text. Text Actions. String Contains

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Following is a list of some of the methods and techniques we encountered in Lesson 3:,,,,, and escape sequences.

Tokens, Expressions and Control Structures

Transcription:

13-1 Character type and types can t be stored into each other. The following lines of code are : char ch = astring; //where astring is a String..illegal char ch = A ; //illegal String x = xchar; //where xchar is a char..illegal String x = X ; //illegal Strangely enough the following legal: int x =1; char ch = A ; //ASCII code for A is 65 (more on ASCII below) int y = x + ch; //This legal! System.out.println(y); // int z = ch; //This is legal! Storing an type into a is illegal. char ch = j; // assuming j is an int Why is this illegal? It s because can take on Unicode values from 0 65536 (two bytes)while types can go over 2 billion. The compiler justly complains about possible loss of precision and refuses to do it. Use casting as a way around this. char ch = (char)j; // assuming j is an int and less than 65,536 Why does the code in middle section above work? It s because characters are just numbers. For example, capital A is stored as a 65. That s why we got 66 above. All characters (letters, numbers, symbols, etc) are stored as numbers. Some ASCII codes that you are: 0 48 A 65 a 97 1 49 B 66 b 98 2 50 C 67 c 99.................. 8 56 Y 89 y 121 9 57 Z 90 z 122 For more on ASCII codes, see Appendix D.

Let s look back at the top section of this page. What do you do if you absolutely have to convert a into a character or vice versa? a. String s = W ; char a = s.charat(0); //a now equals W 13-2 char a = X ; String s = + a; //concatenation of a string and a character is permit- //ed. The result is a String. The trick is to make the //String we are concatenating an empty String ( ). A way to convert capital-letter characters into small-letter characters is to add 32. Look in the chart above capital A is 65 small a is 97.a difference of 32. char bigletter = H ; char smallletter = (char)(bigletter + 32); //(bigletter + 32) is an int that must be //cast see # 3 on previous page. System.out.println(smallLetter); // We can ask the following questions of a character (answers are always or ), c. are you a digit? System.out.println( Character.isDigit(ch) ); // System.out.println( Character.isDigit(ch) ); // d. are you a letter? System.out.println( Character.isLetter(ch) ); // System.out.println(Character.isLetter(ch) ); // e. are you a letter or a digit? System.out.println( Character.isLetterOrDigit(ch) ); // System.out.println( Character.isLetterOrDigit(ch) ); // f. are you whitespace?.(new line character, space and tabs are whitespace)

char ch = ; System.out.println( Character.isWhitespace(ch) ); // 13-3 char ch = p ; System.out.println( Character.isWhitespace(ch) ); // g. are you lowercase? System.out.println( Character.isLowerCase(ch) ); // char ch = A ; System.out.println(Character.isLowerCase(ch) ); // h. are you uppercase? System.out.println( Character.isUpperCase(ch) ); // char ch = A ; System.out.println( Character.isUpperCase(ch) ); // We can convert a character to upper case as follows: char ch = d ; char nn = Character.toUpperCase(ch); System.out.println(nn); // We can convert a character to lower case as follows: char ch = F ; char nn = Character.toLowerCase(ch); System.out.println(nn); //

13-4 1. What is the ASCII code for A? 2. What is the ASCII code for Z? 3. What is the ASCII code for a? 4. What is the ASCII code for z? 5. How many letters are in the English alphabet? 6. What is the ASCII code for the character 0 (this is the number 0 and not the letter O)? 7. What is the ASCII code for the character 9? 8. What does the following code do? char c; for (int j = 97; j <= 122; j++) { c = (char)(j 32); System.out.print(c); 9. What does the following code do? String s = Alfred E. Neuman ; char ch; for (int x = 0; x < s.length( ); x++) { ch = s.charat(x); if ( (ch <= 90) && (ch>=65) ) ch = (char)(ch + 32); System.out.print(ch); 10. Write code that will convert into a. 11. Write code that will convert into a character. ( consists of just one letter.) 12. Is this legal? char ch = V ; String sd = ch;

13-5 13. Is this legal? char ch = V ; char x = (char)(ch + 56); 14. Is this legal? char aa = X ; 15. char k = B ; System.out.println(k + 3); //What s printed? 16. char k = B ; System.out.println( (char)(k + 3) ); //What s printed? 17. Write code that will insure that an uppercase version of is stored in. 18. Write code that will insure that a lowercase version of is stored in. 19. If you have a character called, what could you do to determine if it s a digit? 20. If you have a character called, what could you do to determine if it s a letter? 21. If you have a character called, what could you do to determine if it s an uppercase character? 22. If you have a character called, what could you do to determine if it s either a letter or a digit? 23. If you have a character called, what could you do to determine if it s a lowercase character? 24. Describe what the following code does. for(int j = 0; j <= 127; j++) { char ch = (char)j; if (Character.isWhitespace(ch) ) System.out.println(j);