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: 16 additions & 1 deletion src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String [] words = 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
words[0] = "Diamond";
words[1] = "Ruby";
words[2] = "Pearl";
words[3] = "Emerald";

// Get the value of the array at index 2
String valAtTwo = words[2];

// Get the length of the array
int wordsLength = words.length;

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

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

for(String word : words)
{
System.out.println(word);
}
/*
* Reminder!
*
Expand Down
28 changes: 24 additions & 4 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
import java.util.ArrayList;
import java.util.List;
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
List<String> wordList= new ArrayList<>();

// Add 3 elements to the list (OK to do one-by-one)
wordList.add("Hello");
wordList.add("Beautiful");
wordList.add("World");

// Print the element at index 1
System.out.println(wordList.get(1));
System.out.println();

// Replace the element at index 1 with a new value
wordList.set(1, "Hi");

// (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)
wordList.add(0,"Hola");

// Check whether the list contains a certain string

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

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

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

for(String word : wordList)
{
System.out.println("Value: " + word + " ");
}
/*
* Usage tip!
*
Expand Down
31 changes: 27 additions & 4 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@

import java.util.HashMap;
import java.util.List;
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> wordMap = new HashMap <>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
wordMap.put("Glasgow", 1);
wordMap.put("Mexico City", 10);
wordMap.put("Madrid", 2);

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

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

// Replace the value associated with a given key (the size of the Map shoukld not change)
wordMap.replace("Mexico City", 20);

// Check whether the Map contains a given key
wordMap.containsKey("Madrid");

// Check whether the Map contains a given value
wordMap.containsValue(13);

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

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

for(int value : wordMap.values())
{
System.out.println(value);
}
System.out.println();
// Iterate over the entries in the map, printing each key and value

for(String key : wordMap.keySet())
{
System.out.println("Key: " + key + " Value: " + wordMap.get(key));
}

/*
* Usage tip!
*
Expand Down
13 changes: 13 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,18 @@ public static void main(String args[]) {
* 7 / 3 = 2 when performing int division
*/

double negFloat = -1.5;
int posInt = 1;
int remainder = posInt % 3;
if(remainder % 2 == 0)
{
System.out.println("even");
}
else
{
System.out.println("odd");
}
int intDivision = remainder / 5;

}
}
35 changes: 32 additions & 3 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@
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 pName, int pAge)
{
name = pName;
age = pAge;
}

// Create a toString method that gives the name and age of the person
@Override
public String toString()
{
return "Name: " + name + " Age: " + age;
}

public String getName()
{
return name;
}
Comment on lines +27 to +30
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We don't need a getter for a public variable.


public int getAge()
{
return age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,25 +47,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 person1 = new Person("Ivy", 20);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Kai", 17);

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

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

// Get the name of the first person and store it in a local variable
String pName1 = person1.getName();

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

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

/**
* Terminology!
Expand Down
16 changes: 14 additions & 2 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import java.util.HashSet;
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
Set<String> stringSet = new HashSet<>();

// Add 3 elements to the set
// (It's OK to do it one-by-one)
stringSet.add("Sweet");
stringSet.add("Salty");
stringSet.add("Sour");

// Check whether the Set contains a given String
boolean hasSour = stringSet.contains("Sour");

// Remove an element from the Set
stringSet.remove("Sour");

// Get the size of the Set

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

for(String s : stringSet)
{
System.out.println(s);
}
/*
* Warning!
*
Expand Down
24 changes: 23 additions & 1 deletion src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
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 fiveChar = "Sprinkles";

// Find the length of the string
fiveChar.length();

// Concatenate (add) two strings together and reassign the result
fiveChar = fiveChar + "Frosting";

// Find the value of the character at index 3
char charAtThree = fiveChar.charAt(3);
//System.out.println(charAtThree);

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

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

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

// Add multiple strings to the List (OK to do one-by-one)
wordList.add("Cake");
wordList.add("Berries");
wordList.add("Candles");

// 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 combinedList = wordList.get(0) + ", " + wordList.get(1) + ", " + wordList.get(2);
//System.out.println(combinedList);

// Check whether two strings are equal
boolean isEqual = combinedList.equals(fiveChar);
//System.out.println(isEqual);

/*
* Reminder!
Expand Down