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

// Set the value of the array at each index to be a different String

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
fruits[0] = "Orange"
fruits[1] = "Pear"
fruits[2] = "Banana"
fruits[3] = "Cherry"



// Get the value of the array at index 2
String valueAtIndex2 = fruits[2];
System.out.println("Value at index 2: " + valueAtIndex2);

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


// 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 < fruits.length; i++) {
System.out.println(fruits[i]);
}

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

System.out.println("For-each loop:");
for (String fruit : fruits) {
System.out.println(fruit);
}
/*
* Reminder!
*
Expand Down
32 changes: 30 additions & 2 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
public class ListPractice {
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> animals = new ArrayList<>();

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

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

// (Do not insert a new value. The length of the list should not change)
// Insert a new element at index 0 (the length of the list will change)
animals.add(0, "Elephant");

// Check whether the list contains a certain string
if (animals.contains("Rabbit")) {
System.out.println("The list contains 'Rabbit'.");
} else {
System.out.println("The list does not contain 'Rabbit'.");
}

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

System.out.println("Using traditional for loop:");
for (int i = 0; i < animals.size(); i++) {
System.out.println("Index " + i + ": " + animals.get(i));
}

// Sort the list using the Collections library

Collections.sort(animals);

System.out.println("For-each loop after sorting:");
for (String animal : animals) {
System.out.println(animal);
}

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

Expand Down
27 changes: 26 additions & 1 deletion src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@

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> scores = new HashMap<>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
scores.put("Alice", 85);
scores.put("Bob", 90);
scores.put("Charlie", 78);

// Get the value associated with a given key in the Map
System.out.println("Score for Bob: " + scores.get("Bob"));

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

// Replace the value associated with a given key (the size of the Map shoukld not change)
scores.put("Alice", 95);

// Check whether the Map contains a given key
if (scores.containsKey("Charlie")) {
System.out.println("The map contains the key 'Charlie'.");
}

// Check whether the Map contains a given value
if (scores.containsValue(90)) {
System.out.println("The map contains the value 90.");
}

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

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

// 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 : scores.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}

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

// Create an int with a positive value and assign it to a variable
int positiveInt = 14;

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

// Divide the number by another number using integer division
int result = positiveInt / 4;
System.out.println("Result of integer division: " + result);

/*
* Reminder!
Expand Down
22 changes: 22 additions & 0 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@

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
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}


// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
public int birthYear(int currentYear) {
return currentYear - age;
}

/**
* birthYear returns the year the person was born.
*
Expand All @@ -32,21 +47,28 @@ public class Person {

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

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

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

// In a separate statement, print the local variable holding the birth year.
System.out.println("Birth year of " + firstName + ": " + birthYear1);

/**
* 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> fruits = new HashSet<>();

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

// Check whether the Set contains a given String
if (fruits.contains("Banana")) {
System.out.println("The set contains 'Banana'.");
} else {
System.out.println("The set does not contain 'Banana'.");
}

// Remove an element from the Set
fruits.remove("Cherry");

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

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

/*
* Warning!
Expand Down
25 changes: 25 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +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 message = "Hello";

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

// Concatenate (add) two strings together and reassign the result
message += " World!";
System.out.println("Concatenated string: " + message);

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

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

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

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

// Add multiple strings to the List (OK to do one-by-one)
words.add("Java");
words.add("is");
words.add("awesome");

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

// Check whether two strings are equal
String str1 = "Hello";
String str2 = "hello";
boolean areEqual = str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal (ignoring case)? " + areEqual);

/*
* Reminder!
Expand Down