CS1150 Principles of Computer Science Math Functions, Characters and Strings

Similar documents
CS1150 Principles of Computer Science Midterm Review

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

Mathematical Functions, Characters, and Strings. APSC 160 Chapter 4

CS1150 Principles of Computer Science Introduction (Part II)

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

Chapter 4 Mathematical Functions, Characters, and Strings

CS1150 Principles of Computer Science Final Review

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2

CS1150 Principles of Computer Science Loops

CS-201 Introduction to Programming with Java

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2 Iterative Code Design handout Style Guidelines handout

CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

Introduction to functions in C

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

COP2800 Homework #3 Assignment Spring 2013

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

CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

Lab 4. Name: Checked: Objectives:

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

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

CS110: PROGRAMMING LANGUAGE I

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

CS1150 Principles of Computer Science Introduction

CS5530 Mobile/Wireless Systems Swift

Relational Operators, and the If Statement. 9.1 Combined Assignments. Relational Operators (4.1) Last time we discovered combined assignments such as:

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Computer Organization and Architecture

Reading and writing data in files

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture2 Functions

Flying into Trig on a Paper Plate

Higher Check In Triangle mensuration. 5. Calculate angle K, giving your answer to 3 significant figures. 158 m. 195 m A cm.

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

Data Structure Interview Questions

HOW-TO Use SAP SUIM OR RSUSR008_009_NEW to Analysing Critical Authorisations

EASTERN ARIZONA COLLEGE Java Programming I

Module 4: Characters, Strings, and Mathematical Functions

1 Version Spaces. CS 478 Homework 1 SOLUTION

Using UB Stream and UBlearns

Procurement Contract Portal. User Guide

It has hardware. It has application software.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Common Language Runtime

TECHNICAL REQUIREMENTS

TL 9000 Quality Management System. Measurements Handbook. SFQ Examples

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Chapter 5 Methods / Functions

Solving Problems with Trigonometry

Managing Your Access To The Open Banking Directory How To Guide

Transferring a BERNINA V8 software license

Higher Maths EF1.2 and RC1.2 Trigonometry - Revision

Tutorial 5: Retention time scheduling

STUDIO DESIGNER. Design Projects Basic Participant

LAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C

These tasks can now be performed by a special program called FTP clients.

Preparation: Follow the instructions on the course website to install Java JDK and jgrasp on your laptop.

The Java if statement is used to test the condition. It checks Boolean condition: true or false. There are various types of if statement in java.

CSCI L Topics in Computing Fall 2018 Web Page Project 50 points

Structure Query Language (SQL)

Step 3:- You Will See the Sign-in Page. Then Enter your Login ID & Password and Click on the Sign in Button.

Using CppSim to Generate Neural Network Modules in Simulink using the simulink_neural_net_gen command

I - EDocman Installation EDocman component EDocman Categories module EDocman Documents Module...2

CS5530 Mobile/Wireless Systems Android UI

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

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

Information about the ACC Education App Featuring ACCSAP 9

Create Your Own Report Connector

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

DECISION CONTROL CONSTRUCTS IN JAVA

Outlook Web Application (OWA) Basic Training

MATH PRACTICE EXAM 2 (Sections 2.6, , )

Concentrix University Learning Portal FAQ Document

EASTERN ARIZONA COLLEGE Visual Basic Programming I

Project #1 - Fraction Calculator

Uploading Your Catalogue

TaiRox Mail Merge. Running Mail Merge

EBSCOhost User Guide Print/ /Save. Print, , Save, Notetaking, Export, and Cite Your Search Results. support.ebsco.com

Automatic imposition version 5

RxAXIS Security Module 09/25/2013

Java Programming Course IO

CS4500/5500 Operating Systems Synchronization

Project 3 Specification FAT32 File System Utility

Arduino Basics Intro to ArduBlocks

Maintaining order. A small student database. Managing databases. Representing table of student data. Sorting and searching

Client Configurations

Primitive Types and Methods. Reference Types and Methods. Review: Methods and Reference Types

INSTALLING CCRQINVOICE

Introduction to CS111 Part 2: Big Ideas

CSE 3320 Operating Systems Synchronization Jia Rao

PaperStream Capture change history

Because of security on the site, you cannot create a bookmark through the usual means. In order to create a bookmark that will work consistently:

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Code should compile and demonstrate proper programming style; e.g., whitespace, identifier naming, etc.

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

Importing data. Import file format

How to Mass Assign Student Course Requests

COMPUTER SCIENCE COMPETITION - JAVA TOPIC LIST

3 AXIS STAGE CONTROLLER

Relius Documents ASP Checklist Entry

ClassFlow Administrator User Guide

Transcription:

CS1150 Principles f Cmputer Science Math Functins, Characters and Strings Yanyan Zhuang Department f Cmputer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Clrad Springs

