Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is...

31
Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Chapter 2: Variables and Types

Transcript of Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is...

Page 1: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Think Java:

How to Think Like a Computer

Scientist

5.1.2

by Allen B. Downey

Chapter 2:

Variables and Types

Page 2: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Agenda

• More printing techniques

• Variables

• Operations with Variables

Page 3: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

More Printing

• You can have as many statements as you want in main

• Notice that comments also work at the end of a line

class Hello

{

// Generates some simple output.

public static void main(String[] args) {

System.out.println("Hello, world."); // line1

System.out.println("How are you?"); // line2

}

}

Page 4: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Strings

• Phrases that appear in quotation marks are called strings

– made up of a sequence (string) of characters

– Strings can contain any combination of letters, numbers, and other special characters

– Surrounded by quotation marks

– String Not a string

"Hello, World"

"123abc"

"He said, #$%$%!"

"456" 456

Page 5: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

println vs print

• println is short for “print line”

– after each line it adds a newline character

– next println appears on the next line

• To display multiple print statements on one line, use print:

• Outputs a single line: Goodbye, cruel world!

// Generates some simple output.

public static void main(String[] args) {

System.out.print("Goodbye, "); // same line

System.out.println("cruel world!");

}

Page 6: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

White space and formatting

• Notice the space after comma in “Goodbye, ”

– Appears in quotes so it is sent to output

– Spaces outside of quotation marks are ignored

– This would work just as well:

public static void main(String[] args) {

System.out.print("Goodbye, ");

System.out.println("cruel world!");

}

Page 7: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

White space and formatting (continued)

• Breaks at the ends of lines (newlines) are also ignored:

• Newlines and spaces help organizing your program

– make it easier to read and locate errors

System.out.print("Goodbye, ");System.out.println

("cruel world!");

Page 8: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

1-8

Variables and Declaration

• A variable is a named location that stores a value.

• Values can be printed, stored and operated on. – The strings ("Hello, World.", "Goodbye, ", etc.) are values.

– The integers ( 234, -23, 0, etc) are also values

• To store a value, you have to create a variable.

• If the values we want to store are strings, we declare that the new variable is a String:

• This statement is a declaration

String bob;

Page 9: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Variables, Assignment, and Access

• If we want to store a value in a variable, we assign the value to it using the assigment (=) operator:

• To access the contents of a variable, we can put the variable in a print or println statement:

• Prints the string: Hello, Robert Jones

bob = "Robert Jones";

System.out.print("Hello, ");

System.out.println(bob);

Page 10: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Quiz – what is printed by the following?

String bob;

bob = "Robert Jones";

String joe;

joe = "Frank";

System.out.print("Hello, ");

System.out.print(joe);

System.out.print(", Have you seen ");

System.out.print(bob);

System.out.println("?");

Page 11: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Variables and type

• Each variable has a type

– the type determines what kind of values it can store

• the int type can store integers

• the String type can store strings

– Some types begin with a capital letter and some with lower-case

• There is no such type as Int or string

• compiler will object if you try to make one up

Page 12: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Example • If joe was to store an integer value

– syntax would be int joe;

• Then if the program was:

– The output would be:

Joe is 24 years old

Perhaps age would make a better name for this variable instead of joe

int joe;

joe = 24;

System.out.print("Joe is ");

System.out.print(joe);

System.out.println(" years old");

Page 13: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Naming variables

• make up variable names that indicate what is stored in the variable.

– For example, these variable declarations:

• indicate what values would be stored in them.

– This also demonstrates the syntax for declaring multiple variables with the same type: hour and second are both integers (int type).

String firstName;

String lastName;

int hour, minute;

Page 14: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Diagraming Variables

• If we assign new values to our variables

• A helpful way to picture them is to draw a box

– name of the variable on the outside

– value of the variable on the inside

• This figure shows the effect of the three assignment statements:

bob = "Hello."; // give bob the value "Hello."

hour = 11; // assign the value 11 to hour

minute = 59; // set minute to 59

Page 15: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Some Points to Remember

• A variable must have the same type as the value you assign it.

– Can't store a String in minute or an integer in bob.

• Some strings look like integers, but they are not.

– bob can contain the string "123",

– but NOT the number 123.

– bob = "123"; // legal

– bob = 123; // not legal

Page 16: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

More examples of Printing Variables – 1)

• The output of this program is

The value of firstLine is Hello, again!

String firstLine;

firstLine = "Hello, again!";

System.out.print("The value of firstLine is ");

System.out.println(firstLine);

Page 17: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

More examples of Printing Variables – 2)

• The output of this program is The current time is 11:59.

