CS-201 Introduction to Programming with Java

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

Chapter 4 Mathematical Functions, Characters, and Strings

Module 4: Characters, Strings, and Mathematical Functions

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

Chapter 2 Elementary Programming

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations

Chapter 3 Mathematical Functions, Strings, and Objects

Chapter 2 Primitive Data Types and Operations. Objectives

Zheng-Liang Lu Java Programming 45 / 79

Basic data types. Building blocks of computation

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Programming with Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Review for Test 1 (Chapter 1-5)

ANSI C Programming Simple Programs

Full file at

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

Important Java terminology

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Java Notes. 10th ICSE. Saravanan Ganesh

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

JAVA Programming Concepts

COMP Primitive and Class Types. Yi Hong May 14, 2015

Reserved Words and Identifiers

CMPT 125: Lecture 3 Data and Expressions

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Basics of Java Programming

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

Built-in Types of Data

CS1150 Principles of Computer Science Math Functions, Characters and Strings

Chapter 2 Elementary Programming

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Introduction to C Language

Getting started with Java

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

1001ICT Introduction To Programming Lecture Notes

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

BITG 1233: Introduction to C++

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

Chapter 2 Working with Data Types and Operators

Visual C# Instructor s Manual Table of Contents

Chapter 2. Outline. Simple C++ Programs

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

MODULE 02: BASIC COMPUTATION IN JAVA

ME1107 Computing Y Yan.

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

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

Java Programming Fundamentals. Visit for more.

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

CS112 Lecture: Primitive Types, Operators, Strings

Program Elements -- Introduction

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Chapter 6 Primitive types

Lecture 2 FORTRAN Basics. Lubna Ahmed

Chapter 2: Data and Expressions

Beyond Base 10: Non-decimal Based Number Systems

Declaration and Memory

The type of all data used in a C++ program must be specified

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Operators in java Operator operands.

Variables, Constants, and Data Types

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

CS313D: ADVANCED PROGRAMMING LANGUAGE

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

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

Introduction to Informatics

What did we talk about last time? Examples switch statements

JAVA Programming Fundamentals

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

3. Java - Language Constructs I

CSI33 Data Structures

Variables and Values

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Using the um-fpu with the Javelin Stamp

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Beyond Base 10: Non-decimal Based Number Systems

Chapter 2 Basic Elements of C++

Basic types and definitions. Chapter 3 of Thompson

DATA TYPES AND EXPRESSIONS

Values and Variables 1 / 30

Mr. Monroe s Guide to Mastering Java Syntax

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Full file at

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Datatypes, Variables, and Operations

Computer System and programming in C

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Programming Lecture 3

Transcription:

CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture V: Mathematical Functions, Characters, and Strings

Introduction How would you estimate the area enclosed by four cities, given the GPS locations (latitude and longitude) of the cities? The following diagram illustrates the problem. After this lecture, you will be able to solve this problem using a computer program.

Common Mathematical Functions

Trigonometric Methods the Math class contains the following methods to compute common trigonometric formulas.

Trigonometric Methods Notice that the parameter to sin, cos, and tan must be an angle in radians. Notice that the return value of asin, acos, and atan is a degree in radians in the range between -PI/2 and PI/2

Exponent Methods The Math class also have five methods for dealing with exponential calculations.

Rounding Methods The Math class has four rounding methods.

Rounding Methods

min, max, and abs Methods min and max return the minimum and maximum of two numbers. abs returns the absolute value of a number

Case Study: Computing Angles of a Triangle Given the three sides of a triangle we can calculate the angles by using the formulas in the following diagram. See Code: ComputeAngles.java

Character Data Type and Operations

The char Data Type The char data type represents a single character. A character literal is enclosed in single quotation marks ' ' Example: char letter = 'A'; char numchar = '4'; NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding character. For example, the following statements display character 'b'. char ch = 'a'; System.out.println(++ch);

Unicode and ASCII Code Remember that computers use binary numbers internally. How then, is a character stored? as a sequence of 0s and 1s each character is mapped to a binary representation encoding characters can be encoded in different ways and how they are encoded is defined by an encoding scheme

Unicode and ASCII Code Java supports Unicode, encoding scheme established by the Unicode Consortium supports the interchange, processing, and display of written texts in many of the world's languages originally designed as a 16-bit character encoding. the char data type was originally designed to support character encoding 16 available bits means we can represent 65,536 different types of characters believe it or not, this was not enough to support ALL the characters in the world

Unicode and ASCII Code Unicode was developed to extend the encoding to include 1,112,064 unique characters. characters beyond the original 16-bit limit are called supplementary characters. Java supports these supplementary characters. We will only discuss the characters that can be represented by the original 16-bit encoding scheme.

Unicode and ASCII Code A 16-bit Unicode takes two bytes, preceded by \u, expressed in four hexadecimal digits that run from \u0000 to \uffff. Example: char letter = '\u0041'; (Unicode) char numchar = '\u0034'; (Unicode)

Unicode and ASCII Code Most computers use ASCII (American Standard Code for Information Interchange) to represent the characters. 8-bit encoding scheme for representing all uppercase and lowercase letters, digits, punctuation marks, and control characters. Unicode includes ASCII code, with \u0000 to \u007f corresponding to the 128 ASCII characters. (So the ASCII characters are a subset of the Unicode standard)

Unicode and ASCII Code You can use ASCII characters such as 'X', '1', and '$' or you can use the Unicodes. Example:

Escape Sequences How do you print a message with quotation marks in the output? System.out.println("He said "Java is fun""); The statement has a syntax error The compiler thinks the 2nd quotation character is the end of the string. Java defines escape sequences to represent special characters An escape sequence begins with '\'

Escape Sequences

Escape Sequences Example: System.out.println("He said "Java is fun""); //Would have a compile error because you cannot have nested quotes. System.out.println("He said \"Java is fun\""); //Is correct Note: \ and " together are considered to be one character \"

Casting between char and Numeric Types A char can be cast into any numeric type and vice versa. When a char is cast into a numeric type, the character s unicode is cast into the specified numeric type Example: int i = (int)'a'; //i is now equal to 65 char ch = (char)65.25; //ch is now 'A' Code Example: CharOperands.java

Casting between char and Numeric Types You can also use the numeric operators with the char type. This means that the calculation is performed on the unicode of the character.

Comparing and Testing Characters Characters can actually be compared using the relational operators (similiar to comparing two numbers) the Unicodes of the two characters are compared. Example:

Comparing and Testing Characters Take a look at Appendix B (ASCII character set) in the text book. notice that the Unicodes for the lowercase letters are consecutive integers same goes for the uppercase letters and for numeric characters We can use the above information to test characters. Example:

Comparing and Testing Characters The Character class also provides some methods for testing characters.