Mathematical Functins Java prvides many useful methds in the Math class fr perfrming cmmn mathematical functins. (we have used Math.sqrt(), Math.pw(), Math.randm()) 2

The Math Class Math class is different frm Scanner class Dn t create a Math instance } N need fr Math mymath = new Math(); We call Math class methds withut creating an instance Use Math.methdName() directly } duble squarert = Math.sqrt(25); 3

The Math Class Type Math. Yu can see cnstants and methds 4

The Math Class Class cnstants: PI (3.14159 ) E (2.71828 base f natual lg) Class methds: Trignmetric Methds Expnent Methds Runding Methds min, max, abs, and randm Methds 5

min, max, and abs max(a, b)and min(a, b) Returns the maximum r minimum f tw parameters. abs(a) Returns the abslute value f the parameter. randm() Returns a randm duble value in the range [0.0, 1.0). Numbers1.java Examples: Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1 Numbers3.java 6

The randm Methd Generates a randm duble value greater than r equal t 0.0 and less than 1.0 (0 <= Math.randm() < 1.0). Examples: (int)(math.randm() * 10) 50 + (int)(math.randm() * 50) Returns a randm integer between 0 and 9. Returns a randm integer between 50 and 99. In general, a + Math.randm() * b Returns a randm number between a and a + b, excluding a + b. 7

Previus example Math.randm() Hw t generate a randm integer between [lwer, upper)? } Example: int lwer=100, upper=120; } randmduble = Math.randm(); // [0.0, 1.0) } randmduble = randmduble * (upper-lwer); // [0.0, 20.0) } randmduble = lwer + randmduble; // [100.0, 120.0) } randmint = (int) randmduble; // cast duble à int Or in ne step } randmint = (int) (lwer + Math.randm() * (upper-lwer)); CS1150 UC. Clrad Springs

Runding Methds duble ceil(duble x) x runded up t its nearest integer. This integer is returned as a duble value. duble flr(duble x) x is runded dwn t its nearest integer. This integer is returned as a duble value. duble rint(duble x) x is runded t its nearest integer. If x is equally clse t tw integers, the even ne is returned as a duble. int rund(flat x) Return (int)math.flr(x+0.5). lng rund(duble x) Return (lng)math.flr(x+0.5). 9

Runding Methds Examples Math.ceil(2.1) returns 3.0 Math.ceil(2.0) returns 2.0 Math.ceil(-2.0) returns 2.0 Math.ceil(-2.1) returns -2.0 Math.flr(2.1) returns 2.0 Math.flr(2.0) returns 2.0 Math.flr(-2.0) returns 2.0 Math.flr(-2.1) returns -3.0 Math.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns 2.0 Math.rint(-2.1) returns -2.0 Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 Math.rund(2.6f) returns 3 Math.rund(2.0) returns 2 Math.rund(-2.0f) returns -2 Math.rund(-2.6) returns -3 Numbers2.java 10

Runding Methds Examples Math.rund(x) returns int r a lng (depends n if the argument is a flat r a duble) int x = Math.rund (81.7); // This wn't wrk? Why? // 81.7 is a duble s need t stre result in lng int intresult = Math.rund (81.7f); lng lngresult = Math.rund(81.7); // Returns 82 - an int // Returns 82 - a lng CS1150 UC. Clrad Springs

Expnent Methds exp(duble a) Returns e raised t the pwer f a. lg(duble a) Returns the natural lgarithm f a. lg10(duble a) Returns the 10-based lgarithm f a. pw(duble a, duble b) Returns a raised t the pwer f b. Examples: Math.exp(1) returns 2.71 Math.lg(2.71) returns 1.0 Math.pw(2, 3) returns 8.0 Math.pw(3, 2) returns 9.0 Math.pw(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(10.5) returns 3.24 sqrt(duble a) Returns the square rt f a. 12

Trignmetric Methds sin(duble a) cs(duble a) tan(duble a) acs(duble a) asin(duble a) atan(duble a) Radians tradians(90) Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cs(0) returns 1.0 Math.cs(Math.PI / 6) returns 0.866 Math.cs(Math.PI / 2) returns 0.0 tdegrees() 13

Trignmetric Methds Prvide an angle in radians duble sinofzer = Math.sin(0); // 0 is in radian System.ut.println ("Math.sin(0) = " + sinofzer); // Displays 0.0 Examples with a value in degrees duble angleinradians = Math.tRadians(60); // 60 is degree System.ut.println("Sixty degrees = " + angleinradians + " radians"); duble sinofangle = Math.sin(angleInRadians); System.ut.println ("Math.sin(60) = " + sinofangle); CS1150 UC. Clrad Springs

Case Study: Cmputing Angles f a Triangle x2, y2 A = acs((a * a - b * b - c * c) / (-2 * b * c)) c B a C B = acs((b * b - a * a - c * c) / (-2 * a * c)) C = acs((c * c - b * b - a * a) / (-2 * a * b)) A x3, y3 x1, y1 b Write a prgram that prmpts the user t enter the x- and y-crdinates f the three crner pints in a triangle and then displays the triangle s angles. Let s see CmputeAngle.java example. 15

Character Data Type Values: ne single character Use single qute x t represent a character (duble qutes xxx are fr Strings) } char middleinitial = 'M'; } char numcharacter = '4'; // Assigns digit character 4 t numcharacter } System.ut.println(numCharacter); // Displays 4 Placing a character in it is n lnger a char: it is a String (with ne char in it) } char middleinitial = "M"; // Errr - cannt cnvert String t char Example: Char1.java CS1150 UC. Clrad Springs

