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
18 changes: 15 additions & 3 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] shoppingList = 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

shoppingList[0] = "Bread";
shoppingList[1] = "Milk";
shoppingList[2] = "Cheese";
shoppingList[3] = "Banana";

// Get the value of the array at index 2
System.out.println(shoppingList[2]);

// Get the length of the array

// Iterate over the array using a traditional for loop and print out each item
System.out.println("Length of array: " + shoppingList.length);

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

for(String str: shoppingList){
System.out.println(str);
}
/*
* Reminder!
*
Expand Down
22 changes: 20 additions & 2 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
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> myList = new ArrayList<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, use interface types (List, Map, etc.) where appropriate.
For example:
List<String> strings = new ArrayList<>();
Note that the type on the left is List, not ArrayList. When we do this, we're more flexible to be able to change our code to use a different type of list later.

Similarly for maps:
Map<String, String> myMap = new HashMap<>();
Note that on the left we use Map instead of HashMap.

And sets:
Set<String> strings = new HashSet<>();
Note that on the left we use Set instead of HashSet.

In summary:

  • interface type on left to declare type (List, Map, etc.)
  • Concrete type on right to instantiate instance (HashMap, ArrayList etc.)


// Add 3 elements to the list (OK to do one-by-one)

myList.add("Apple");
myList.add("Banana");
myList.add("Grapes");
// Print the element at index 1
System.out.println(myList.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)
myList.set(1,"Blueberry");

// Insert a new element at index 0 (the length of the list will change)
myList.add(0,"Raspberry");

// Check whether the list contains a certain string

System.out.println("Does myList contain Pear? " + myList.contains("Pear"));
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

for(int i = 0; i < myList.size(); i++){
System.out.println("myList index " + i + ": " + myList.get(i));
}

// Sort the list using the Collections library
Collections.sort(myList);

// Iterate over the list using a for-each loop
// Print each value on a second line

for (String str : myList) {
System.out.println(str);
}

/*
* Usage tip!
*
Expand Down
24 changes: 20 additions & 4 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@

import java.util.HashMap;

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
HashMap<String,Integer> myHashMap = new HashMap<>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
myHashMap.put("Six", 3);
myHashMap.put("Three", 5);
myHashMap.put("Four", 4);

// Get the value associated with a given key in the Map

myHashMap.get("Six");
// Find the size (number of key/value pairs) of the Map

System.out.println("Size of hashmap: " + myHashMap.size());
// Replace the value associated with a given key (the size of the Map shoukld not change)

myHashMap.put("Six", 6);
// Check whether the Map contains a given key
System.out.println("Does myHashmap containsKey Four? " + myHashMap.containsKey("Four"));

// Check whether the Map contains a given value
System.out.println("Does myHashmap containsValue 9 " + myHashMap.containsValue(9));

// Iterate over the keys of the Map, printing each key

// Iterate over the keys of the Map, printing each key
for(String s: myHashMap.keySet()){
System.out.println("Key: " + s);
}
// Iterate over the values of the map, printing each value
for(int i: myHashMap.values()){
System.out.println("Value: " + i);
}

// Iterate over the entries in the map, printing each key and value
for(String s: myHashMap.keySet()){
System.out.println("Key: " + s + ", Value: " + myHashMap.get(s));
}

/*
* Usage tip!
Expand Down
17 changes: 15 additions & 2 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float myFloat = -1.2f;
System.out.println("my float: " + myFloat);
// Create an int with a positive value and assign it to a variable

int number = 20;
System.out.println("number: " + number);
// Use the modulo % operator to find the remainder when the int is divided by 3
int remainder = number % 3;
System.out.println("20 % 3 = " +remainder);

// 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(number % 2 == 0){
System.out.println(number + " is even!");
}else{
System.out.println(number + " is odd!");
}

// Divide the number by another number using integer division

/*
Expand All @@ -21,5 +31,8 @@ public static void main(String args[]) {
* 7 / 3 = 2 when performing int division
*/

int solution = number / 4;
System.out.println(number + " / 4 = " + solution);

}
}
33 changes: 28 additions & 5 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
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

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 @@ -29,25 +35,42 @@ public class Person {
*/
// (create the instance method here)

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

public String getName(){
return name;
}


public static void main(String[] args) {
// Create an instance of Person
Person person1 = new Person("Leo", 20);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Joshua", 40);

// Print the first person

// Print the second person
System.out.println(person1);

// Print the second person
System.out.println(person2);
// Get the name of the first person and store it in a local variable

String person2Name = person2.getName();
System.out.println(person2Name);
// 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 person1Age = person1.birthYear(2025);

// In a separate statement, print the local variable holding the birth year.

System.out.println(person1Age);

/**
* Terminology!
*
Expand Down
16 changes: 13 additions & 3 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import java.util.HashSet;

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

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

fruits.add("Kiwi");
fruits.add("Blackberry");
fruits.add("Tomato");
// Check whether the Set contains a given String

System.out.println("Does fruit set contain Banana?" + fruits.contains("Banana"));
// Remove an element from the Set

fruits.remove("kiwi");
// Get the size of the Set

int sizeOfSet = fruits.size();
System.out.println(sizeOfSet);
// Iterate over the elements of the Set, printing each one on a separate line
for (String fruit : fruits) {
System.out.println(fruit);
}

/*
* Warning!
Expand Down
26 changes: 25 additions & 1 deletion src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
import java.util.ArrayList;

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

// Find the length of the string
int fruitLength = fruit.length();
System.out.println(fruitLength);

// Concatenate (add) two strings together and reassign the result
String drink = "milk";
String result = fruit + drink;
System.out.println(result);

// Find the value of the character at index 3
String letter = result.substring(3,4);
System.out.println(letter);

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
System.out.println("Does result have milk? " + result.contains("milk"));

// Iterate over the characters of the string, printing each one on a separate line
for(char c : result.toCharArray()){
System.out.println(c);
}

// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> myList = new ArrayList<String>();


// Add multiple strings to the List (OK to do one-by-one)
myList.add("Leo");
myList.add("Will");
myList.add("Joshua");
myList.add("Jadon");


// 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 combinedString = String.join(",", myList);

System.out.println(combinedString);
// Check whether two strings are equal

System.err.println("Does Leo equal to Will? " + "Leo".equals("Will"));

/*
* Reminder!
*
Expand Down
2 changes: 2 additions & 0 deletions toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

As you work through this exercise, write down anything that you needed to look up or struggled to remember here. It can be just a word or two (e.g. "joining strings"). You can use this as a guide of what to make extra sure you're refreshed on before exams and interviews.



-