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

// Set the value of the array at each index to be a different String
stringArray[0]= "one";
stringArray[1]="Two";
stringArray[2]="Three";
stringArray[3]="Four";
// It's OK to do this one-by-one

// Get the value of the array at index 2
System.out.println("Value at index 2: " + stringArray[2]);

// Get the length of the array
System.out.println("Length of the array: " + stringArray.length);

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

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

System.out.println("for-each loop: ");
for(String number: stringArray ){
System.out.println(number);
}
/*
* Reminder!
*
Expand Down
30 changes: 23 additions & 7 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
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> stringList = new ArrayList<>();
// Add 3 elements to the list (OK to do one-by-one)
stringList.add("Apple");
stringList.add("Banana");
stringList.add("Cherry");

// Print the element at index 1
System.out.println("Element at index 1: " + stringList.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)

stringList.set(1, "Blueberry");
// Insert a new element at index 0 (the length of the list will change)

stringList.add(0, "Avocado");
// Check whether the list contains a certain string

boolean containsCherry = stringList.contains("Cherry");
System.out.println("Does the list contain 'Cherry'? " + containsCherry);
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

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

Collections.sort(stringList);
System.out.println("\nSorted list: " + stringList);
Comment on lines +29 to +35
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here and elsewhere the indentation is a bit off. Make sure to realign your code to make it easier to read

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

System.out.println("\nFor-each loop:");
for (String fruit : stringList) {
System.out.println(fruit);
}
/*
* Usage tip!
*
Expand Down
66 changes: 43 additions & 23 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@

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

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

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

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

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

// Check whether the Map contains a given key

// Check whether the Map contains a given value

// 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

// Create a HashMap with String keys and Integer values and assign it to a variable of type Map
Map<String, Integer> map = new HashMap<>();

// Put 3 different key/value pairs in the Map (it's OK to do this one-by-one)
map.put("Apple", 5);
map.put("Banana", 3);
map.put("Cherry", 8);

// Get the value associated with a given key in the Map
System.out.println("Value associated with 'Banana': " + map.get("Banana"));

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

// Replace the value associated with a given key (the size of the Map should not change)
map.put("Banana", 6);
System.out.println("Updated value for 'Banana': " + map.get("Banana"));

// Check whether the Map contains a given key
System.out.println("Does the map contain key 'Apple'? " + map.containsKey("Apple"));

// Check whether the Map contains a given value
System.out.println("Does the map contain value 8? " + map.containsValue(8));

// Iterate over the keys of the Map, printing each key
System.out.println("\nKeys in the map:");
for (String key : map.keySet()) {
System.out.println(key);
}

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

// Iterate over the entries in the map, printing each key and value
System.out.println("\nEntries in the map:");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
/*
* Usage tip!
*
Expand Down
15 changes: 12 additions & 3 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float negativeFloat = -7.5f;

// Create an int with a positive value and assign it to a variable
int positiveInt = 10;

// Use the modulo % operator to find the remainder when the int is divided by 3
int remainder = positiveInt % 3;
System.out.println("Remainder when " + positiveInt + " is divided by 3: " + 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.
// 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(positiveInt + " is Even");
} else {
System.out.println(positiveInt + " is Odd");
}

// Divide the number by another number using integer division

int divisionResult = positiveInt / 3;
System.out.println(positiveInt + " divided by 3 (integer division): " + divisionResult);
/*
* Reminder!
*
Expand Down
28 changes: 20 additions & 8 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@

public class Person {
// Declare a public String instance variable for the name of the person
public String name;
// Declare a private int instance variable for the age of the person

public 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

@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,26 +35,31 @@ 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("Alice", 23);

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

Person person2 = new Person("Bob", 25);
// 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 personName = 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 birthYear = person1.birthYear(2025);

// In a separate statement, print the local variable holding the birth year.

System.out.println(personName + " was born in: " + birthYear);
/**
* Terminology!
*
Expand Down
23 changes: 19 additions & 4 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
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<>();
// Add 3 elements to the set
mySet.add("Apple");
mySet.add("Banana");
mySet.add("Cherry");

// (It's OK to do it one-by-one)

// Check whether the Set contains a given String

boolean containsApple = mySet.contains("Apple");
System.out.println("Set contains 'Apple': " + containsApple);
// Remove an element from the Set

mySet.remove("Banana");
System.out.println("After removing 'Banana': " + mySet);
// Get the size of the Set
int size = mySet.size();
System.out.println("The size of the Set is: " + size);

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

// Iterate over the elements of the Set, printing each one on a separate line
System.out.println("Set elements:");
for (String item : mySet) {
System.out.println(item);
}
/*
* Warning!
*
Expand Down
33 changes: 26 additions & 7 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
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 myString = "Hello";
// Find the length of the string
int length = myString.length();
System.out.println("Length of the string: " + length);

// Concatenate (add) two strings together and reassign the result
myString = myString + " World";
System.out.println("After concatenation: " + myString);

// Find the value of the character at index 3

char charAtIndex3 = myString.charAt(3);
System.out.println("Character at index 3: " + charAtIndex3);
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

boolean containsSubstring = myString.contains("abc");
System.out.println("Contains 'abc': " + containsSubstring);
// Iterate over the characters of the string, printing each one on a separate line

System.out.println("Characters in the string:");
for (int i = 0; i < myString.length(); i++) {
System.out.println(myString.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

ArrayList<String> stringList = 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 (List)

// Add multiple strings to the List (OK to do one-by-one)

stringList.add("Apple");
stringList.add("Banana");
stringList.add("Cherry");
// 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(", ", stringList);
System.out.println("Joined string: " + joinedString);

// Check whether two strings are equal

String string1 = "hello";
String string2 = "hello";
boolean areEqual = string1.equals(string2);
System.out.println("Are the strings equal? " + areEqual);

/*
* Reminder!
*
Expand Down