8/25/2015 COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 – 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? • COMP 110 ‐ Fall 2015 2 1
8/25/2015 Today Announcements • Still registering… • Labs / recitations – register for them • Join Piazza: • piazza.com/unc/fall2015/comp110 Assignment1: due Fri, Aug 28 at 11:55 PM • Check Sakai, Piazza and class webpage • regularly Your first program • COMP 110 ‐ Fall 2015 3 Identifiers • 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 identifiers COMP 110 ‐ Fall 2015 4 2
8/25/2015 Illegal Identifiers COMP 110 ‐ Fall 2015 5 Questions Classify the following as legal or illegal identifiers: 1. My First Program illegal 2. my1stProgram legal 3. 1stProgram illegal 4. $money legal 5. an_identifier legal 6. Jane'sProgram illegal COMP 110 ‐ Fall 2015 6 3
8/25/2015 Primitive Data Types What is a Data Type? • Primitive data are fundamental values such as numbers and characters • A set of values and the operations that can be performed on those values • Operations are performed on primitive types using built‐in operators COMP 110 ‐ Fall 2015 7 Primitive 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 • boolean COMP 110 ‐ Fall 2015 8 4
8/25/2015 Primitive Data Types (Numeric Types) • The difference between the various numeric primitive types is their size, and therefore the values they can store: Type Storage Min Value Max Value byte 8 bits -128 127 short 16 bits -32,768 32,767 int 32 bits -2,147,483,648 2,147,483,647 < -9 x 10 18 > 9 x 10 18 long 64 bits +/- 3.4 x 10 38 with 7 significant digits float 32 bits +/- 1.7 x 10 308 with 15 significant digits double 64 bits COMP 110 ‐ Fall 2015 9 Integers • 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 list COMP 110 ‐ Fall 2015 10 5
8/25/2015 Primitive Data Types (Characters) • 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) COMP 110 ‐ Fall 2015 11 Primitive Data Types (Boolean) • 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 words COMP 110 ‐ Fall 2015 12 6
8/25/2015 Arithmetic Expressions • Expression ‐ a combination of one or more operands and their operators • Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % • If either or both operands associated with an arithmetic operator are floating point, the result is a floating point COMP 110 ‐ Fall 2015 13 Division and Remainder • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) equals? 14 / 3 4 equals? 8 / 12 0 • The remainder, or mod ulus, operator (%) returns the remainder after dividing the second operand into the first (only works with integer types) equals? 14 % 3 2 equals? 8 % 12 8 COMP 110 ‐ Fall 2015 14 7
8/25/2015 Unary vs. Binary Operators • Unary operators – has only one operand – example: ‐ (negative, not subtraction) ‐5 • Binary operators – has two operands – example: ‐ (subtraction) 5 ‐ 3 COMP 110 ‐ Fall 2015 15 Operator Precedence Determines the order in which • operators are evaluated: 1. multiplication, division, and remainder 2. addition, subtraction, and string concatenation 3. arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the • evaluation order (just like in math) COMP 110 ‐ Fall 2015 16 8
8/25/2015 Operator Precedence (PEMDAS) Parentheses: 6 * (5 + 7) vs. 6 * 5 + 7 • Exponents (powers, roots – 2 5 36 1/2 ) • Multiplication / Division / Mod • Addition / Subtraction • Left to right • Which is these is correct? • this one! 30 / 5 * 3 = 6 * 3 = 18 30 / 5 * 3 = 30 / 15 = 2 COMP 110 ‐ Fall 2015 17 Operator Precedence • What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1 COMP 110 ‐ Fall 2015 18 9
8/25/2015 Integral Expressions • All operands are integers • Result is an integer • Examples: 2 + 3 * 5 3 + x – y / 7 x + 2 * (y – z) + 18 COMP 110 ‐ Fall 2015 19 Floating-point Expressions • All operands are floating‐point numbers • Result is a floating‐point • Examples: 12.8 * 17.5 – 34.50 x * 10.5 + y ‐ 16.2 7.0 / 3.5 COMP 110 ‐ Fall 2015 20 10
8/25/2015 Mixed Expressions • Operands of different types • Examples: 2 + 3.5 6 / 4 + 3.9 • Integer operands yield an integer result • Floating‐point operands yield a floating‐point result • If both types of operands are present, the result is a floating‐point number – implicit type coercion • Precedence rules are followed COMP 110 ‐ Fall 2015 21 Type Conversion (Casting) • Used to avoid implicit type coercion • Syntax ( dataTypeName ) expression • Expression evaluated first, then type converted to dataTypeName • Examples: ( int ) (7.9 + 6.7) = 14 ( int ) (7.9) + ( int )(6.7) = 13 COMP 110 ‐ Fall 2015 22 11
8/25/2015 Questions 1. (5 + 4) % 6 9 % 6 3 11 % 3.5 2. (5 + 6) % 3.5 not possible 3. (double) (13) / 2 13.0 / 2 6.5 4. (double) (13 / 2) (double) (6) 6.0 COMP 110 ‐ Fall 2015 23 Our First Program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } COMP 110 ‐ Fall 2015 24 12
8/25/2015 Next class (Thu, Aug 27) Binary representation • Program in class: Adding two numbers • Assignment1 DUE Fri, Aug 28 • Reading Assignment: Chapter 1 COMP 110 ‐ Fall 2015 25 Teaching Assistants o Yenchun Chen o Junpyo Hong (JP) o Ben Newton Ben Newton Junpyo Hong (JP) COMP 110 ‐ Spring 2015 26 13
8/25/2015 Teaching Assistants Dana Elhertani Spencer Byers Sarah White Jeffrey Young Max Daum Camden Link COMP 110 ‐ Spring 2015 27 14
Recommend
More recommend