Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] array = new String[4];
// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one

array[0] = "Jameson";
array[1] = "Auberon";
array[2] = "Tina";
array[3] = "Josh";
// Get the value of the array at index 2

String index2 = array[2];
// Get the length of the array

int arrayLength = array.length;
// Iterate over the array using a traditional for loop and print out each item

for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
// Iterate over the array using a for-each loop and print out each item

for (String string : array) {
System.out.println(string);
}
/*
* Reminder!
*
Expand Down
32 changes: 23 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import java.util.ArrayList;
import java.util.Collections;

public class ListPractice {


public static void main(String[] args) {
// Create an empty ArrayList of Strings and assign it to a variable of type List

ArrayList<String> list = new ArrayList<String>();
// Add 3 elements to the list (OK to do one-by-one)

list.add("Jameson");
list.add("Josh");
list.add("Auberon");
// Print the element at index 1

System.out.println(list.get(1));
// Replace the element at index 1 with a new value
// (Do not insert a new value. The length of the list should not change)

list.set(1,"Tina");
// Insert a new element at index 0 (the length of the list will change)

list.add(0, "Susan");
// Check whether the list contains a certain string

String check = "Jameson";
if(list.contains(check)){
System.out.println("List contains " + check);
}else{
System.out.println("List does not contain " + check);
}
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

for (int i = 0; i < list.size(); i++) {
System.out.println("Index " + i + " is " + list.get(i));
}
// Sort the list using the Collections library

Collections.sort(list);
// Iterate over the list using a for-each loop
// Print each value on a second line

for (String i : list) {
System.out.println(i + "\n");
}
/*
* Usage tip!
*
Expand Down
41 changes: 30 additions & 11 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@

import java.util.HashMap;
import java.util.Map;

public class MapPractice {
public static void main(String[] args) {
// Create a HashMap with String keys and Integer values and
// assign it to a variable of type Map

Map<String, Integer> map = new HashMap<String, Integer>();
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

map.put("Jameson", 18);
map.put("Jace", 16);
map.put("Lexie", 17);
// Get the value associated with a given key in the Map

int value = map.get("Jameson");
// Find the size (number of key/value pairs) of the Map

int size = map.size();
// Replace the value associated with a given key (the size of the Map shoukld not change)

map.put("Jameson", 19);
// Check whether the Map contains a given key

String testKey = "Jameson";
if(map.containsKey(testKey)){
System.out.println("Map contains " + testKey);
}else{
System.out.println("Map does not contain " + testKey);
}
// Check whether the Map contains a given value

int testValue = 19;
if(map.containsValue(testValue)){
System.out.println("Map contains " + testValue);
}else{
System.out.println("Map does not contain " + testValue);
}
// Iterate over the keys of the Map, printing each key

for (String i : map.keySet()) {
System.out.println(i);
}
// Iterate over the values of the map, printing each value

for (int i : map.values()) {
System.out.println(i);
}
// Iterate over the entries in the map, printing each key and value

for (String i : map.keySet()) {
System.out.println("key: " + i + ", value: " + map.get(i));
}
/*
* Usage tip!
*
Expand Down
14 changes: 9 additions & 5 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float negativeNum = -0.5f;
// Create an int with a positive value and assign it to a variable

int positiveNum = 10;
// Use the modulo % operator to find the remainder when the int is divided by 3

System.out.println(positiveNum % 3);
// Use the modulo % operator to determine whether the number is even
// (A number is even if it has a remainder of zero when divided by 2)
// Use an if-else to print "Even" if the number is even and "Odd"
// if the number is odd.

if(positiveNum % 2 == 0){
System.out.println(positiveNum + " is Even");
}else{
System.out.println(positiveNum + " is Odd");
}
// Divide the number by another number using integer division

System.out.println(positiveNum / 5);
/*
* Reminder!
*
Expand Down
38 changes: 25 additions & 13 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
public class Person {
// Declare a public String instance variable for the name of the person
// Declare a private int instance variable for the age of the person

public String name;
private int age;

// Create a constructor that takes the name and age of the person
// and assigns it to the instance variables

public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Create a toString method that gives the name and age of the person

public String toString() {
return "Name: " + name + " Age: " + age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,39 +35,44 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)

public int birthYear(int currentYear) {
return currentYear - age;
}

public static void main(String[] args) {
// Create an instance of Person

Person person = new Person("Jameson", 18);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person person2 = new Person("Lexie", 17);
// Print the first person

person.toString();
// Print the second person

person2.toString();
// Get the name of the first person and store it in a local variable

String name = person.name;
// Using the birthYear method, get the birth year of the first person
// and store it in a local variable. Input the actual current year (e.g. 2025)
// as the argument.

int birthyear = person.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.

System.out.println(birthyear);
/**
* Terminology!
*
* A class is the overall definition, like a blueprint.
* An instance is a specific object made according to that definition.
* We use "instance" and "object" to mean the same thing.
*
* For example, if there is a Person class, we can make an instance of a specific person: Auberon.
* For example, if there is a Person class, we can make an instance of a
* specific person: Auberon.
*
* There can be many instances for the same class. For example: Auberon, Xinting, Baya are all
* There can be many instances for the same class. For example: Auberon,
* Xinting, Baya are all
* different instances of the Person class.
*
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
* Each instance has its own instance variables: Auberon's age can be different
* from Baya's age.
*/
}
}
24 changes: 18 additions & 6 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import java.util.HashSet;
import java.util.Set;

public class SetPractice {
public static void main(String[] args) {
// Create a HashSet of Strings and assign it to a variable of type Set

Set<String> set = new HashSet<>();
// Add 3 elements to the set
// (It's OK to do it one-by-one)

set.add("Jameson");
set.add("Jace");
set.add("Lexie");
// Check whether the Set contains a given String

String test = "Jameson";
if(set.contains(test)){
System.out.println("Set contains " + test);
}else{
System.out.println("Set does not contain " + test);
}
// Remove an element from the Set

set.remove("Jace");
// Get the size of the Set

System.out.println(set.size());
// Iterate over the elements of the Set, printing each one on a separate line

for (String i : set) {
System.out.println(i);
}
/*
* Warning!
*
Expand Down
39 changes: 29 additions & 10 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import java.util.ArrayList;
import java.util.List;

public class StringPractice {
public static void main(String[] args) {
// Create a string with at least 5 characters and assign it to a variable

String word = "Jameson";
// Find the length of the string

System.out.println(word.length());
// Concatenate (add) two strings together and reassign the result

word = word + " Farmer";
// Find the value of the character at index 3

System.out.println(word.charAt(3));
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

String test = "James";
if(word.contains(test)){
System.out.println(word + " contains " + test);
}else{
System.out.println(word + " does not contain " + test);
}
// Iterate over the characters of the string, printing each one on a separate line

for (int i = 0; i < word.length(); i++) {
System.out.println(word.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

List<String> list = new ArrayList<>();
// Add multiple strings to the List (OK to do one-by-one)

list.add("Tina");
list.add("Josh");
list.add("Auberon");
// Join all of the strings in the list together into a single string separated by commas
// Use a built-in method to achieve this instead of using a loop

String combined = String.join(", ", list);
System.out.println(combined);
// Check whether two strings are equal

String test1 = "Jameson";
String test2 = "James";
if(test1.equals(test2)){
System.out.println(test1 + " equals " + test2);
}else{
System.out.println(test1 + " does not equal " + test2);
}
/*
* Reminder!
*
Expand Down