Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
11bf9ba
Initial comment for testing purpose
Jan 3, 2025
3602d0f
Removed testing comment
Jan 3, 2025
7393237
Created a new array of strings with size 4
Jan 3, 2025
5670765
Set each index with its own string
Jan 3, 2025
08022ba
Added print commands to get value of the array at index 2 and length …
Jan 3, 2025
fb8ccb9
Added traditional loop to print out each item
Jan 3, 2025
e9b3177
Added for-each loop to print out each item
Jan 3, 2025
e41db81
Added space in between for easier readability
Jan 3, 2025
1941847
Completed ArrayPractice
Jan 3, 2025
e8a40d3
Added import for ArrayList and created new ArrayList called stringList
Jan 3, 2025
7fdb49c
Fixed datatype and added 3 elements to the list
Jan 3, 2025
ad17790
Added a print command to get the element at index 1
Jan 3, 2025
a8328be
Replaced element at index 1, instead of BANANA its now STRAWBERRY
Jan 3, 2025
5180ae5
Replaced index at 1 with new value and inserted new element to the list
Jan 3, 2025
852cff5
Added if statement to check whether if list contains a certain string
Jan 3, 2025
d7f6340
Added a for-loop to print each element from the list
Jan 3, 2025
518f05e
Fixed for-loop to print each index and value on seperate line
Jan 3, 2025
add10b9
Added collection to sort stringList
Jan 3, 2025
cd9d36e
Added a for-each loop and printed each on second line
Jan 3, 2025
99eb7a5
Removed extra import and added space for better readability
Jan 3, 2025
296b2d6
Completed ListPractice
Jan 3, 2025
4abee5d
Created and added 3 different key and value pairs into mappy
Jan 3, 2025
9902325
Added for mappy to get the value with associated key and added size o…
Jan 3, 2025
520b7cd
Added and replaced value and added if statements to check whether key…
Jan 3, 2025
3ed5a73
Added if statements checking whether if value is present
Jan 3, 2025
b80f9ea
Added loop to iterate over keys and values for MAPPY
Jan 3, 2025
f5475c7
Added for-each loop, printing each key and value
Jan 3, 2025
abbfdcd
Added space for better readability
Jan 3, 2025
2e03a12
Completed MapPractice
Jan 3, 2025
d1b27ca
Added ideas to refreshers
Jan 3, 2025
1e7c0b4
Added float to refresher
Jan 3, 2025
a9c1a98
Addec and created a float with negative value, created an int with po…
Jan 3, 2025
5fef464
Added if statement to determine whether number is even or odd
Jan 3, 2025
d8abcea
Added division to the number
Jan 3, 2025
1ac7783
Added space for better readability
Jan 3, 2025
1b4e713
Completed NumberPractice
Jan 3, 2025
5f2d93d
Created and added elements to the HashSet
Jan 3, 2025
f6a167c
Added, checked, removed, and found the size of the Set within an Array
Jan 3, 2025
51b87dc
Added for each loop to iterate over each element
Jan 3, 2025
e1e0e6d
Completed SetPractice
Jan 3, 2025
55cd535
Created a string, found the length, and concatenated them and printed…
Jan 3, 2025
fd1b79f
Added and founded value of name at index 3, and checked whether given…
Jan 3, 2025
a6232b9
Iterated over characters of a string, name
Jan 3, 2025
967598b
Created ArrayList named Spiders, and added strings to Spiders
Jan 3, 2025
ec661ab
Added SpiderPeople to join all the strings seperated by commas
Jan 3, 2025
55ded98
Added if statements to check whether if two string are equal
Jan 3, 2025
95364f5
Completed StringPractice
Jan 3, 2025
f7aff83
Created and initialized person and age and created constructor
Jan 3, 2025
54b66a8
Created a toString
Jan 3, 2025
9f3f58c
Created two instances person
Jan 3, 2025
ea78bc0
Printed both names, and made a local variable out of personOne
Jan 3, 2025
6ec6b20
Called to the birthYear method for personOne and printed their birthYear
Jan 3, 2025
9679e1c
Completed Person.java
Jan 3, 2025
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: 15 additions & 0 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] arrayFour = 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
arrayFour[0] = "ZERO";
arrayFour[1] = "ONE";
arrayFour[2] = "TWO";
arrayFour[3] = "THREE";

// Get the value of the array at index 2
System.out.println(arrayFour[2]); //output: TWO

// Get the length of the array
System.out.println(arrayFour.length);

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

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

/*
* Reminder!
*
* Arrays start at index 0
*/

//DONE
}
}
26 changes: 25 additions & 1 deletion src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
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> 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("APPLE");
stringList.add("BANANA");
stringList.add("KIWI");

