Introduction to Computers and Programming Lecture 7:

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Class 7.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Chapter 2: Elementary Programming Shahriar Hossain.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Introduction to Java Programming, 4E Y. Daniel Liang.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
String Escape Sequences
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
The character data type char
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
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.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
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.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to programming in java Lecture 21 Arrays – Part 1.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
EPSII 59:006 Spring 2004.
Multiple variables can be created in one declaration
Computing with C# and the .NET Framework
Chapter 2 Basic Computation
Chapter 2.
IDENTIFIERS CSC 111.
Introduction to C++ Programming
Introduction to Java, and DrJava part 1
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

Introduction to Computers and Programming Lecture 7:

Road map char data type

Review True or False: An int variable can contain any integer. When must you use an if / else if / else statement instead of using a switch statement? When is it preferable to use a switch statement? What is a magic number? How should you deal with a magic number? Why? Explain what the exclusive or (^) operator tests. (exp a) ^ (exp b) Define the purpose of each part of this expression: (part a) ? (part b) : (part c)

Review continued What is the output of the following code fragment? int a = 100, b = 50; if ((a == 60) && (b <= 100)) System.out.println ("Yes"); What is the output of the following code fragment? int a = 100, b = 50; if ((a == 60) || (b <= 100)) System.out.println ("Yes");

Review continued What is the output of this switch statement? int a = 90; switch (a) { case 80: System.out.println (80); case 90: System.out.println (90); case 100: System.out.println (100); }

char data type

Java allows us to store "single" character values. char character = 'a'; // not the same as "a"; char character = '7'; char newline = '\n'; char tab = '\t'; char space = ' '; The characters are actually stored as integers (ascii values). See Note: chars use single quotes. We have seen that Strings use double quotes.

ASCII Table Source: Liang

char arithmetic Given: char letter = 'a'; The following code: letter = (char) (letter + 1); Would result in letter storing a 'b' character. If we add or subtract two char values, the result is an int value. –For example 'c' – 'a' would result in the value 2.

Casting between char and int values A char uses 16 bits of memory. –You can implicitly cast a char to an int char c = 'a'; int i = c; –You must explicitly cast an int to a char int i = 65; char = (char) i; Note: Even though chars are equal to shorts in the amount of memory they use, they do not hold the same values (shorts can hold negative numbers).

Reading char values from the user You should use charAt(0) to parse the character from user input. For example: char c; String cAsString; cAsString = JOptionPane.showInputDialog (null, "Enter a character"); c = cAsString.charAt(0); Will grab the first character from the user's input.

Warning about numeric digit characters The value of a the single digit numeric characters are not equivalent to the values themselves. In fact the ASCII value of '0' is 48, '1' is 49, …, '9' is 57. How do you think we could convert a numeric char to an int ?

Unicode In Java, there are many more characters available then in the basic ascii table. The ascii table only has 128 characters in it. Java uses 2 bytes to store characters which allows it to hold unique characters. Java can store any Unicode (see: unicode.org) character in a char variable.unicode.org That means you can print any character from any language on any platform. To print a Unicode character, use '\uxxxx' where xxxx is a hexadecimal representation of the Unicode for the desired character.