Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100Lecture 21 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.

Similar presentations


Presentation on theme: "CS100Lecture 21 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."— Presentation transcript:

1 CS100Lecture 21 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

2 CS100Lecture 22 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

3 CS100Lecture 23 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?

4 CS100Lecture 24 Think of a variable as a box into which a value can be placed Declarations: int x; int timeOfDay Introduction to Variables 32 x-532 timeOfDay 1115

5 CS100Lecture 25 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

6 CS100Lecture 26 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

7 CS100Lecture 27 int timeOfDay = 3; // timeOfDay timeOfDay = 430; // timeOfDay timeOfDay = 30 + timeOfDay + 5; // timeOfDay = 465 Assignment 3 430 465

8 CS100Lecture 28 Assignment Syntax = ; 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

9 CS100Lecture 29 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” ?

10 CS100Lecture 210 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!

11 CS100Lecture 211 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

12 CS100Lecture 212 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”

13 CS100Lecture 213 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

14 CS100Lecture 214 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; }

15 CS100Lecture 215 More on block statements Consider: { …. } { 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

16 CS100Lecture 216 Second Form of if statement Provide multiple options: // Store maximum of x and y in z if (x >= y) { z = x; } else { z = y; }

17 CS100Lecture 217 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.”);

18 CS100Lecture 218 Operator Syntax = = equal to ! = not equal to < less than > greater than < = less than or equal to > = greater than or equal to

19 CS100Lecture 219 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

20 CS100Lecture 220 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

21 CS100Lecture 221 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

22 CS100Lecture 222 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);

23 CS100Lecture 223 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

24 CS100Lecture 224 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 NameDOB –“foo\n\nbar” results in foo bar

25 CS100Lecture 225 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?


Download ppt "CS100Lecture 21 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."

Similar presentations


Ads by Google