Skip to content

Commit 65bd830

Browse files
committed
push after week 2 seminar session
1 parent 977c012 commit 65bd830

File tree

8 files changed

+217
-1
lines changed

8 files changed

+217
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
out/
12
.idea
23
*.iml

week_1/SwitchDemo.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package week_1;
2+
3+
import java.util.Scanner;
4+
5+
public class SwitchDemo {
6+
7+
public static void main(String[] args) {
8+
9+
Scanner scanner = new Scanner(System.in);
10+
System.out.println("enter you membership level");
11+
12+
String s = scanner.nextLine();
13+
14+
switch(s) {
15+
case "platinum":
16+
System.out.println("super");
17+
break;
18+
case "gold":
19+
System.out.println("duper");
20+
break;
21+
case "silver":
22+
System.out.println("member");
23+
break;
24+
default:
25+
System.out.println("not found");
26+
}
27+
28+
// more or less the same as
29+
if (s.equalsIgnoreCase("platinum")){
30+
System.out.println("super");
31+
} else if (s.equalsIgnoreCase("gold")){
32+
System.out.println("duper");
33+
} else if (s.equalsIgnoreCase("silver")){
34+
System.out.println("member");
35+
} else {
36+
System.out.println("not found");
37+
}
38+
}
39+
}

week_1/TypeCastingDemo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public static void main(String[] args) {
2222
// need to explicitly cast this int "i" into the short "s2"
2323
s2 = (short) i;
2424

25+
long l = 122344l;
26+
float f = l;
27+
2528

2629
System.out.println(s);
2730

week_2/ArraysDemo.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package week_2;
2+
3+
public class ArraysDemo {
4+
5+
public static void main(String[] args) {
6+
7+
// create empty array
8+
int[] nums = new int[5];
9+
10+
// populate array
11+
nums[4] = 10;
12+
nums[1] = 20;
13+
nums[2] = 30;
14+
nums[3] = 40;
15+
nums[0] = 50;
16+
17+
// array indices start at zero
18+
System.out.println(nums[5]);
19+
20+
for(int i = 0; i < nums.length; i++){
21+
System.out.print(nums[i] + " - ");
22+
}
23+
24+
System.out.println();
25+
26+
// another way to declare an array
27+
int[] vals = {12, 34, 56, 78};
28+
29+
System.out.println(vals[2]);
30+
31+
// final loop to learn - for-each
32+
for(int trex : vals){ System.out.print(trex + " - ");}
33+
// create array of size 3
34+
char[] characters = new char[3];
35+
36+
characters[0] = 'z'; // put "z" in index 0
37+
characters[1] = 'm'; // put "m" in index 1
38+
characters[2] = 'w'; // put "w" in index 2
39+
40+
System.out.println(characters[1]);
41+
42+
if (characters[0] == 'z') {
43+
//...
44+
if (10 > 8){
45+
// ...
46+
} else {
47+
// ...
48+
}
49+
} else if (characters[0] == 'm'){
50+
// ..
51+
}
52+
}
53+
}

week_2/Challenge1.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package week_2;
2+
3+
import java.util.Scanner;
4+
5+
public class Challenge1 {
6+
7+
// Ask the user to enter two numbers.
8+
// Print which number is greater or if they are equal.
9+
public static void main(String[] args) {
10+
// create two vars to hold the user input
11+
int a;
12+
int b;
13+
String response = "";
14+
15+
// create Scanner to get user input
16+
Scanner input = new Scanner(System.in);
17+
18+
do {
19+
// give user instructions
20+
System.out.println("enter your first number: ");
21+
// assign the input from the scanner to the variables
22+
a = input.nextInt();
23+
System.out.println("enter your second number: ");
24+
b = input.nextInt();
25+
26+
if (a > b) {
27+
System.out.println(a + " is greater than " + b);
28+
} else if (b > a) {
29+
System.out.println(b + " is greater than " + a);
30+
} else {
31+
System.out.println(b + " and " + a + " are equal");
32+
}
33+
34+
System.out.println("would you like to do this again? (yes/no)");
35+
36+
response = input.next();
37+
38+
} while(response.equalsIgnoreCase("yes"));
39+
40+
System.out.println("done");
41+
}
42+
}

week_2/Challenge2.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package week_2;
2+
3+
public class Challenge2 {
4+
5+
// Print whether each number from 1 to 20 is even or odd using a `for` loop.
6+
7+
public static void main(String[] args) {
8+
9+
for(int i = 1; i < 21; i++){
10+
if(i % 2 == 0){
11+
System.out.println(i + " is even");
12+
} else {
13+
System.out.println(i + " is odd");
14+
}
15+
}
16+
}
17+
}

week_2/LoopsDemo.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package week_2;
2+
3+
public class LoopsDemo {
4+
public static void main(String[] args) {
5+
6+
// finite iteration
7+
//for (initialize counter; boolean condition; incrementer)
8+
9+
// for(int i = 100; i >= 0; i-=25){
10+
// System.out.println("i is currently: " + i);
11+
// i -=25;
12+
// }
13+
14+
// non-finite iteration
15+
int count = 0;
16+
// while (boolean condition is true) -> loop
17+
while(count < 100){
18+
19+
// make sure you have a way out
20+
count++;
21+
22+
if (count == 50){
23+
// break out of the nearest loop (or switch statement)
24+
break;
25+
}
26+
27+
if (count % 2 == 0){ // if count is even
28+
// forces an early iteration of the loop
29+
continue;
30+
}
31+
32+
System.out.println("true count = " + count);
33+
}
34+
35+
// count = 1;
36+
//
37+
// // do-while loop ensures the body of the loop
38+
// // wille execute at least once
39+
// do {
40+
// System.out.println("count = " + count);
41+
// count++;
42+
// } while(count < 10);
43+
44+
System.out.println("all done");
45+
}
46+
}

week_2/Week_2.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ do {
4747

4848
---
4949

50+
### Debugger
51+
52+
Discussion on using the debugger.
53+
54+
---
55+
5056
### Arrays
5157

5258
```java
@@ -81,17 +87,26 @@ System.out.println(list.size()); // 1
8187
## Quiz Questions
8288

8389
1. **What does the `%` operator do?**
90+
* it returns the remainder of a division operation
8491

85-
2. **What is the output of `while(false)`?**
92+
2. **What is the result of `while(false)`?**
93+
* this loop will not run - only runs when condition is true
8694

8795
3. **Which loop executes at least once, even if the condition is false?**
96+
* do-while loop will always execute the body of the loop at least once
8897

8998
4. **What’s the default value of an uninitialized `int` array element?**
99+
* zero - the default value of an int primitive is zero
90100

91101
5. **What’s the key difference between an array and an ArrayList?**
102+
*
92103

93104
6. **What does `break` do inside a loop or switch statement?**
105+
* it will immediately exit the loop or switch and go to the next line of code
106+
outside/below the loop (or switch)
94107

108+
7. **What does `continue` do inside a loop?**
109+
* `continue` will send the flow of execution directly back to the beginning of the loop
95110
---
96111

97112
## Coding Challenges

0 commit comments

Comments
 (0)