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: 17 additions & 1 deletion src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
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] = "Dog";
array[1] = "Cat";
array[2] = "Duck";
array[3] = "Wombat";

// Get the value of the array at index 2
String index2Value = array[2];
System.out.println("Index 2 contains the word: " + index2Value);

// Get the length of the array
int arrayLength = array.length;
System.out.println("Length of the array: " + arrayLength);

// Iterate over the array using a traditional for loop and print out each item
System.out.println("FOR-LOOP:");
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

System.out.println("FOR-EACH LOOP: ");
for (String word : array) {
System.out.println(word);
}
/*
* Reminder!
*
Expand Down
23 changes: 23 additions & 0 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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> list = new ArrayList<>();

// Add 3 elements to the list (OK to do one-by-one)
list.add("Dog");
list.add("Cat");
list.add("Duck");

// Print the element at index 1
System.out.println("Element at index 1 is: " + 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, "Wombat");

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

// Check whether the list contains a certain string
boolean containsCat = list.contains("Cat");
System.out.println("Does the list contain the word 'Cat'? " + containsCat); //false since the word was replaced

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line
System.out.println("FOR-LOOP:");
for (int i = 0; i < list.size(); i++) {
System.out.println("Index " + i + ": " + 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
System.out.println("FOR-EACH LOOP:");
for (String word : list) {
System.out.println(word);
}

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

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


// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
maps.put("coin", 2);
maps.put("battery", 5);
maps.put("chair", 12);

// Get the value associated with a given key in the Map
int batteryKey = maps.get("battery");
System.out.println("Value for battery: " + batteryKey);

// Find the size (number of key/value pairs) of the Map
int mapSize = maps.size();
System.out.println("The Map size is: " + mapSize);

// Replace the value associated with a given key (the size of the Map shoukld not change)
maps.put("battery", 10);

// Check whether the Map contains a given key
String containsKey = "battery";
System.out.println("Contains '" + containsKey + "' key? " + maps.containsKey(containsKey));

// Check whether the Map contains a given value
int containsValue = 4;
System.out.println("Contains '" + containsValue + "' value? " + maps.containsValue(containsValue));

// Iterate over the keys of the Map, printing each key
System.out.println("KEYS:");
for (String key : maps.keySet()) {
System.out.println(key);
}

// Iterate over the values of the map, printing each value
System.out.println("VALUES:");
for (int value : maps.values()) {
System.out.println(value);
}

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

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

// Create an int with a positive value and assign it to a variable
int positiveValue = 10;

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

// 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 (positiveValue % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}

// Divide the number by another number using integer division
int number = positiveValue / 4;
System.out.println("Integer Division is: " + number);

/*
* Reminder!
Expand Down
27 changes: 22 additions & 5 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,69 @@
/*
* In this file you will follow the comments' instructions to complete
* the Person class.
* the Person class.
*/

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.
/**
* birthYear returns the year the person was born.
*
* The birth year is calculated by subtracting the person's age from currentYear
* The birth year is calculated by subtracting the person's age from currentYear
* that's passed in as an int. It assumes that the person's birthday has already
* passed this year.
*
* @param currentYear an int for the current year
* @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 person1 = new Person("Bud", 13);
// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Nelie", 45);

// Print the first 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 firstName = person1.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 currentYear = 2025;
int birthYear = person1.birthYear(currentYear);

// In a separate statement, print the local variable holding the birth year.
System.out.println(firstName + "'s birth year is " + birthYear + ".");

/**
* Terminology!
Expand Down
17 changes: 17 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@

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> wordSet = new HashSet<>();
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)
wordSet.add("Good");
wordSet.add("Morning");
wordSet.add("Everyone");

// Check whether the Set contains a given String
String checkString = "Hello";
boolean contains = wordSet.contains(checkString);
System.out.println("Does this set contains the word " + " '" + checkString + "'? " + contains);

// Remove an element from the Set
wordSet.remove("Good");

// Get the size of the Set
int size = wordSet.size();
System.out.println("Size of Set: " + size);

// Iterate over the elements of the Set, printing each one on a separate line
System.out.println("Here are the elements in the set:");
for (String word : wordSet) {
System.out.println(word);
}

/*
* Warning!
Expand Down
26 changes: 26 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@

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 word = "Computer";

// Find the length of the string
int length = word.length();
System.out.println("Length of this word is: " + length);

// Concatenate (add) two strings together and reassign the result
String word2 = "Table";
String addedWord = word + word2;
System.out.println("Concatenated Strings: " + addedWord);

// Find the value of the character at index 3
char findChar = word.charAt(3);
System.out.println("Character at index 3 is: " + findChar);

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
boolean containsSubsting = word.contains("abc");
System.out.println("Does it contain 'abc'? " + containsSubsting); //false

// Iterate over the characters of the string, printing each one on a separate line
System.out.println("-- Prints each letter in every 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
ArrayList<String> strList = new ArrayList<>();
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)
strList.add("Camera");
strList.add("Pen");
strList.add("Book");

// 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 allStrings = String.join(", ", strList);
System.out.println("Joined string: " + allStrings);

// Check whether two strings are equal
String word3 = "Paper";
boolean areEqual = word.equals(word3);
System.out.println("Are they equal? " + areEqual);

/*
* Reminder!
Expand Down
22 changes: 21 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,24 @@

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.

-
ListPractice
"https://www.geeksforgeeks.org/arraylist-set-method-in-java-with-examples/"
- List<String> list = new ArrayList<>(); I always get confused how to set this up
- get() function access the element
- set() function that replaces a word in the list
- contains(): https://www.geeksforgeeks.org/list-contains-method-in-java-with-examples/?ref=header_outind
- size(): https://stackoverflow.com/questions/4438710/using-collection-size-in-for-loop-comparison
- Collections.sort(): https://www.geeksforgeeks.org/collections-sort-java-examples/?ref=header_outind

StringPractice
- How to find the value of character at index by using chartAt(): "https://stackoverflow.com/questions/11229986/get-string-character-by-index"
- Joining all the strings by using join(): https://www.w3schools.com/java/ref_string_join.asp

SetPractice
- remove(): https://www.w3schools.com/java/java_hashset.asp

MapPractice
- A good review for different methods - keySet, values, keys, boolean, and how to loop Map: https://www.w3schools.com/java/java_hashmap.asp

Person
- Almost forgot how to manually do this, there is also a shortcut that was shown in SDEV 217(?) before. Here is a good review: https://www.youtube.com/watch?v=VhErIOUHwuw