// Print the element at index 1

System.out.println(stringList.get(1)); //output: BANANA

// 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, "STRAWBERRY");

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

// Check whether the list contains a certain string
if (stringList.contains("AVOCADO")) {
System.out.println("We have avocado :)");
} else {
System.out.println("There is no avocado :(");
}

// 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(i + ": " + stringList.get(i));
}

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

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

/*
* Usage tip!
Expand All @@ -32,5 +54,7 @@ public static void main(String[] args) {
* Otherwise, if you're iterating the in the conventional order and don't need the
* index values a for-each loop is cleaner.
*/

//DONE
}
}
31 changes: 30 additions & 1 deletion src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@

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> mappy = 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)
mappy.put("a", 1);
mappy.put("b", 2);
mappy.put("c", 3);

// Get the value associated with a given key in the Map
mappy.get("a");

// Find the size (number of key/value pairs) of the Map
mappy.size();

// Replace the value associated with a given key (the size of the Map shoukld not change)
mappy.replace("a", 0);

// Check whether the Map contains a given key
if (mappy.containsKey("d")) {
System.out.println("Letter d, is present within list MAPPY");
} else {
System.out.println("There is no key present");
}

// Check whether the Map contains a given value
if (mappy.containsValue(2)) {
System.out.println("Value 2, is present within list MAPPY");
} else {
System.out.println("There is no value present");
}

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

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

// Iterate over the entries in the map, printing each key and value
for (HashMap.Entry<String, Integer> entries : mappy.entrySet()) {
System.out.println("KEY " + entries.getKey());
System.out.println("VALUE " + entries.getValue());
}

/*
* Usage tip!
Expand All @@ -39,5 +66,7 @@ public static void main(String[] args) {
* Example: If you want to hold the student ID numbers of everyone in a course,
* and you don't care about any ordering.
*/

//DONE
}
}
12 changes: 11 additions & 1 deletion src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float negativeVal = -123f;

// Create an int with a positive value and assign it to a variable
int positiveVal = 123;

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

// Divide the number by another number using integer division
int division = positiveVal / 3;
System.out.println(division);

/*
* Reminder!
Expand All @@ -20,6 +30,6 @@ public static void main(String args[]) {
* Example:
* 7 / 3 = 2 when performing int division
*/

//DONE
}
}
28 changes: 23 additions & 5 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

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.
Expand All @@ -28,25 +34,35 @@ 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 personOne = new Person("Caitlyn Kiramman", 24);

// Create another instance of Person with a different name and age and
// assign it to a different variable

Person personTwo = new Person("Mel Medarda", 33);

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

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

// Get the name of the first person and store it in a local variable
String ArcaneChara1 = personOne.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 birthYear1 = personOne.birthYear(currentYear);

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

/**
* Terminology!
Expand All @@ -62,5 +78,7 @@ public static void main(String[] args) {
*
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
*/

//DONE
}
}
14 changes: 14 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
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("TOMATO");
StringSet.add("POTATO");
StringSet.add("YAM");

// Check whether the Set contains a given String
StringSet.contains("CABBAGE");

// Remove an element from the Set
StringSet.remove("TOMATO");

// Get the size of the Set
StringSet.size();

// Iterate over the elements of the Set, printing each one on a separate line
for (String veggie : StringSet) {
System.out.println(veggie);
}

/*
* Warning!
Expand All @@ -25,5 +37,7 @@ public static void main(String[] args) {
*
* Also remember that sets do NOT have duplicates.
*/

//DONE
}
}
33 changes: 31 additions & 2 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 name = "Luigi";
System.out.println(name);

// Find the length of the string
System.out.println(name.length());
System.out.println();

// Concatenate (add) two strings together and reassign the result
String nameTwo = "Mario ";
String result = nameTwo + name;
System.out.println(result);

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

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

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

// Create an ArrayList of Strings and assign it to a variable

ArrayList<String> Spiders = 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)
Spiders.add("Peter Parker");
Spiders.add("Miguel O'Hara");
Spiders.add("Miles Morales");
Spiders.add("Gwen Stacy");
Spiders.add("Peni Parker");
Comment on lines +33 to +37
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.

Fun!


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

// Check whether two strings are equal
if (Spiders.get(3).equals(4)) {
System.out.println("Their names are equal!");
} else {
System.out.println("Their names are not equal");
}

/*
* Reminder!
Expand All @@ -28,5 +55,7 @@ public static void main(String[] args) {
*
* We use == when comparing primitives (e.g. int or char).
*/

//DONE
}
}
11 changes: 10 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

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
- Map Entries
- Set vs. Map
- Float
- .toCharArray()

- Public Private
- Constructor
- toString
- Instance