CS 302 - Week 2 Jim Williams, PhD
This Week Team Labs Lecture: Scanner, data types, conditionals
Team Labs First meeting this week. 1350 cs and 1370 cs Meet TAs & LAs, expect an Ice Breaker, be assigned a partner and do the lab together. Pair programming
Pair Programming 2 people working together on 1 computer. One person types, the other provides direction and reviews. Many report more confidence in solution and more enjoyment programming. Import to switch roles (who has the keyboard). Provide respectful, honest and friendly feedback
P1 Available P1 - Individual work Style and Commenting Discuss concepts, don’t share assignment code Style and Commenting
Piazza notes Don't post your code publicly (working or not). You can post code from the book if you cite it. From section 2.3.1, ... Review the other posts (search for common terms) before posting as your question may already have been answered. Refer to these posts along with your question and say how yours is different.
Read In Values Recall: import java.util.Scanner; Scanner input = new Scanner( System.in); int age = input.nextInt(); String name = input.nextLine();
What are values of variables? name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: name: Minsub age: 22 String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();
What are values of variables? name: Minsub\n22\CS age: major: name: Minsub age: 22 name: Minsub major: CS String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();
What are values of variables? score1: 20 score2: 25 title: scores title: score1: 20 25 score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();
Questions (Degrees Fahrenheit – 32) x 5 / 9 = Degrees Celsius What symbols have different meanings in Java? What changes must be made to implement this equation in Java? Retrieval practice importance of committing to an answer
Eclipse IDE Opening project, copying in files Style and Commenting Guides Strings I/O Calling methods Compiler & Runtime Errors Scanner reading a number
Review (Degrees Fahrenheit – 32) x 5 / 9 = Degrees Celsius What symbols have different meanings in Java? What changes must be made to implement this equation in Java?
My List X vs * equals (==) vs assignment (=) value is stored on the left hand side of assignment (=) operator Variables: name areas of computer memory, declare before use, declare type of data, initialize Variable names: start with letter, include letters numbers and _, but no spaces Conventions: camelCasing, spell out names Semicolon at the end of statements
Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1
Decimal 100 10 1 3 0 2 = 302
Binary 8 4 2 1 1 1 1 0 = 14
Binary 8 4 2 1 0 1 0 1 = 5
Names for Numbers of Bits 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 bit nibble (4 bits) byte (8 bits) 2 bytes (16)
Hexadecimal (group bits by nibble) 0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F
What character is this? 0000 0000 0100 0001 @ A Unicode: B C Unicode: 0x003E 62 > 0x003F 63 ? 0x0040 64 @ 0x0041 65 A 0x0042 66 B 0x0043 67 C http://www.ssec.wisc.edu/~tomw/java/unicode.html
Color 183, 1, 1 B70101 Red, Green, Blue (RGB) 183, 1, 1 B70101 Red, Green, Blue (RGB) https://umark.wisc.edu/brand/web/colors.php
Primitive Data Types int whole numbers (4 bytes) double floating point numbers (8 bytes) char single character (2 bytes) boolean true or false value (>= 1 bit) less commonly used: float, long, short, byte
boolean operators (comparison) == != < <= > >= int n = 6; boolean result = n != 5; result: true result: false
Primitive vs Reference Data Types Store value Reference Store a reference to place in memory to find value
java.util.Random Random randGen; //Declare reference variable randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);
Calling String methods answer:true answer:false String strA = "This is a string"; char aChar = strA.charAt( 3); boolean answer; answer = aChar == 'i';
java.lang.String String strA = "This is a string"; //String literal int lengthA = strA.length(); int lengthB = "This is a string".length();
Values of a1 and a2? String str1 = "Hello"; a1: true a2: true a1: false a2: false String str1 = "Hello"; String str2 = new String( "Hello"); boolean a1 = str1 == str2; boolean a2 = str1.equals(str2);
Values of a1 and a2? char ch1 = 'H'; String str = "Hello"; a1: true a2: true a1: false a2: false char ch1 = 'H'; String str = "Hello"; char ch2 = str.charAt(0); boolean a1 = ch1 == ch2; boolean a2 = ch1.equals(ch2);
Calling java.lang.Math methods double max( double a,double b) float max( float a, float b) int max(int a, int b) double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int age1 = 5; int age2 = 25; double big = Math.max( age1, age2);
Are these equivalent? boolean tired = true; if ( tired) { Same result in all cases Different result sometimes Error boolean tired = true; if ( tired) { //take break tired = false; } if ( !tired) { //keep working boolean tired = true; if ( tired) { //take break tired = false; } else { //keep working }
Floating Down a River http://www.thehiltonorlando.com/discover/pools-and-lazy-river/
Side Trip, maybe multiple side trips boolean tired = true; if ( tired) { System.out.println(“take break”); }
One side of the Island or the other boolean sunny = false; if ( sunny) { System.out.print(“sunny”); } else { System.out.print(“not sunny”); } System.out.println( “ and back together”);
Equivalent? Yes No char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } else if ( chr == 'B') { out = 'Y'; } else { out = 'Z'; } char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } if ( chr == 'B') { out = 'Y'; } if ( chr != 'A' || chr != 'B') { out = 'Z'; } Yes No
Dangling Else - What is out? int value = 0; String out = "abc"; if ( value >= 1 ) if ( value <= 10) out = "1 to 10"; else out = "else"; out: abc out: 1 to 10 out: else out: abcelse
What is the value of msg? boolean flag = true; String msg = "before if"; if ( flag = false); { msg = "" + flag; } before if true false