Announcements • For homework due Thursday, work alone -do not work in pairs • New class location: Olin 155 • Office hour oops! Lyn: MW, 11:15-12:15 • Review Sessions • Do not send mail that has html or attachments CS100 Lecture 2 1 Today’s Topics • • • • • • • Brief review of yesterday Variables and constants Declaration of a variable Assignment to a variable Declaration of a constant The conditional (if) statement Input CS100 Lecture 2 2 Review • • • • Algorithms Methods Method call/method invocation Importance of comments -- document your code • See the course website for information on Java programming style • Any questions on homework yet? CS100 Lecture 2 3 Introduction to Variables • Think of a variable as a box into which a value can be placed 32 • Declarations: int x; int timeOfDay CS100 x -532 timeOfDay 1115 Lecture 2 4 What Happens in a Declaration? • A variable is a name for a location in memory • When a variable is declared you instruct the compiler to reserve enough space for the type of variable you’re declaring • A variable can only store one value of its declared type CS100 Lecture 2 5 Variable Names -- Identifiers • • • • Begin with a letter, _, or $ Contain letters, digits, _, and $ Generally, we don’t use _ or $ One convention: first letter small, capitalize words within the identifier – – – – CS100 timeOfDay f12 f0 restOfInput Lecture 2 6 Assignment • int timeOfDay = 3; // timeOfDay 3 • timeOfDay = 430; // timeOfDay 430 • timeOfDay = 30 + timeOfDay + 5; // timeOfDay = 465 465 CS100 Lecture 2 7 Assignment Syntax • <variable> = <expression> ; • Note well: “=” does not mean equality in Java • Think of x = y as “Assign y to x” or “x becomes y” or “x gets the value of y” • Think of x == y as “x equals y” • The expression “x==y” yields true if x and y contain the same value, false otherwise CS100 Lecture 2 8 Booleans • Boolean is a data type like int • It has two values: true and false boolean answer; int x; int y; x = 3; y = 5; answer = (x == y); CS100 What value ends up in the variable “answer” ? Lecture 2 9 Some history = was introduced as a sign for equality in the 1500s. It was chosen because nothing could be more equal than 2 parallel lines. The use of = for assignment was started in Fortran in the early 1950s. C and C++ continued this and introduced == for equality. This has caused great confusion and much wasted time on the part of programmers the world over. Algol 60 used := for assignment and = for equality. Remember: concepts, not syntax, but be careful of the syntax! CS100 Lecture 2 10 Constants • If data doesn’t change throughout program, use constant • Helpful to name this value final int CS100_STUDENTS = 78; final double PI = 3.14159; • Use uppercase to distinguish from variables, whose values do change throughout the program CS100 Lecture 2 11 Naming Conventions • Short variable names make programs shorter, and more manageable • Long names can convey more meaning • Name can almost never give full meaning, so comment when the variable is declared. • Avoid names such as: “thisIsTheVariableThatStoresTheNumberOfBooksInMyLibrary” CS100 Lecture 2 12 Conditional Statement -- If • Conditional statements allows a choice of a command based on some condition // Store the maximum of x and y in z z = x; if (y > x) z = y; • Syntax: if (boolean expression) statement CS100 Lecture 2 13 Block statements in if statements • Suppose you would like to execute more than one thing based on the condition? // if x != y, store 0 in x, store y in z if (y != x) { x = 0; z = y; } OR // if x != y, store 0 in x, store y in z if (y != x) { x = 0; z = y; } CS100 Lecture 2 14 More on block statements • Consider: { <statement 1> <statement 2> <statement 3> …. } • { and } are used to delimit a sequence of statements that are to act together, just as ( and ) are used in arithmetic expressions • Many options for positioning { and }, just be consistent CS100 Lecture 2 15 Second Form of if statement • Provide multiple options: // Store maximum of x and y in z if (x >= y) { z = x; } else { z = y; } CS100 Lecture 2 16 Example of Nested Ifs if (coin == HEADS) if (choice == RECEIVE) System.out.println(“You won, will receive”); else System.out.println(“You won, will kickoff”); else System.out.println(“You lost.”); CS100 Lecture 2 17 Operator Syntax • • • • • • = = equal to ! = not equal to < less than > greater than < = less than or equal to > = greater than or equal to CS100 Lecture 2 18 Operator Precedence • * / % then + - + then = • 14 + 8 / 2 is therefore 18 (not 11) • Use parentheses to make things clear: – (14 + 8) / 2 = 11 – Must be an equal number of left and right parens CS100 Lecture 2 19 Input (briefly) • Classes that provide facilities for input and export are available in package java.io • Place the phrase import java.io.* at the top of your Java source file • Ok if you don’t understand all of this yet • Read Section 3.3 in text for details CS100 Lecture 2 20 Characters and Strings (briefly) • char date type – ASCII plus stuff = UNICODE – A character literal uses single quotes: ‘b’ – char firstChar = ‘b’ • String is not a primitive type, it’s a Class • Strings represented as objects of the String class • String manipulation can get complicated -see text for more details CS100 Lecture 2 21 Getting Input from user Import java.io.*; // Read a string from the user String message; message = stdin.readLine(); // Read numeric input from the user String string1; int num1; string1 = stdin.readLine(); num1 = Integer.parseInt(string1); CS100 Lecture 2 22 Example system.out.println(“What’s your name?”); name = stdin.readLine(); system.out.println(“Your name is: ” + name); What is your name? Milly Lunor Your name is: Milly Lunor CS100 Lecture 2 23 Small Formatting Tricks • Java uses the \ in output statements to indicate formatting – \t tab, \n newline, \” double quote, \’ single quote, \\ backslash – “Name\tDOB” results in Name DOB – “foo\n\nbar” results in foo bar CS100 Lecture 2 24 Discussion Issues • • • • Java is strongly-typed. What good is that? Why use constants? Why would you use (a > 0) vs. (a >= 1) Is Y2K a compile-time, run-time or logical error? • If a system fails, who is responsible? CS100 Lecture 2 25