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

String[] ofFour = 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
ofFour[0] = "Hello";
ofFour[1] = "goodbye";
ofFour[2] = "Hello Again";
ofFour[3] = "Good Night";

// Get the value of the array at index 2

System.out.println(ofFour[2]);
// Get the length of the array

System.out.println(ofFour.length);
// Iterate over the array using a traditional for loop and print out each item

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

for (String string : ofFour) {
System.out.println(string);
}
/*
* Reminder!
*
Expand Down
30 changes: 21 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import java.util.ArrayList;
import java.util.Collection;
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 arraylist = 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.

Here and in the other Collections exercises remember to use interface types and generics, e.g.

List<String> myList = new ArrayList<>();

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

arraylist.add("Yo");
arraylist.add("Yo2");
arraylist.add("Yo3");

// Print the element at index 1

System.out.println(arraylist.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)

arraylist.set(1, "New Value");
System.out.println(arraylist.get(1));
// Insert a new element at index 0 (the length of the list will change)

arraylist.add(0, "New 0 index Element");
// Check whether the list contains a certain string

arraylist.contains("String");
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

for (int i = 0; i < arraylist.size(); i++) {
System.out.println(arraylist.get(i));
}
// Sort the list using the Collections library

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

for (Object item : arraylist) {
System.out.println(item);
}
/*
* Usage tip!
*
Expand Down
27 changes: 17 additions & 10 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@

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> hmp = new HashMap<String, Integer>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
hmp.put("Number 1", 0);
hmp.put("Number 2", 1);
hmp.put("Number 3", 2);

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

hmp.get("Number 1");
// Find the size (number of key/value pairs) of the Map

System.out.println(hmp.size());
// Replace the value associated with a given key (the size of the Map shoukld not change)

hmp.replace("Number 1", 00000);
// Check whether the Map contains a given key

hmp.containsKey("Number 4");
// Check whether the Map contains a given value

hmp.containsValue(3);
// 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

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

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

for (String key : hmp.keySet()) {
Integer value = hmp.get(key);
System.out.println("Key: " + key + ", Value: " + value);
}
/*
* Usage tip!
*
Expand Down
12 changes: 8 additions & 4 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

double negativeValue = -4.0;
// Create an int with a positive value and assign it to a variable

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

double remainder = positveValue % 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)
if (remainder % 2 == 0) {
System.out.println("the Number is even");
}
else {System.out.println("Number is odd");}
// Use an if-else to print "Even" if the number is even and "Odd"
// if the number is odd.

// Divide the number by another number using integer division

int dividiedInts = 7/7;
/*
* Reminder!
*
Expand Down
27 changes: 21 additions & 6 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ 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 "This persons name is " + name + " and the age is " + age;
}

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


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

Person person1 = new Person("Liban James", 44);
// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Bial", 323);


// Print the first person
person1.toString();

// Print the second person

person2.toString();
// Get the name of the first person and store it in a local variable

String p1Name = 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 p1By = person1.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.

System.out.println(p1By);
/**
* Terminology!
*
Expand Down
17 changes: 13 additions & 4 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
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> hashsetString = new HashSet<>();

// Add 3 elements to the set
// (It's OK to do it one-by-one)

hashsetString.add("Hello");
hashsetString.add("Hello Again");
hashsetString.add("Hello Again #2");
// Check whether the Set contains a given String
hashsetString.contains("String");

// Remove an element from the Set

hashsetString.remove("Hello");
// Get the size of the Set

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

for (String item : hashsetString) {
System.out.println(item);
}
/*
* Warning!
*
Expand Down
32 changes: 25 additions & 7 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 five = "ontwo";
// Find the length of the string

System.out.println(five.length());
// Concatenate (add) two strings together and reassign the result
String secondString = "another";

five += secondString;
// Find the value of the character at index 3

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

boolean check = five.contains("abc");
System.out.println(check);
// Iterate over the characters of the string, printing each one on a separate line

for (int i = 0; i < five.length() ; i++) {
System.out.println(five.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

ArrayList newAList = new ArrayList<>();

// Add multiple strings to the List (OK to do one-by-one)
newAList.add("Hello");
newAList.add("No");
newAList.add("Back");
newAList.add("Afhasiuhs");

// 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
StringBuilder newString = new StringBuilder();

for (Object word : newAList) {
newString.append(word);
}
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.

This is better than naive string concatenation, but can you find a method that lets you do it without any loop?

System.out.println(newString);
// Check whether two strings are equal

boolean checker = five.equals(secondString);
System.out.println(checker);
/*
* Reminder!
*
Expand Down
8 changes: 7 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

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.

-
- How to make an array list
- How to print a element in a arraylist
- How to add a new value (arrayList)
- How to reset a value (arrayList)
- How to make a hashset (also what is a hashset)
- How to iterate through hashmap
- How to make a constructor