Base 10, base 2 and base 16 The Decimal Number System is als called "Base 10" There are 10 symbls (0,1,2,3,4,5,6,7,8 and 9) There is n symbl fr "ten". "10" is actually tw symbls put tgether, a "1" and a "0 Hw t get a number in base 10? Example: 23, 123 But dn't have t use 10 as a "Base". Culd use 2 ("Binary"), 16 ("Hexadecimal"), r any number UC. Clrad Springs

Base 10, base 2 and base 16 Binary (base 2) 000, 001, 010, 011, 100, Cmputers can nly recgnize 0/1 s Hexadecimal (base 16) Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 3BD UC. Clrad Springs

Character Data Type char letter = 'A'; (ASCII) Fur hexadecimal digits. char numchar = '4'; (ASCII) char letter = '\u0041'; (Unicde) char numchar = '\u0034'; (Unicde) NOTE: The increment and decrement peratrs can als be used n char variables t get the next r preceding ASCII/Unicde character. Fr example, the fllwing statements display character b. char ch = 'a'; System.ut.println(++ch); 19

What s ASCII A cmputer cannt stre characters The nly thing it can stre and wrk with are bits A bit can nly have tw values: 1 r 0 (an "actual" bit is a blip f electricity that either is r isn't there) American Standard Cde fr Infrmatin Interchange (ASCII): 8-bit character scheme Prvides encding fr 128 characters (0 t 127) Based n rdering f English alphabet CS1150 UC. Clrad Springs

ASCII Cde fr Cmmnly Used Characters Characters Cde Value in Decimal Unicde Value '0' t '9' 48 t 57 \u0030 t \u0039 'A' t 'Z' 65 t 90 \u0041 t \u005a 'a' t 'z' 97 t 122 \u0061 t \u007a Mre infrmatin: http://kunststube.net/encding/ 21

Unicde Frmat Java characters use Unicde, a 16-bit encding scheme t supprt texts in the wrld s diverse languages. Unicde takes tw bytes, preceded by \u, expressed in fur hexadecimal numbers: frm '\u0000' t '\uffff'. S, Unicde can represent 65535 + 1 characters. Unicde \u03b1 \u03b2 \u03b3 fr three Greek letters 22

Escape Sequences fr Special Characters (Cnsidered a Single character) 23

Casting between char and Numeric Types int i = 'a'; // Same as int i = (int)'a'; System.ut.println ( i = " + i); // i = 97 all numeric peratrs can be applied t the char perands char c = 97; // Same as char c = (char)97; System.ut.println ("c = " + c); // c = a Increment and decrement can be used n char variables t get the next r preceding ASCII/Unicde character. char ch = 'a'; System.ut.println(++ch); //shws character b 24

Cmparing and Testing Characters if (ch >= 'A' && ch <= 'Z') System.ut.println(ch + " is an uppercase letter"); else if (ch >= 'a' && ch <= 'z') System.ut.println(ch + " is a lwercase letter"); else if (ch >= '0' && ch <= '9') System.ut.println(ch + " is a numeric character"); all numeric peratrs can be applied t the char perands 25

Methds in the Character Class Methd Descriptin isdigit(ch) Returns true if the specified character is a digit. isletter(ch) Returns true if the specified character is a letter. isletterofdigit(ch) Returns true if the specified character is a letter r digit. islwercase(ch) Returns true if the specified character is a lwercase letter. isuppercase(ch) Returns true if the specified character is an uppercase letter. tlwercase(ch) Returns the lwercase f the specified character. tuppercase(ch) Returns the uppercase f the specified character. Like the Math class - yu dn't create an instance f this class 26

Methds in the Character Class if (Character.isUpperCase(c)) { System.ut.println( c is an uppercase letter"); System.ut.println ("Its lwercase versin is: " + Character.tLwerCase(c)); } Example: Char2.java CS1150 UC. Clrad Springs