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

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one

// Get the value of the array at index 2

// Get the length of the array

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

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

/*
* Reminder!
*
* Arrays start at index 0
*/
String[] cueists = {"Efren","Francisco","Johann","AJ"};
System.out.println(cueists[2]);
System.out.println(cueists.length);
for (int i=0; i < cueists.length; i++) {
System.out.println(cueists[i]);
}
for (String cueist : cueists) {
System.out.println(cueist);
}
}
}
55 changes: 25 additions & 30 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import java.util.*;

public class ListPractice {


public static void main(String[] args) {
// Create an empty ArrayList of Strings and assign it to a variable of type List

// Add 3 elements to the list (OK to do one-by-one)

// Print the element at index 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)

// Insert a new element at index 0 (the length of the list will change)

// Check whether the list contains a certain string

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

// Sort the list using the Collections library

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

/*
* Usage tip!
*
* 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
* index values a for-each loop is cleaner.
*/

ArrayList<String> poolEquipment = new ArrayList<>();
poolEquipment.add("Cue");
poolEquipment.add("Pool Balls");
poolEquipment.add("Chalk");
System.err.println(poolEquipment);

System.out.println(poolEquipment.get(1));
poolEquipment.set(1,"Pool Table");
System.err.println(poolEquipment);
poolEquipment.add(0, "Glove");
System.err.println(poolEquipment);

for (int i=0;i<poolEquipment.size();i++) {
System.out.println(poolEquipment.get(i));
}

poolEquipment.sort(null);
System.out.println(poolEquipment);

for (String equipment : poolEquipment) {
System.out.println(equipment);
}
}
}
64 changes: 26 additions & 38 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@

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

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

// Get the value associated with a given key in the Map

// 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)

// Check whether the Map contains a given key

// Check whether the Map contains a given value

// Iterate over the keys of the Map, printing each key

// Iterate over the values of the map, printing each value

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

/*
* 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
*
* 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
* 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.
*/
HashMap<String,Integer> contactID = new HashMap<>();

contactID.put("Johann",00);
contactID.put("Efren",01);
contactID.put("Francisco",02);

System.out.println(contactID.get("Efren"));

System.out.println(contactID.size());

contactID.put("Francisco",04);
System.out.println(contactID.get("Francisco"));

System.out.println("Does the HashMap Contain Efren? " + contactID.containsKey("Efren"));
System.out.println("Does the HashMap Contain 05? " + contactID.containsValue(05));

for (String i : contactID.keySet()) {
System.out.println(i);
}
for (Integer k : contactID.values()) {
System.out.println(k);
}
for (String j : contactID.keySet()) {
System.out.println("Keys: " + j + " Values: " + contactID.get(j));
}
}
}
32 changes: 14 additions & 18 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@

public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

// Create an int with a positive value and assign it to a variable

// Use the modulo % operator to find the remainder when the int is divided by 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.
double numFloat = -1.2;
int numInt = 4;

// Divide the number by another number using integer division
System.out.println(numFloat);
System.out.println(numInt);

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

if (numInt%2==0) {
System.out.println(numInt + " is even!");
}
else {
System.out.println(numInt + " is negative!");
}

System.out.println(numFloat/4);
}
}
57 changes: 30 additions & 27 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/*
* In this file you will follow the comments' instructions to complete
* the Person class.
*/

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


// Create a constructor that takes the name and age of the person
// and assigns it to the instance variables
public String name;
private final int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

// Create a toString method that gives the name and age of the person
public int getAge() {
return age;
}

public String string() {
return name + age;
}

public int birthyear(int currentyear) {
return currentyear - age;
}
// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
/**
Expand All @@ -31,22 +39,17 @@ public class Person {


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

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

// Print the first person

// Print the second person

// Get the name of the first person and store it in a local variable

// 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.

// In a separate statement, print the local variable holding the birth year.
Person person1 = new Person("RJ",20);
Person person2 = new Person("Yao",19);

System.out.println(person1.string());
System.out.println(person2.string());

String person1name = person1.getName();
System.out.println(person1name);

int byear = person1.birthyear(2025);
System.out.println(byear);

/**
* Terminology!
Expand Down
18 changes: 13 additions & 5 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import java.util.*;

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> set = new HashSet<>();
set.add("Philippines");
set.add("USA");
set.add("Canada");

// Add 3 elements to the set
// (It's OK to do it one-by-one)
System.out.println("Contains Philippines? " + set.contains("Philippines"));

set.remove("USA");

// Check whether the Set contains a given String
System.out.println("Size of the HashSet: " + set.size());

// Remove an element from the Set
for (String i : set) {
System.out.println(i);
}

// Get the size of the Set

Expand Down
39 changes: 18 additions & 21 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
public class StringPractice {
public static void main(String[] args) {
// Create a string with at least 5 characters and assign it to a variable

// Find the length of the string

// Concatenate (add) two strings together and reassign the result
import java.util.Arrays;

// Find the value of the character at index 3
public class StringPractice {
public static void main(String[] args) {
String word = "Billiards";
System.out.println("Length of word: " + word.length());

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

// Iterate over the characters of the string, printing each one on a separate line
System.out.println(word.concat(word2));

// Create an ArrayList of Strings and assign it to a variable
System.out.println(word.charAt(3));

// Add multiple strings to the List (OK to do one-by-one)
String subString = "abc";
System.out.println("Does string contain: " + subString + "? " + word.contains(subString));

// 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
char[] charArray = word.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}

// Check whether two strings are equal
String[] wordList = {"Carom","Bank","Billiard","Kick","Masse"};
String arrStr = Arrays.toString(wordList);
System.out.println(arrStr);

/*
* Reminder!
*
* When comparing objects in Java we typically want to use .equals, NOT ==.
*
* We use == when comparing primitives (e.g. int or char).
*/
System.out.println("Strings equal? " + word.equals(word2));
}
}