Java Basics SP17 ICS 111 Ed Meyer
Assignment 01 Write a Java program that: Prints a 2-line computer-related joke or a 4-line poem Uses print and println at least once Follows the Coding Standard Full details in Digital Dropbox Due Tues. Jan. 24 by 11:55pm Submit only the.java file 2
Quiz 01 Due Wed. Jan. 25 by 11:55pm Password: quiz01 15 Questions Multiple choice, True/False Based on lectures: Week 01 Day 02 Week 02 Day 01 and Day 02 (today) Attempts: 1 Time: 20 minutes 3
Last Time Print complex messages Escape Sequences \ Concatenation + Programming concepts Variables Constants Data types Declaring vs Initializing 4
Today Using String variables Arithmetic Expressions Assignment and Comparisons 5
String Variables What is a String? is a sequence of characters or words It is NOT a primitive data type Made up of characters 6
Breaking Down a String String message = "It's over 9,000!"; I t ' s o v e r 9, 0 0 0! The string is 16 characters long. 7
More on Strings Strings are very commonly used They are special They have object properties They have primitive type properties Can be declared like a primitive data type 8
Strings are Useful Help us label output Give instructions to the user Build more complex strings using concatenation 9
The String Data Type It is a Java object String JavaDoc It may be declared As a Java object As a primitive data type 10
Declaring and Initializing a String As you would expect from previous examples String message = "I choose you, Pikachu!"; Declaring and initializing as a primitive data type prim_data_type var_name = value; 11
Printing a Previously Declared String String message = "I choose you, Pikachu!"; // Pretend there is more code here System.out.println(message); Notice you don't need the quotes when you use the variable name. 12
When do you put the quote? If you want to use a string that is inside a variable No quotes are needed, just use the variable name If you are hard-coding a message You need to quote the message If you are initializing a string variable You need to quote the message 13
Printing Two Strings String location = "a bottle"; System.out.print("This message was found in"); System.out.println(location); This message was found ina bottle 14
Printing Two Strings String location = "a bottle"; System.out.print("This message was found in "); System.out.println(location); This message was found in a bottle 15
Arithmetic Expressions 16
Arithmetic Expressions add + subtract multiply * divide / modulus % Asterisk: Shift + 8 Follows the PEMDAS rule as you would expect Modulus has same priority as * and / 17
Adding Example int i = 20; int j = 5; int total = 0; total = i + j; These are declaration and initializing statements. 18
Adding Example int i = 20; int j = 5; int total = 0; total = i + j; This is an assignment statement. Take the thing on the right, and store it in the left. 19
More about Modulus % Calculate the remainder of a division Examples: 23 % 5 =? 20
More about Modulus % Calculate the remainder of a division Examples: 23 % 5 = 3 17 % 3 =? 21
More about Modulus % Calculate the remainder of a division Examples: 23 % 5 = 3 17 % 3 = 2 16 % 4 =? 22
More about Modulus % Calculate the remainder of a division Examples: 23 % 5 = 3 17 % 3 = 2 16 % 4 = 0 23
Assignment, Comparisons, and Output
Why Declare All Your Variables Because Java says... If you use the same piece of data more than once, declaring a variable is very useful. Declare all variables at the beginning Set aside all the boxes you will need in the program 25
Declaration Example int i = 20; int j = 5; int resulta = 0; int results = 0; int resultm = 0; int resultd = 0; You can reuse the same code with different values of i and j. resulta = i + j; results = i j; resultm = i * j; resultd = i / j; 26
Comparisons If we compare two items, the result will be true or false Boolean! Primitive data type: boolean 27
Boolean Operators Comparison Symbol Greater than > Less than < Greater than or Equal to >= Less than or Equal to <= Equal to == (double equals) 28
Comparison Example int x = 7; int y = 19; boolean isgreater = true; boolean issmaller = false; isgreater = x > y; issmaller = x < y; 30
Assignment and Comparison In programming, = does not mean "is equal to" = means assignment Take the thing on the right, and put it in the left == means "is equal?" It is a question It compares and gives a Boolean result 31
Assignment and Comparison Example int x = 5; int y = 20; int total = 0; boolean isequal = true; total = x + y; isequal = x == y; 32
Assignment and Comparison Example isequal = x == y; Evaluate the stuff on the right first Take the thing on the right, and put it in the left 33
What will print? String output = ""; output = output + "Hi, my name is"; output = output + "Ed"; System.out.print(output); Hi, my name ised 34
More on System.out A program is worthless without feedback print and println have parameters Parameters are things inside parenthesis ( ) Information required to execute an action System.out.println() will print a blank line System.out.print() will throw an error If there is nothing in the parenthesis Java expects a parameter to print 35
A Bad Example int x = 5; int y = 20; int total = 0; boolean isequal = true; total = x + y; isequal = x == y; System.out.println(total); System.out.println(isEqual); 36
Let's Do Some Examples 37