Young Coders: Week 1 Notes
We started out by learning how to declare and initialize a variable in Java:
We then looked at if statements:
Using this, we wrote the following program that classifies a person according to their age:
public class Age {
public static void main(String[] args) {
int age = 114;
if(age <= 0)
System.out.println("nonexistent");
else if(age > 0 && age < 2)
System.out.println("baby");
else if(age >= 2 && age <= 5)
System.out.println("toddler");
else if(age > 5 && age <= 12)
System.out.println("in elementary school");
else if(age > 12 && age <= 15)
System.out.println("middle schooler");
else if(age > 15 && age <= 18)
System.out.println("high schooler");
else if(age > 18 && age <= 22)
System.out.println("college");
else
System.out.println("adult");
}
}
We realized we couldn't write else if(0 < age < 2). Instead, we have to break it down into else if(age > 0 && age < 2). The && means "and", so both the conditions age > 0 AND age < 2 must be true in order for "baby" to be printed out.
&& is called a "boolean operator". Here are some more boolean operators:
|| means "or"
== means "equals"
!= means "not equal"
We can use &&, ||, and == to write this program:
public class Birthday {
public static void main(String[] args) {
boolean birthday = false;
int temperature = 100;
if(birthday == true || (temperature > 60 && temperature < 100))
System.out.println("happy");
else
System.out.println("not happy");
}
}
We did three problems in class. I'll include solutions to these at the end of this post in case you want to try solving them again:
1) Create three integer variables. If at
least one of them is negative, print
out “found negative”. Otherwise,
print out “all positive.”
2) Create two integer variables. Print out whichever one is closer to 13. If they are equally close, print out “equally close”.
3) Create three integer variables. Print “ascending” if they are in ascending order (e.g. 1,2,3 but not 4,4,6) and “descending” if they are in descending order (e.g. 3,2,1 but not 6,6,4). Otherwise, don’t print anything.
Next, we looked at Strings. Strings hold a sequence of characters. Examples are "banana" or "This is a sentence. This is another sentence." Below is a list of things we covered:
Finally, we looked at Scanners. The Scanner class lets the user input things into your program.
import java.util.Scanner; //don't forget this!
public class ScannerExample {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
System.out.println(input);
}
}
One more example with a Scanner:
2) Create two integer variables. Print out whichever one is closer to 13. If they are equally close, print out “equally close”.
3) Create three integer variables. Print “ascending” if they are in ascending order (e.g. 1,2,3 but not 4,4,6) and “descending” if they are in descending order (e.g. 3,2,1 but not 6,6,4). Otherwise, don’t print anything.
Next, we looked at Strings. Strings hold a sequence of characters. Examples are "banana" or "This is a sentence. This is another sentence." Below is a list of things we covered:
Declaring a String
String str = "Abraham Lincoln";
String empty = "";
String concatenation
String word1 = "Not";
String word2 = "so";
String word3 = "Boston.";
System.out.println(word1 + " " + word2+ " " + word3);
//this prints out "Not so Boston."
String str = "Abraham Lincoln";
String empty = "";
String concatenation
String word1 = "Not";
String word2 = "so";
String word3 = "Boston.";
System.out.println(word1 + " " + word2+ " " + word3);
//this prints out "Not so Boston."
Length
word.length()
String comparison
String str1 = "pop";
String str2 = "pop";
String str3 = "dog";
str1.equals(str2) is true
str1.equals(str3) is false
Note: Remember not to use == when comparing Strings!
String a = "aardvark";
System.out.println(a.compareTo("zephyr")); //negative System.out.println(a.compareTo("aardvark")); //0 System.out.println(a.compareTo("aaaaaaaah!")); //positive
word.length()
String comparison
String str1 = "pop";
String str2 = "pop";
String str3 = "dog";
str1.equals(str2) is true
str1.equals(str3) is false
Note: Remember not to use == when comparing Strings!
String a = "aardvark";
System.out.println(a.compareTo("zephyr")); //negative System.out.println(a.compareTo("aardvark")); //0 System.out.println(a.compareTo("aaaaaaaah!")); //positive
Substrings
includes first index, goes up to last index, doesn't include last index
String word = "pickles";
String part1 = word.substring(0,3); //part1 is pic
String part2 = word.substring(3); //part2 is kles
String part3 = word.substring(2, 5); //part3 is ckl
charAt
String example = "oops";
System.out.println(example.charAt(0)); //o System.out.println(example.charAt(1)); //o System.out.println(example.charAt(2)); //p System.out.println(example.charAt(3)); //s
includes first index, goes up to last index, doesn't include last index
String word = "pickles";
String part1 = word.substring(0,3); //part1 is pic
String part2 = word.substring(3); //part2 is kles
String part3 = word.substring(2, 5); //part3 is ckl
charAt
String example = "oops";
System.out.println(example.charAt(0)); //o System.out.println(example.charAt(1)); //o System.out.println(example.charAt(2)); //p System.out.println(example.charAt(3)); //s
indexOf
String bigword = "Pneumonoultramicroscopicsilicovolcanoconiosis";
System.out.println(bigword.indexOf("ultra")); //this returns 8
String bigword = "Pneumonoultramicroscopicsilicovolcanoconiosis";
System.out.println(bigword.indexOf("ultra")); //this returns 8
Finally, we looked at Scanners. The Scanner class lets the user input things into your program.
import java.util.Scanner; //don't forget this!
public class ScannerExample {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
System.out.println(input);
}
}
One more example with a Scanner:
import java.util.Scanner;
public class HelloThere {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name = s.nextLine();
System.out.println("Hello, " + name + "!");
}
}
We ran out of time before finishing some problems, so those will become Candy Challenges.
Hope you all enjoyed today! Same time, same place next week: Sunday, 1-3 pm. We will cover loops, arrays, methods, and recursion.