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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Most of these taks should be easy, but there will likely be some that ask you to
- SetPractice
- MapPractice
- Person.java
- Making classes

## Submitting
Make a Pull Request (PR) against the original repository. Copy the link into Canvas to submit.
Expand Down
19 changes: 16 additions & 3 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] strings = 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
strings[0] = "aardvark";
strings[1] = "bison";
strings[2] = "donkey";
strings[3] = "cheetah";

// Get the value of the array at index 2

String indexTwo = strings[2];
System.out.println(indexTwo);

// Get the length of the array
int length = strings.length;
System.out.println(length);

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

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

for (String string : strings) {
System.out.println(string);
}
/*
* Reminder!
*
Expand Down
19 changes: 18 additions & 1 deletion src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
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> list = new ArrayList<>();
Copy link
Copy Markdown

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 where appropriate (Map)


// Add 3 elements to the list (OK to do one-by-one)
list.add("homework");
list.add("pencils");
list.add("erasers");

// Print the element at index 1
System.out.println(list.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)
list.set(1, "pencil case");

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

// Check whether the list contains a certain string
System.out.println("Does the list contain a notebook?: " + list.contains("notebook"));

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line
for (int i = 0; i < list.size(); i++) {
System.out.println(i + "=" + list.get(i));
}

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

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

for (String item : list) {
System.out.println(item);
}
/*
* Usage tip!
*
Expand Down
38 changes: 32 additions & 6 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +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> map = new HashMap<>();
Copy link
Copy Markdown

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 where appropriate (Map)


// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
map.put("Cheese", 2);
map.put("Onions",1);
map.put("Bread",5);

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

// Get the value associated with a given key in the Map
System.out.println("Cheese costs " + map.get("Cheese") + " dollars.");

// Find the size (number of key/value pairs) of the Map
System.out.println("There are " + map.size() + " pairs in the Map.");

// Replace the value associated with a given key (the size of the Map shoukld not change)
map.put("Cheese", 3);
System.out.println("Cheese now costs " + map.get("Cheese") + " dollars.");

// Check whether the Map contains a given key

if (map.containsKey("Onions")) {
System.out.println("Map contains onions.");
} else {
System.out.println("Map does not contain onions.");
}

// Check whether the Map contains a given value

if (map.containsValue(2)) {
System.out.println("One of the items has a value of 2.");
} else {
System.out.println("None of the items has a value of 2.");
}
// Iterate over the keys of the Map, printing each key
for (String string : map.keySet()) {
System.out.println(string);
}

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

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

for (String string : map.keySet()) {
System.out.println(string + "=" + map.get(string));
}
Comment on lines +50 to +52
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works! Can you search to find another way to iterate over the entries directly, so you you don't need to repeatedly call get?

/*
* Usage tip!
*
Expand Down
21 changes: 18 additions & 3 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +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 negativeFloat = -3.14f;
System.out.println("A negative float is: " + negativeFloat);

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

int positiveInt = 3;
System.out.println("A positive int is: " + positiveInt);

// Use the modulo % operator to find the remainder when the int is divided by 3

int remainder = positiveInt % 3;
System.out.println("When you divide " + positiveInt + " by 3, the remainder is: " + 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 anotherInt = 2;

int result = positiveInt / anotherInt;
System.out.println("When you divide " + positiveInt + " by " + anotherInt + " the answer is rounded down to: " + result);
/*
* Reminder!
*
Expand Down
31 changes: 27 additions & 4 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
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;
}

// added getters
public String getName() { return name; }

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

// Create a toString method that gives the name and age of the person
@Override
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 +40,36 @@ 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 steve = new Person("Steve",27);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person jackie = new Person("Jackie", 28);

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

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

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

String steveName = steve.getName();
System.out.println(steveName);

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


// In a separate statement, print the local variable holding the birth year.
System.out.println(birthYear);

/**
* Terminology!
Expand Down
19 changes: 17 additions & 2 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
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> set = new HashSet<String>();
Copy link
Copy Markdown

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)
set.add("coffee");
set.add("tea");
set.add("juice");

// Check whether the Set contains a given String

if (set.contains("juice")) {
System.out.println("Set contains 'juice'.");
} else {
System.out.println("Set does not contain 'juice'");
}
// Remove an element from the Set
set.remove("juice");

// Get the size of the Set
System.out.println("The set contains " + set.size() + " items.");

// Iterate over the elements of the Set, printing each one on a separate line

for (String string : set) {
System.out.println(string);
}

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

// Find the length of the string
int stringLength = string.length();
System.out.println(stringLength);

// Concatenate (add) two strings together and reassign the result
string = string + " 1; " + string + " 2";
System.out.println(string);

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

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
Boolean containsSubstring = string.contains("example");
System.out.println("The sentence contains example?: " + containsSubstring);

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

for (int i = 0; i < string.length(); i++) {
System.out.println(string.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> stringArray = new ArrayList<>();
Copy link
Copy Markdown

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


// Add multiple strings to the List (OK to do one-by-one)
stringArray.add("example 1");
stringArray.add("example 2");
stringArray.add("example 3");

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

// Check whether two strings are equal
String first = "test";
String second = "text";

/*
System.out.println("String 'test' equals 'text'?: " + first.equals(second));
/*java
* Reminder!
*
* When comparing objects in Java we typically want to use .equals, NOT ==.
Expand Down
7 changes: 6 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

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 a float (used to using double)
- difference between list and arraylist syntax
- array syntax
- string utilities
- set syntax
- forEach for maps