250 likes | 258 Views
This lecture covers the basics of variables, assignments, and conditional statements in Java programming. Topics include variable declaration, assignment syntax, boolean expressions, constants, naming conventions, and if statements.
E N D
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 Lecture 2
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 Lecture 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? Lecture 2
Introduction to Variables • Think of a variable as a box into which a value can be placed • Declarations: int x;int timeOfDay 32 x -532 1115 timeOfDay Lecture 2
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 Lecture 2
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 • timeOfDay • f12 • f0 • restOfInput Lecture 2
Assignment • int timeOfDay = 3; // timeOfDay • timeOfDay = 430;// timeOfDay • timeOfDay = 30 + timeOfDay + 5;// timeOfDay = 465 3 430 465 Lecture 2
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 Lecture 2
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); What value ends up in the variable “answer” ? Lecture 2
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! Lecture 2
Constants • If data doesn’t change throughout program, use constant • Helpful to name this valuefinal int CS100_STUDENTS = 78;final double PI = 3.14159; • Use uppercase to distinguish from variables, whose values do change throughout the program Lecture 2
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” Lecture 2
Conditional Statement -- If • Conditional statements allows a choice of a command based on some condition// Store the maximum of x and y in zz = x;if (y > x) z = y; • Syntax: if (boolean expression) statement Lecture 2
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 zif (y != x) { x = 0; z = y; }OR// if x != y, store 0 in x, store y in zif (y != x) { x = 0; z = y; } Lecture 2
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 Lecture 2
Second Form of if statement • Provide multiple options: // Store maximum of x and y in zif (x >= y) { z = x; }else { z = y; } Lecture 2
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.”); Lecture 2
Operator Syntax • = = equal to • ! = not equal to • < less than • > greater than • < = less than or equal to • > = greater than or equal to Lecture 2
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 Lecture 2
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 Lecture 2
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 Lecture 2
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); Lecture 2
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 Lecture 2
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 Lecture 2
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? Lecture 2