Download presentation
Presentation is loading. Please wait.
1
CS Week 2 Jim Williams, PhD
2
This Week Team Labs Lecture: Scanner, data types, conditionals
3
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
4
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
5
P1 Available P1 - Individual work Style and Commenting
Discuss concepts, don’t share assignment code Style and Commenting
6
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.
7
Read In Values Recall: import java.util.Scanner;
Scanner input = new Scanner( System.in); int age = input.nextInt(); String name = input.nextLine();
8
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();
9
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();
10
What are values of variables?
score1: 20 score2: 25 title: scores title: score1: score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();
11
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
12
Eclipse IDE Opening project, copying in files
Style and Commenting Guides Strings I/O Calling methods Compiler & Runtime Errors Scanner reading a number
13
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?
14
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
15
Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1
16
Decimal = 302
17
Binary = 14
18
Binary 8 4 2 1 = 5
19
Names for Numbers of Bits
bit nibble (4 bits) byte (8 bits) 2 bytes (16)
20
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
21
What character is this? 0000 0000 0100 0001 @ A Unicode:
B C Unicode: 0x003E 62 > 0x003F 63 ? 0x @ 0x A 0x B 0x C
22
Color 183, 1, 1 B70101 Red, Green, Blue (RGB)
183, 1, 1 B70101 Red, Green, Blue (RGB)
23
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
24
boolean operators (comparison)
== != < <= > >= int n = 6; boolean result = n != 5; result: true result: false
25
Primitive vs Reference Data Types
Store value Reference Store a reference to place in memory to find value
26
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);
27
Calling String methods
answer:true answer:false String strA = "This is a string"; char aChar = strA.charAt( 3); boolean answer; answer = aChar == 'i';
28
java.lang.String String strA = "This is a string"; //String literal
int lengthA = strA.length(); int lengthB = "This is a string".length();
29
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);
30
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);
31
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);
32
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 }
33
Floating Down a River
34
Side Trip, maybe multiple side trips
boolean tired = true; if ( tired) { System.out.println(“take break”); }
35
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”);
36
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
37
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
38
What is the value of msg? boolean flag = true;
String msg = "before if"; if ( flag = false); { msg = "" + flag; } before if true false
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.