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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DO NOT DOWNLOAD THIS CODE AS A ZIP FILE
# DO NOT DOWNLOAD THIS CODE AS A ZIP FILE.
# READ ME FIRST BEFORE DOWNLOADING

This repository will be used to practice both your git and Java skills. DO NOT DOWNLOAD IT AS A ZIP FILE. Downloading it as a zip file will prevent the git commands from working properly.
Expand Down
23 changes: 23 additions & 0 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import java.util.Arrays;

public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] myArray = new String[4];
System.out.println(myArray.toString());

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
/*for (int i = 0; i < myArray.length; i++)
{
myArray[i] = "index" + i;
}*/
myArray[0] = "index0";
myArray[1] = "index1";
myArray[2] = "index2";
myArray[3] = "index3";
System.out.println(Arrays.toString(myArray));

// Get the value of the array at index 2
System.out.println(myArray[2]);

// Get the length of the array
System.out.println(myArray.length);

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

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

/*
* Reminder!
Expand Down
27 changes: 27 additions & 0 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
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> list = new ArrayList<String>();

// Add 3 elements to the list (OK to do one-by-one)
list.add("i0");
list.add("i1");
list.add("i2");

// 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, "replaced");
System.out.println(list);

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

// Check whether the list contains a certain string
if (list.contains("insertNew"))
{
System.out.println("insertNew exists in list.");
}

// 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("Index: : " + i + ", Value: " + list.get(i).toString());
}

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

// Iterate over the list using a for-each loop
// Print each value on a second line
for (String item : list)
{
System.out.println("for-each loop: " + item + "\n");
}

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

import java.util.Map;
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
Map<String, Integer> myMap = new HashMap<String, Integer>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
myMap.put("item_0", 0);
myMap.put("item_1", 1);
myMap.put("item_2", 2);
System.out.println(myMap);

// Get the value associated with a given key in the Map
System.out.println(myMap.get("item_1"));

// Find the size (number of key/value pairs) of the Map
System.out.println(myMap.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
myMap.put("item_1", 99);
System.out.println(myMap);

// Check whether the Map contains a given key
if (myMap.containsKey("item_7"))
{
System.out.println(true);
}
else
{
System.out.println(false);
}

// Check whether the Map contains a given value
if (myMap.containsValue(99))
{
System.out.println(true);
}
else
{
System.out.println(false);
}

// Iterate over the keys of the Map, printing each key
for (String key : myMap.keySet())
{
System.out.println("for-each key: " + key);
}

// Iterate over the values of the map, printing each value
for (Integer value : myMap.values())
{
System.out.println("for-each value: " + value);
}

// Iterate over the entries in the map, printing each key and value
for (String keyEntry : myMap.keySet())
{
System.out.println("Key: " + keyEntry + " & the value: " + myMap.get(keyEntry));
}

/*
* Usage tip!
Expand Down
17 changes: 17 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float num = -3.44f;
System.out.println(num);

// Create an int with a positive value and assign it to a variable
int x = 2;
System.out.println(x);

// Use the modulo % operator to find the remainder when the int is divided by 3
int remainder = 7%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.
int numEorO = 3;
if (numEorO % 2 == 0)
{
System.out.println("This number is even: " + numEorO);
}
else
{
System.out.println("This number is odd: " + numEorO);
}

// Divide the number by another number using integer division
int result = numEorO/x;
System.out.println("int division result: " + result);

/*
* Reminder!
Expand Down
27 changes: 24 additions & 3 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
* the Person class.
*/

public class Person {
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 n, int a)
{
name = n;
age = a;
}


// Create a toString method that gives the name and age of the person

public String toString()
{
return "Name: " + name.toString() + ", Age: " + String.valueOf(age);
}

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

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person mark = new Person("Mark", 30);

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

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

// Get the name of the first person and store it in a local variable
String johnsName = john.name;
System.out.println(johnsName);

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

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

/**
* Terminology!
Expand Down
27 changes: 27 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
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> mySet = new HashSet<String>();

// Add 3 elements to the set
// (It's OK to do it one-by-one)
for (int i=0; i<3; i++)
{
mySet.add("element" + i);
}
System.out.println(mySet);

// Check whether the Set contains a given String
if (mySet.contains("element2"))
{
System.out.println(true);
}
else
{
System.out.println(false);
}

// Remove an element from the Set
mySet.remove("element2");
System.out.println(mySet);

// Get the size of the Set
System.out.println(mySet.size());

// Iterate over the elements of the Set, printing each one on a separate line
String[] myArray = new String[2];
mySet.toArray(myArray);

for (int i=0; i<mySet.size(); i++)
{
System.out.println("Iteration: " + myArray[i]);
}

/*
* Warning!
Expand Down
36 changes: 36 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
import java.util.List;
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 word = "hello";

// Find the length of the string
System.out.println(word.length());

// Concatenate (add) two strings together and reassign the result
word = "NewWord";
System.out.println(word);

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

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
if (word.contains("New"))
{
System.out.println(true);
}
else
{
System.out.println(false);
}

// Iterate over the characters of the string, printing each one on a separate line
for (int i = 0; i < word.length(); i++)
{
System.out.println(word.charAt(i));
}

// Create an ArrayList of Strings and assign it to a variable
List<String> myArray = new ArrayList<String>();

// Add multiple strings to the List (OK to do one-by-one)
for (int i=0; i < 4; i++)
{
myArray.add("String" + i);
}
System.out.println(myArray);

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

// Check whether two strings are equal
if (word.equals("NewWord"))
{
System.out.println(true);
}
else
{
System.out.println(false);
}

/*
* Reminder!
Expand Down
Loading