Skip to content
Open
28 changes: 25 additions & 3 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
public class ArrayPractice {
public static void main(String[] args) {
public class ArrayPractice
{
public static void main(String[] args)
{
// Create an array of Strings of size 4
String[] newArray = 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
newArray[0] = "apple";
newArray[1] = "banana";
newArray[2] = "strawberry";
newArray[3] = "pear";

// Get the value of the array at index 2

String value = newArray[2];
System.out.println(value);

// Get the length of the array

int length = newArray.length;
System.out.println(length);

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

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

/*
* Reminder!
*
Expand Down
30 changes: 30 additions & 0 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListPractice {


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

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

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

newList.add("apple");
newList.add("banana");
newList.add("orange");

// Print the element at index 1

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

newList.set(1, "pear");

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

newList.set(0, "strawberry");

// Check whether the list contains a certain string

boolean check = newList.contains("orange");
System.out.println(check);

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

for(int i=0; i<newList.size(); i++)
{
System.out.println("index: " + i + ", value: " + newList.get(i));
}

// Sort the list using the Collections library

Collections.sort(newList);

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

/*
* Usage tip!
Expand Down
39 changes: 38 additions & 1 deletion src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@

import java.util.HashMap;
import java.util.Map;

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

Map<String,Integer> newMap = new HashMap<>();

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

newMap.put("bob", 25);
newMap.put("tom", 30);
newMap.put("jim", 28);

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

int value = newMap.get("tom");
System.out.println(value);

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

int size = newMap.size();
System.out.println(size);

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

newMap.put("tom", 22);

// Check whether the Map contains a given key

boolean checkKey = newMap.containsKey("tom");
System.out.println(checkKey);

// Check whether the Map contains a given value

boolean checkValue = newMap.containsValue(22);
System.out.println(checkValue);

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

for(String key: newMap.keySet())
{
System.out.println(key);
}

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

for(Integer values: newMap.values())
{
System.out.println(values);
}

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

for (Map.Entry<String,Integer> entry : newMap.entrySet())
{

System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
}

/*
* Usage tip!
Expand Down
21 changes: 21 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,38 @@ public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float num1 = -42.3f;
System.out.println(num1);

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

int num2 = 7;
System.out.println(num2);

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

int remainder = num2 % 3;
System.out.println(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 (num2 % 2 == 0)
{
System.out.println("Even");
}
else{
System.out.println("Odd");
}

// Divide the number by another number using integer division

int sum = num2 / 3;
System.out.println(sum);


/*
* Reminder!
*
Expand Down
35 changes: 35 additions & 0 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ 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 @@ -29,24 +44,44 @@ public class Person {
*/
// (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 person1 = new Person("Bob", 25);

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

Person person2 = new Person("Jim", 20);

// Print the first person

System.out.println(person1.toString());

// Print the second person

System.out.println(person2.toString());

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

String name = person1.name;
System.out.println(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 bYear = person1.birthYear(2025);


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

/**
* Terminology!
Expand Down
29 changes: 27 additions & 2 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
public class SetPractice {
public static void main(String[] args) {
import java.util.HashSet;
import java.util.Set;

public class SetPractice
{
public static void main(String[] args)
{
// Create a HashSet of Strings and assign it to a variable of type Set

Set<String> newSet = new HashSet<>();

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

newSet.add("apple");
newSet.add("banana");
newSet.add("orange");

// Check whether the Set contains a given String

boolean check = newSet.contains("apple");
System.out.println(check);

// Remove an element from the Set

newSet.remove("orange");


// Get the size of the Set

int size = newSet.size();
System.out.println("Size: " + size);

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

for(String fruit: newSet)
{
System.out.println(fruit);
}

/*
* Warning!
*
Expand Down
39 changes: 39 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
import java.util.ArrayList;
import java.util.List;

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 = "Matthew";
System.out.println(name);

// Find the length of the string

int length = name.length();
System.out.println("length: " + length);


// Concatenate (add) two strings together and reassign the result

String word1 = "hello";
String word2 = "world";
String word3 = word1 + word2;
System.out.println(word3);

// Find the value of the character at index 3

char value = word3.charAt(3);
System.out.println("character at index 3: " + value);


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

boolean contains = word3.contains("low");
System.out.println(contains);

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

for (int i = 0; i < word3.length(); i++)
{
System.out.println(word3.charAt(i));
}

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

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

// Add multiple strings to the List (OK to do one-by-one)
newList.add("apple");
newList.add("banana");
newList.add("strawberry");
newList.add("orange");

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

// Check whether two strings are equal

boolean test = word1.equals(word2);
System.out.println(test);

/*
* Reminder!
*
Expand Down
Loading