int hour, minute;

hour = 11;

minute = 59;

System.out.print("The current time is ");

System.out.print(hour);

System.out.print(":");

System.out.print(minute);

System.out.println(".");

Page 18: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Quiz – a) Draw a diagram of the variables:

b) What is the output?

int age, height;

String name;

age = 45;

height = 65;

name = "Sarita";

System.out.print(name);

System.out.print(" is ");

System.out.print(age);

System.out.print(" years old and ");

System.out.print(height);

System.out.print(" inches tall");

Page 19: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Operators for Strings

• the + operator does work with Strings

– represents concatenation

– "Hello, " + "world." yields the string "Hello, world."

– bob + "ism" adds the suffix ism to the end of whatever bob is (for example "Robert Jonesism")

• All other operations illegal

– even if the strings look like numbers.

– illegal: (if bob has type String)

bob - 1 "Hello"/123 bob * "Hello"

• how to tell type of variable?

– look at the place where it is declared.

Page 20: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Due Lab1 Part B problem 1

If you finish early, do problem 2 -- play “Stump the Chump” on your solution to problem 2

Page 21: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Keywords

• Certain words are reserved in Java

– used by the compiler to parse a program

– These words are called keywords

– include public, class, void, int, and many more.

• Complete list at http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html.

• code highlighting helps identify

– turns keywords blue

Page 22: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Math Operators and Expressions

• Operator: symbol used to represent computation

– addition is +, subtraction is -, multiplication is *

– division is /

• Expressions:

1+1 hour-1 hour*60 + minute minute/60

• Expressions can contain variable names and numbers

• Variables are replaced with their values before the computation is performed.

Page 23: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Arithmetic Example

• generates this output: Number of minutes since midnight: 719

Fraction of hour that has passed: 0 huh?

• Something weird with minute/60 calculation

int hour, minute; hour = 11; minute = 59;

System.out.print("Number of minutes since midnight: ");

System.out.println(hour*60 + minute);

System.out.print("Fraction of hour that has passed: ");

System.out.println(minute/60);

Page 24: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Integer Division

• Suppose minute = 59

– We would expect minute/60 = 0.98333

– Why is it 0 ???

• When both operands are integers

– (operands are the things operators operate on)

– result is also an integer

– by convention integer division always rounds down

– even in cases like this where the next integer is so close.

Page 25: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Workaround for Fraction of hour

• The result is Percentage of the hour that has passed: 98

• Still rounds down but multiplying minute by 100 gives a more accurate result

• Another variable type, float, handles this situation better (next chapter)

System.out.print("Percentage of hour that has passed: ");

System.out.println(minute*100/60);

Page 26: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

2-26

Order of Operations

• There is a set order in which arithmetic operations will be carried out.

• Operators of same precedence are handled left to right

Operator Associativity Example Result

-

(unary negation) Right to left x = -4 + 3; -1

* / Left to right x = -4 + 7 / 3 * 13 + 2; 24

+ - Left to right x = 6 + 3 – 4 + 6 * 3; 23

Higher

Priority

Lower

Priority

Page 27: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

2-27

Overriding Precedence with Parenthesis

• When parenthesis are used in an expression, the inner most parenthesis are processed first.

• If two sets of parenthesis are at the same level, they are processed left to right.

• x = ((4*5) / (5-2) ) – 25; // result = -19

1

3

4

2

Page 28: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Composition

• Ability to take small building blocks and compose them

– For example, we know how to multiply

– we know how to print;

– combine them in a single statement:

System.out.println(17 * 3); // prints 51

System.out.println("Hello, " + bob) // "Hello, Robert Jones"

• Any expression involving numbers, strings and variables, can be used inside a print statement.

Page 29: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Composition and Assignment

• you can also put arbitrary expressions

– on the right-hand side of an assignment statement: int percentage;

percentage = (minute * 100) / 60;

• WARNING: left side of assignment has to be a variable name, not an expression.

– illegal: minute+1 = hour;

Page 30: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Do Lab 1 part B Exercise 3

Page 31: Chapter 2: Variables and Types - tomrebold.comtomrebold.com/csis10a/ch01a02/lec2.pdfQuiz – what is printed by the following? ... •Certain words are reserved in Java ... Chapter

Exercise 2 continued

• Modify the program so that it prints the date in standard American form: Saturday, July 16, 2011.

• Modify the program again so that the total output is:

• American format: Saturday, July 16, 2011

• European format: Saturday 16 July, 2011

• The point of this exercise is to use string concatenation to display values with different types (int and String), and to practice developing programs gradually by adding a few statements at a time.