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

String[] stringArray = 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

stringArray[0] = "Primus";
stringArray[1] = "Secundus";
stringArray[2] = "Tertius";
stringArray[3] = "Quartus";
// Get the value of the array at index 2

System.out.println("The Value of Index 2 is: " + stringArray[2]);
// Get the length of the array

System.out.println("The Length of the Array is: " + stringArray.length);
// Iterate over the array using a traditional for loop and print out each item

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

for (String string : stringArray) {
System.out.println(string);
}
/*
* Reminder!
*
Expand Down
30 changes: 20 additions & 10 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
public class ListPractice {
import java.util.ArrayList;

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> stringList = 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 3 elements to the list (OK to do one-by-one)

stringList.add("Primus");
stringList.add("Secundus");
stringList.add("Tertius");
// 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, stringList.get(0));
System.out.println(stringList.get(1));
// Insert a new element at index 0 (the length of the list will change)

stringList.add(0, "Zero");
System.out.println(stringList.get(0));
// Check whether the list contains a certain string

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

// Iterate over the list using a for-each loop
Expand All @@ -26,10 +34,12 @@ public static void main(String[] args) {
/*
* Usage tip!
*
* Use a traditional for-loop when you need to use the index or you need to iterate in an
* Use a traditional for-loop when you need to use the index or you need to
* iterate in an
* unconventional order (e.g. backwards)
*
* Otherwise, if you're iterating the in the conventional order and don't need the
* Otherwise, if you're iterating the in the conventional order and don't need
* the
* index values a for-each loop is cleaner.
*/
}
Expand Down
36 changes: 22 additions & 14 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@

import java.util.HashMap;

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> stringMap = 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)

stringMap.put("Primus", 1);
stringMap.put("Secundus", 2);
stringMap.put("Tertius", 3);
// Get the value associated with a given key in the Map

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

// Replace the value associated with a given key (the size of the Map shoukld not change)

System.out.println("The Size of the Map is: " + stringMap.size());
// Replace the value associated with a given key (the size of the Map shoukld
// not change)
System.out.println("Before: " + stringMap);
System.out.println(stringMap.replace("Primus", 1, 1000));
System.out.println("After: " + stringMap);
// Check whether the Map contains a given key

System.out.println(stringMap.containsKey("Tertius"));
// Check whether the Map contains a given value

System.out.println(stringMap.containsValue(1000));
// Iterate over the keys of the Map, printing each key

System.out.println("The Keys of the Map are: " + stringMap.keySet());
// Iterate over the values of the map, printing each value

System.out.println("The Values within the Map are: " + stringMap.values());
// Iterate over the entries in the map, printing each key and value

System.out.println("The Key-Value Pairs are: " + stringMap.entrySet());
Comment on lines +28 to +32
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.

Make sure to practice doing a loop over these sets.

/*
* Usage tip!
*
* Maps are great when you want a specific key to value mapping.
* Example: The key could be a person's name, and the value could be their phone number
* Example: The key could be a person's name, and the value could be their phone
* number
*
* However if your keys are simple ascending 0-indexed integers with no gaps
* (0, 1, 2, 3, 4...) then an array or List is likely a better choice.
* Example: If you want to store the order of songs in a playlist.
*
* If you're finding that you're just wanting to store unordered values and the keys
* If you're finding that you're just wanting to store unordered values and the
* keys
* are unimportant, a Set may be a better choice.
* Example: If you want to hold the student ID numbers of everyone in a course,
* and you don't care about any ordering.
Expand Down
20 changes: 15 additions & 5 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float negativeNum = -13;
// Create an int with a positive value and assign it to a variable

int positiveNum = 13;
// Use the modulo % operator to find the remainder when the int is divided by 3

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

if (positiveNum % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
// Divide the number by another number using integer division

System.out.println(negativeNum / 5);
/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* Example:
* 7 / 3 = 2 when performing int division
*/

Expand Down
40 changes: 28 additions & 12 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

String publicName;
private int privAge;
Comment on lines +9 to +10
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.

You need to put public before public variables and methods, otherwise they get package visibility by default.


// 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) {
publicName = name;
privAge = age;
}

// Create a toString method that gives the name and age of the person

public String toString() {
return "Name: " + publicName + " Age: " + privAge;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,25 +34,32 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)
public int birthYear(int currentYear) {

int birthYear = currentYear - privAge;

return birthYear;
Comment on lines +39 to +41
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.

Consider simplifying this to a single line

}

public static void main(String[] args) {
// Create an instance of Person

Person firstPerson = new Person("Harold", 32);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person secondPerson = new Person("Tilda", 53);
// Print the first person

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

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

String firstPersonName = firstPerson.publicName;
System.out.println("The Name of the First Person: " + 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 firstPersonBirthYear = firstPerson.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.
System.out.println("The Birth Year of the First Person is: " + firstPersonBirthYear);

/**
* Terminology!
Expand All @@ -55,12 +68,15 @@ public static void main(String[] args) {
* An instance is a specific object made according to that definition.
* We use "instance" and "object" to mean the same thing.
*
* For example, if there is a Person class, we can make an instance of a specific person: Auberon.
* For example, if there is a Person class, we can make an instance of a
* specific person: Auberon.
*
* There can be many instances for the same class. For example: Auberon, Xinting, Baya are all
* There can be many instances for the same class. For example: Auberon,
* Xinting, Baya are all
* different instances of the Person class.
*
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
* Each instance has its own instance variables: Auberon's age can be different
* from Baya's age.
*/
}
}
26 changes: 18 additions & 8 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
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> stringSet = 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)

stringSet.add("Primus");
stringSet.add("Secundus");
stringSet.add("Tertius");
// Check whether the Set contains a given String

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

System.out.println(stringSet);
stringSet.remove("Secundus");
System.out.println(stringSet);
// Get the size of the Set

System.out.println(stringSet.size());
// Iterate over the elements of the Set, printing each one on a separate line

for (String str : stringSet) {
System.out.println(str);
}
/*
* Warning!
*
* The iteration order over the items in a HashSet is NOT GUARANTEED.
*
* Even running the exact same program multiple times may give different results.
* Even running the exact same program multiple times may give different
* results.
* Do not use a HashSet if order is important! You can use a TreeSet if you
* want items in sorted order, or an array or List if you want them in a specified
* want items in sorted order, or an array or List if you want them in a
* specified
* order.
*
* Also remember that sets do NOT have duplicates.
Expand Down
42 changes: 30 additions & 12 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
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 string1 = "Primus";
// Find the length of the string

System.out.println(string1.length());
// Concatenate (add) two strings together and reassign the result
String string2 = "Secundus";

String concatenatedString = string1 + string2;
System.out.println(concatenatedString);
// Find the value of the character at index 3

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

// Iterate over the characters of the string, printing each one on a separate line

System.out.println(concatenatedString.charAt(3));
// Check whether the string contains a given substring (i.e. does the string
// have "abc" in it?)
if (concatenatedString.contains("abc")) {
System.out.println("This String contains abc");
} else {
System.out.println("This String does not contain abc");
}
// Iterate over the characters of the string, printing each one on a separate
// line
char[] charArray = concatenatedString.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
// Create an ArrayList of Strings and assign it to a variable

ArrayList<String> stringList = 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)

// Join all of the strings in the list together into a single string separated by commas
stringList.add("Tertius");
stringList.add("Quartus");
stringList.add("Quintus");
// 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.join(", ", stringList);
System.out.println(stringList);
// Check whether two strings are equal

System.out.println(string1.equals(string2));
/*
* Reminder!
*
Expand Down