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
22 changes: 21 additions & 1 deletion src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import java.util.Arrays;

public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] stringList = new String[4];
System.out.println(Arrays.toString(stringList));

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
stringList[0] = "hi";
stringList[1] = "hello";
stringList[2] = "howdy";
stringList[3] = "hey";
System.out.println(Arrays.toString(stringList));

// Get the value of the array at index 2
String element2 = stringList[2];
System.out.println("Element at index 2: " + element2);

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

// Iterate over the array using a traditional for loop and print out each item

System.out.println("--- Traditional For Loop ---");
for (int i = 0; i < stringList.length; i++){
System.out.println(stringList[i]);
}
// Iterate over the array using a for-each loop and print out each item
System.out.println("--- For Each Loop ---");
for (String string : stringList){
System.out.println(string);
}

/*
* Reminder!
Expand Down
27 changes: 26 additions & 1 deletion src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
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> stringList = new ArrayList<String>();
System.out.println(stringList);

// Add 3 elements to the list (OK to do one-by-one)
stringList.add("hello");
stringList.add("world");
stringList.add("!");
System.out.println(stringList);

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

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

// Check whether the list contains a certain string
System.out.println("Does the list contain \"hello\"? " + stringList.contains("hello"));
System.out.println("Does the list contain \".\"? " + stringList.contains("."));

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line
for (int i = 0; i < stringList.size(); i++) {
System.out.println("Index: " + i + " Value: " + stringList.get(i));
}

// Sort the list using the Collections library
Collections.sort(stringList);
System.out.println(stringList);

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

/*
* Usage tip!
Expand Down
33 changes: 31 additions & 2 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@

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> myMap = new HashMap<String, Integer>();
System.out.println(myMap);


// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
myMap.put("i", 56);
myMap.put("me", 23);
myMap.put("mine", 99);
System.out.println(myMap);

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

// Find the size (number of key/value pairs) of the Map
System.out.println("size: " + myMap.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
myMap.replace("mine", 2);
System.out.println(myMap);

// Check whether the Map contains a given key
System.out.println("Contains \"you\"? " + myMap.containsKey("you"));
System.out.println("Contains \"me\"? " + myMap.containsKey("me"));

// Check whether the Map contains a given value
System.out.println("Contains \"1\"? " + myMap.containsValue(1));
System.out.println("Contains \"2\"? " + myMap.containsValue(2));

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

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

// Iterate over the entries in the map, printing each key and value

System.out.println("--- Sets ---");
for (String key : myMap.keySet()){
System.out.println(key + " - " + myMap.get(key));
}
/*
* Usage tip!
*
Expand Down
14 changes: 14 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float negFloat = -10;
System.out.println("Negative Float: " + negFloat);

// Create an int with a positive value and assign it to a variable
int posInt = 10;
System.out.println("Positive Int: " + posInt);

// Use the modulo % operator to find the remainder when the int is divided by 3
int remainder = posInt % 3;
System.out.println("Remainder: " + 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 (posInt % 2 != 0){
System.out.println("Odd");
}
else System.out.println("Even");


// Divide the number by another number using integer division
int divided = posInt / 7;
System.out.println(posInt + " / 7 = " + divided );


/*
* Reminder!
Expand Down
27 changes: 24 additions & 3 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@

public class Person {
// Declare a public String instance variable for the name of the person
public String name;

// Declare a private int instance variable for the age of the person
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 + " - " + age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,25 +37,37 @@ 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 me = new Person("Augy", 22);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person sister = new Person("Alyssa", 16);


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

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

// Get the name of the first person and store it in a local variable
String firstPersonName = me.name;
System.out.println(firstPersonName);

// 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 firstBirthYear = me.birthYear(2025);


// In a separate statement, print the local variable holding the birth year.
System.out.println(firstBirthYear);

/**
* Terminology!
Expand Down
18 changes: 17 additions & 1 deletion src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
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> stringSet = new HashSet<String>();
System.out.println(stringSet);

// Add 3 elements to the set
// (It's OK to do it one-by-one)
stringSet.add("ahh");
stringSet.add("ahhhhh");
stringSet.add("ahhhhhhhhhhhhh");
Comment on lines +12 to +14
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.

Mood 😵‍💫

System.out.println(stringSet);

// Check whether the Set contains a given String
System.out.println("contains \"ah\"? " + stringSet.contains("ah"));
System.out.println("contains \"ahh\"? " + stringSet.contains("ahh"));

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

// Get the size of the Set
System.out.println("size: " + stringSet.size());

// Iterate over the elements of the Set, printing each one on a separate line

for (String element : stringSet){
System.out.println(element);
}
/*
* Warning!
*
Expand Down
27 changes: 27 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
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 myString = "bagels";
System.out.println(myString);

// Find the length of the string
int myStringLength = myString.length();
System.out.println("length: " + myStringLength);

// Concatenate (add) two strings together and reassign the result
myString = myString + myString;
System.out.println("new string: " + myString);

// Find the value of the character at index 3
char value3 = myString.charAt(3);
System.out.println("value at index 3: " + value3);

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

// Iterate over the characters of the string, printing each one on a separate line
for (int i = 0; i < myString.length(); i++) {
System.out.println(i + ". " + myString.charAt(i));
}

// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> stringList= new ArrayList<String>();
System.out.println(stringList);

// Add multiple strings to the List (OK to do one-by-one)
stringList.add("lipitytoo");
stringList.add("lipitytah");
stringList.add("lippytappy");
stringList.add("tootah");
System.out.println(stringList);

// 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 conglomString = String.join(",", stringList);
System.out.println(conglomString);


// Check whether two strings are equal
System.out.println("Are \"this\" and \"that\" equal? " + "this".equals("that"));
System.out.println("Is \"this\" and \"this\" equal? " + "this".equals("this"));

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

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.

-
- ArrayList.set
- ArrayList methods in general
- Using Collections Library
- Printing content in array (toString)
- String methods
- overriding toString