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

String[] myArray = new String[4];
// Set the value of the array at each index to be a different String
myArray[0] = "Hello";
myArray[1] = "Tissue";
myArray[2] = "Phone";
myArray[3] = "Cup";
// It's OK to do this one-by-one

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

// Get the length of the array

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

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

/*
* Reminder!
Expand Down
27 changes: 21 additions & 6 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
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>();
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 3 elements to the list (OK to do one-by-one)
list.add("Hello");
list.add("Good");
list.add("Node");

System.out.println("New list: " + list);
// 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, "Bad");
System.out.println("Updated List: " + list);

// Insert a new element at index 0 (the length of the list will change)
list.add(0, "Food");
System.out.println("Expected: Food, Hello, Bad, Node | " + list);

// Check whether the list contains a certain string

System.out.println(list.contains("Food"));
// 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(list.get(i));
}
// Sort the list using the Collections library

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

for(String item : list){
System.out.println(item);
}
/*
* Usage tip!
*
Expand Down
37 changes: 36 additions & 1 deletion src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@

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<>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
myMap.put("Alice", 25);
myMap.put("Bob", 30);
myMap.put("Charlie", 35);

// Get the value associated with a given key in the Map
String keyToGet = "Bob";
Integer value = myMap.get(keyToGet);
System.out.println("Value associated with key '" + keyToGet + "': " + value);

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

// Replace the value associated with a given key (the size of the Map shoukld not change)
myMap.put("Alice", 26); // Replace the value for key "Alice"
System.out.println("Updated value for key 'Alice': " + myMap.get("Alice"));

// Check whether the Map contains a given key
String keyToCheck = "Charlie";
if (myMap.containsKey(keyToCheck)) {
System.out.println("The map contains the key '" + keyToCheck + "'.");
} else {
System.out.println("The map does not contain the key '" + keyToCheck + "'.");
}

// Check whether the Map contains a given value
int valueToCheck = 30;
if (myMap.containsValue(valueToCheck)) {
System.out.println("The map contains the value " + valueToCheck + ".");
} else {
System.out.println("The map does not contain the value " + valueToCheck + ".");
}

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

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

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

/*
* Usage tip!
Expand Down
13 changes: 13 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +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 newFloat = -4.545f;
System.out.println(newFloat);

// Create an int with a positive value and assign it to a variable
int newInt = 36;
System.out.println(newInt);

// Use the modulo % operator to find the remainder when the int is divided by 3
float remainder = newInt % 3;
System.out.println(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(newInt % 2 == 0){
System.out.println("This number is even");
}else{
System.out.println("This number is odd!");
}

// Divide the number by another number using integer division

int division = newInt / 4;
System.out.println(division);
/*
* Reminder!
*
Expand Down
32 changes: 26 additions & 6 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
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 of person: " + name + " | Age: " + age;
}

public String getName(){
return name;
}
public int getAge(){
return age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,26 +41,33 @@ 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 example1 = new Person("Kim", 20);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person example2 = new Person("Hayden", 19);
// Print the first person
System.out.println(example1);

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

// Get the name of the first person and store it in a local variable
String name = example1.getName();
System.out.println(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 = example1.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.

System.out.println(birthYear);
/**
* Terminology!
*
Expand Down
20 changes: 20 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
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<>();

// Add 3 elements to the set
// (It's OK to do it one-by-one)
stringSet.add("Apple");
stringSet.add("Banana");
stringSet.add("Cherry");

// Check whether the Set contains a given String
String elementToCheck = "Banana";
if (stringSet.contains(elementToCheck)) {
System.out.println(elementToCheck + " is in the set.");
} else {
System.out.println(elementToCheck + " is not in the set.");
}

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

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

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

/*
* Warning!
Expand Down
21 changes: 19 additions & 2 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
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 = "hello";
// Find the length of the string
System.out.println("String length: " + myString.length());

// Concatenate (add) two strings together and reassign the result
String string2 ="goodbye";
String newString = myString + string2;
System.out.println(newString);

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

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

// 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(myString.charAt(i));
}

// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> list = 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)
list.add("Hello");
list.add("Good");
list.add("Oui");

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

// Check whether two strings are equal

joinedString.equals(myString);
/*
* Reminder!
*
Expand Down