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
13 changes: 13 additions & 0 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] pets = 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
pets[0] = "cat";
pets[1] = "dog";
pets[2] = "parrot";
pets[3] = "hamster";

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

// Get the length of the array
System.out.println(pets.length);

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

// Iterate over the array using a for-each loop and print out each item
for (String pet : pets){
System.out.println(pet);
}

/*
* Reminder!
Expand Down
19 changes: 19 additions & 0 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
import java.util.*;
public class ListPractice {


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

// Add 3 elements to the list (OK to do one-by-one)
fruits.add("Apple");
fruits.add("Pear");
fruits.add("Grape");

// Print the element at index 1
System.out.println(fruits.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)
fruits.set(1, "Banana");
System.out.println(fruits.get(1));

// Insert a new element at index 0 (the length of the list will change)
fruits.add(0, "Pinapple");
System.out.println(fruits);

// Check whether the list contains a certain string
System.out.println(fruits.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 < fruits.size(); i++){
System.out.println("Index: " + i + ", Fruit: " + fruits.get(i));
}


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

// Iterate over the list using a for-each loop
// Print each value on a second line
for (String fruit : fruits){
System.out.println(fruit);
}

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

import java.util.*;

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

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
orders.put("Tea", 3);
orders.put("Coffee", 1);
orders.put("Smoothie", 1);

// Get the value associated with a given key in the Map
System.out.println(orders.get("Smoothie"));

// Find the size (number of key/value pairs) of the Map
System.out.println(orders.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
orders.put("Tea", 5);
System.out.println(orders);

// Check whether the Map contains a given key
System.out.println(orders.containsKey("Coffee"));

// Check whether the Map contains a given value
System.out.println(orders.containsValue(3));

// Iterate over the keys of the Map, printing each key
for (int quantity : orders.values()){
System.out.println(quantity);
}

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

// Iterate over the entries in the map, printing each key and value
for (Map.Entry<String, Integer> order : orders.entrySet()){
System.out.println(order.getKey() + ": " + order.getValue());
}

/*
* Usage tip!
Expand Down
14 changes: 12 additions & 2 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float floatNum = -1.12f;

// Create an int with a positive value and assign it to a variable
int intNum = 9;

// Use the modulo % operator to find the remainder when the int is divided by 3

int remainder = intNum % 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.
String evenOrOdd = "";
if (intNum % 2 == 0){
evenOrOdd = "Number is even";
} else {
evenOrOdd = "Number is odd";
}

// Divide the number by another number using integer division

/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* 7 / 3 = 2 when performing int division
*/
int divided = intNum / 2;

System.out.println(floatNum + ", " + intNum + ", " + remainder + ", " + evenOrOdd + ", " + divided );
}
}
23 changes: 19 additions & 4 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

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,25 +34,34 @@ 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("Bob", 13);

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

// Print the first person
System.out.println(person);

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

// Get the name of the first person and store it in a local variable
String personName = 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 personBirthYear = person.birthYear(2025);

// In a separate statement, print the local variable holding the birth year.
System.out.println("Name: " + personName + ", Birth Year: " + personBirthYear);

/**
* Terminology!
Expand Down
13 changes: 13 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import java.util.*;
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> instruments = new HashSet<String>();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Remember to use interface types (Set)



// Add 3 elements to the set
// (It's OK to do it one-by-one)
instruments.add("piano");
instruments.add("guitar");
instruments.add("violin");

// Check whether the Set contains a given String
System.out.println(instruments.contains("violin"));

// Remove an element from the Set
instruments.remove("guitar");
System.out.println(instruments);

// Get the size of the Set
System.out.println(instruments.size());

// Iterate over the elements of the Set, printing each one on a separate line
for (String instrument : instruments){
System.out.println(instrument);
}

/*
* Warning!
Expand Down
21 changes: 21 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import java.util.*;
public class StringPractice {
public static void main(String[] args) {
// Create a string with at least 5 characters and assign it to a variable
String name = "eliza";

// Find the length of the string
System.out.println(name.length());

// Concatenate (add) two strings together and reassign the result
String name2 = "beth";
name = name + name2;
System.out.println(name);

// Find the value of the character at index 3
System.out.println(name.charAt(3));

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

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

// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> lastName = new ArrayList<String>();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Remember to use interface types (List)


// Add multiple strings to the List (OK to do one-by-one)
lastName.add("Potter");
lastName.add("Granger");
lastName.add("Weasley");
lastName.add("Malfoy");

// 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 words = String.join(", ", lastName);
String lastNameString = String.join(", ", lastName);
String words = "Potter, Granger, Weasley, Malfoy";
System.out.println(lastNameString);

// Check whether two strings are equal
System.out.println(lastNameString.equals(words));

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

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.

Looked up how float number is being defined.
Looked up how to iterate through a string using a for each loop.
Looked up how to iterate trhoug a HashMap using a for each loop.
-