DOC PREVIEW
UNC-Chapel Hill COMP 14 - LECTURE NOTES

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 14 Introduction to ProgrammingReview ExerciseToday in COMP 14Reading Check-UpIntroductionSample Java ProgramProgramming LanguagesSpecial SymbolsWord Symbols aka reserved words, or keywordsIdentifiersIllegal IdentifiersQuestionsPrimitive Data Types What’s A Data Type?Primitive Data TypesPrimitive Data Types Numeric TypesIntegersPrimitive Data Types CharactersPrimitive Data Types BooleansArithmetic ExpressionsDivision and RemainderUnary vs. Binary OperatorsOperator PrecedenceSlide 23Integral ExpressionsFloating-point ExpressionsMixed ExpressionsType Conversion (Casting)Questions Evaluate These ExpressionsThe class StringStringsParsing Numeric StringsSlide 32SummarySlide 34To doTomorrowThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJune 27, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Review Exercise•Execution of c=2*a+b in a computer:♦Take the value of a♦Multiply it by 2♦Take the value of b♦Add b to the previous result♦Put final result into cThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Today in COMP 14•Java♦special symbols♦identifiers♦data types♦operators♦expressions♦stringsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4Reading Check-Up1. The ________ rules of a language determine which instructions are valid.2. True or False? Hello! is an example of a legal Java identifier. _______3. If an operator has an integer and a floating-point operand, the result of the operation is a ___________ number.4. The expression (int) (9.2) evaluates to ___.syntaxFalsefloating-point9The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5Introduction•Computer program: a sequence of statements whose objective is to accomplish a task •Programming: process of planning and creating a program•Programming language: a set of rules, symbols, and special wordsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6Sample Java Programpublic class Hello {public static void main (String[] args){System.out.println ("Hi There!");}}Upon execution, this program would displayHi There!The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7Programming Languages•Programming languages have rules of grammar just as English does•syntax rules - which statements are legal and which are not•semantic rules - determine the meaning of the instructions•token - smallest individual unit of a program♦special symbols♦word symbols♦identifiersThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8Special Symbols+ - * /. ; ? ,<= != == >=The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9Word Symbolsaka reserved words, or keywords•int•float•double•char•void•public•static•throws •return• reserved words are always all lowercase• each word symbol is considered to be a single symbol• cannot be used for anything other than their intendedpurpose in a program• shown in blue typewriter font in textbook• full table in Appendix AThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10Identifiers•Names of things (variables, constants, methods) in your programs•Can be composed of any combination of letters, digits, underscore (_), and dollar sign ($) •Cannot begin with a digit•May be any length•Java is case-sensitive♦Total, total, and TOTAL are different identifiersThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11Illegal IdentifiersThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12QuestionsClassify the following as legal or illegal identifiers:1. My First Program ____2. my1stProgram ____3. 1stProgram ____4. $money ____5. an_identifier ____6. Jane'sProgram ____illegallegalillegallegallegalillegalThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Primitive Data TypesWhat’s A Data Type?•A set of values and the operations that can be performed on those values•Primitive data are fundamental values such as numbers and characters•Operations are performed on primitive types using built-in operatorsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14Primitive Data Types•8 primitive data types in Java♦4 represent integers•byte, short, int, long♦2 represent floating point numbers•float, double♦1 represents characters•char♦1 represents boolean values •booleanThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15Primitive Data TypesNumeric Types•The difference between the various numeric primitive types is their size, and therefore the values they can store:TypebyteshortintlongfloatdoubleStorage8 bits16 bits32 bits64 bits32 bits64 bitsMin Value-128-32,768-2,147,483,648< -9 x 1018+/- 3.4 x 1038 with 7 significant digits+/- 1.7 x 10308 with 15 significant digitsMax Value12732,7672,147,483,647> 9 x 1018The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16Integers•Examples: -6728, -67, 0, 78, 36782•Positive integers do not have a '+' sign in front of them (but they can)•No commas are used in an integer♦commas in Java are used to separate items in a listThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17Primitive Data TypesCharacters•A char stores a single character from the Unicode character set♦an ordered list of characters, and each character corresponds to a unique number♦uses 16 bits per character, allowing for 65,536 unique characters•Character literals are delimited by single quotes:'a' 'X' '7' ' ' '$' ',' '\n'newline character(we'll discuss later)The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18Primitive Data TypesBooleans•Only two valid values♦true or false♦uses 1 bit for storage•Represent any situation that has 2 states♦on - off♦true - false•true and false are reserved wordsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie19•Expression - a combination of one or more operands and their operators•Arithmetic expressions compute numeric results and make use of the arithmetic operators:•If either or both operands associated with an arithmetic operator are floating point, the result is a floating pointAddition +Subtraction -Multiplication *Division /Remainder %Arithmetic ExpressionsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie20•If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded)•The remainder, or modulus, operator (%) returns the remainder after dividing the second operand into the first


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?