Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming

2 SE15: Types and Expressions4–24–2 Today’s 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

3 SE15: Types and Expressions4–34–3 Lecture Outline Primitive Types Keyboard and Screen I/O Expressions

4 SE15: Types and Expressions4–44–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

5 SE15: Types and Expressions4–54–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

6 SE15: Types and Expressions4–64–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

7 SE15: Types and Expressions4–74–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

8 SE15: Types and Expressions4–84–8 Assignment Statements Examples: amount = 3.99; firstInitial = ‘B’; score = numberOfQueens*11+numberOfKings*12; count = count + 10; amount += 4; You can’t 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;

9 SE15: Types and Expressions4–94–9 Simple Screen Output 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

10 SE15: Types and Expressions4–10 Simple input import java.util.Scanner; Tells the compiler where to find the definition of Scanner Scanner keyboard = 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

11 SE15: Types and Expressions4–11 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

12 SE15: Types and Expressions4–12 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

13 SE15: Types and Expressions4–13 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

14 SE15: Types and Expressions4–14 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?

15 SE15: Types and Expressions4–15 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 -

16 SE15: Types and Expressions4–16 Arithmetic Expressions Ordinary expression Java expression rate*rate + delta 2*(salary+bonus) (a-7)/(t-9*v)

17 SE15: Types and Expressions4–17 Summary Looked at: what variables & Identifiers are and naming conventions used in Java Primitive types & writing arithmetic expressions in Java Simple input and output

18 SE15: Types and Expressions4–18 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


Download ppt "Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google