Presentation on theme: "Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming."— Presentation transcript:
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming
SE15: Types Expressions and Simple I/O5–25–2 Todays Learning Objectives To become familiar with the Java data types Learn about the assignment statement and expressions Learn about simple keyboard input and screen output
SE15: Types Expressions and Simple I/O5–35–3 Lecture Outline Identifiers Primitive Types Expressions Keyboard and Screen I/O
SE15: Types Expressions and Simple I/O5–45–4 Variables Variables are used to store data such as numbers or letters The computer needs to know: the name of the variable the type of data to be stored in the variable Declaring a variable gives this information int numberOfDeposits; char answer; double interestRate; Savitch pp47-55
SE15: Types Expressions and Simple I/O5–55–5 Rules for naming Identifiers Names in a program are called identifiers Java is case sensitive!! accountNumber, AccountNumber and accountnumber are all different Naming Conventions Use only letters and digits Punctuate multiword names using upper case letters numberOfDeposits How about the following? players-score, Faces.com, 4cast Names of classes start with an uppercase letter Names of variables, objects and methods start with a lowercase letter
SE15: Types Expressions and Simple I/O5–65–6 Variable names The names should suggest the variables use or indicate the kind of data they will hold distance - distance between two objects count - for counting something You should avoid using single letters x = y + z ???? Some words are reserved for keywords void for while
SE15: Types Expressions and Simple I/O5–75–7 Primitive Types Type nameKind of valueMemory usedRange byte integer1 byte-128 to 127 short integer2 bytes-32768 to 32767 int integer4 bytes long integer8 bytes float floating-point number 4 bytes double floating-point number 8 bytes char single character2 bytes boolean true or false1 bit
SE15: Types Expressions and Simple I/O5–85–8 Assignment Statements Examples: amount = 3.99; firstInitial = B; score = numberOfQueens*11+numberOfKings*12; count = count + 10; amount += 4; You cant put a square peg into a round hole int cost = 14.99; char initial = 2.6; int fraction = 4/6; int cost = cost + 0.1;
SE15: Types Expressions and Simple I/O5–95–9 Arithmetic Operators & Expressions You can form arithmetic expressions involving addition (+), subtraction (-), multiplication (*) and division (/) as you would in ordinary arithmetic or algebra. newBalance = oldBalance + deposit; Parentheses can be used to group items together newBalance = oldBalance – (oldBalance*rate); Using parentheses can help tell which operations will be performed first (cost + tax) * discount; cost + (tax * discount); Savitch pp 66-76
SE15: Types Expressions and Simple I/O5–10 Another Operator % operator When you divide one integer by another you get a result and a remainder, ie. 20 divided by 7 gives 2 with a remainder of 6 The % operator gives you the remainder 20%7 evaluates to 6 How can I test is an integer is even or odd ? How would I print out every 4 th integer in a series?
SE15: Types Expressions and Simple I/O5–11 Precedence Rules How do you evaluate Total = cost + tax * discount; ? Is it Total = (cost + tax) * discount; or Total = cost + (tax * discount); ? Operators are evaluated in order of precedence First: unary operators: +, -, ++, --, and ! Second: binary operators: *, /, and % Third:binary operators: + and -
SE15: Types Expressions and Simple I/O5–13 Quick Quiz Which of the following are legal statements 1) int x = 36.7; 2) Boolean b = x; 3) int k = 15; 4) int t = k; 5) t = t + 5; 6) double e = 2.5 * t; 7) e = e * 2 + 10 / 2; 8) double f = e – 5.0; If all the legal statements were part of the same method, what would the value of f be after the last valid statement?
SE15: Types Expressions and Simple I/O5–14 Simple Screen Output You have already used System.out System.out.println(The sum of those two numbers is + sum); System.out is an object println is a method + is a kind of and Savitch pp 56,57
SE15: Types Expressions and Simple I/O5–15 Simple input import java.util.Scanner; Tells the compiler where to find the definition of Scanner Scanner input = new Scanner(System.in); Sets things up so data can be entered from the keyboard int n1 = input.nextInt(); This is an assignment statement which gives a value to the variable n1 input.nextInt reads one int from the keyboard
SE15: Types Expressions and Simple I/O5–16 Constants The values of variables can change (hence the name) Literals like 2 or 3.7 are called constants G is a constant of type char Naming constants can be useful for improving clarity circumference = 2.0 * 3.14159 * radius; Can be more clearly written as circumference = 2.0 * PI * radius; How do we name 3.14159 as PI? public static final double PI = 3.14159; Savitch pp 58-59
SE15: Types Expressions and Simple I/O5–17 Type Casting Involves changing the type of a value to another type Changing 2.0 from a double to an int. double speed; speed = 10.0; int revolutionsPerSecond; revolutionsPerSecond = speed; revolutionsPerSecond = (int)speed; Savitch pp 61-63
SE15: Types Expressions and Simple I/O5–18 Summary Looked at: what variables & Identifiers are and naming conventions used in Java Primitive types & writing arithmetic expressions in Java Simple input and output
SE15: Types Expressions and Simple I/O5–19 Follow-up Work Read Chapter 2 of Savitch Complete the ranges for each of the primitive types Find out why numbers such as 3.7 are called floating point numbers. Write out the following as a Java expression
Your consent to our cookies if you continue to use this website.