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
22 changes: 22 additions & 0 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import java.util.Arrays;
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] strArray = {"red", "orange", "yellow", "green"};
System.out.println(Arrays.toString(strArray));

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
strArray[0] = "blue";
strArray[1] = "purple";
strArray[2] = "red";
strArray[3] = "orange";
System.out.println(Arrays.toString(strArray));

// Get the value of the array at index 2
System.out.println("The value at index 2 is " + strArray[2]); // expect "The value at index 2 is red"

// Get the length of the array
System.out.print("The array length is ");
System.out.print(strArray.length);
// expects "The array length is 4"
System.out.println();


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

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

/*
* Reminder!
Expand Down
33 changes: 33 additions & 0 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
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<String>();
System.out.println(stringList);

// Add 3 elements to the list (OK to do one-by-one)
stringList.add("banana");
stringList.add("mango");
stringList.add("lemon");
System.out.println(stringList);

// Print the element at index 1
System.out.println(stringList.get(1)); // should print "mango"

// 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, "kiwi");
System.out.println(stringList); // should print "[banana, kiwi, lemon]"

// Insert a new element at index 0 (the length of the list will change)
stringList.add(0, "strawberry");
System.out.println(stringList); // should print "[strawberry, banana, kiwi, lemon]"

// Check whether the list contains a certain string
if (stringList.contains("banana")) {
System.out.println("That's bananas!"); // expected output
} else {
System.out.println("No bananas here.");
}

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

// Sort the list using the Collections library
Collections.sort(stringList);
System.out.println(stringList); // should print "[banana, kiwi, lemon, strawberry]"
System.out.println();

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

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

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> simpsons = new HashMap<String, Integer>();
System.out.println(simpsons);

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
simpsons.put("Homer", 38);
simpsons.put("Marge", 36);
simpsons.put("Bart", 10);
simpsons.put("Lisa", 8);
simpsons.put("Maggie", 1);
Comment on lines +13 to +17
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Fun!

System.out.println(simpsons);

// Get the value associated with a given key in the Map
System.out.println(simpsons.get("Maggie")); // expect 1

// Find the size (number of key/value pairs) of the Map
System.out.print("The map size is ");
System.out.println(simpsons.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
System.out.print("It's Lisa's birthday! Now she is: ");
simpsons.put("Lisa", simpsons.get("Lisa") + 1);
System.out.println(simpsons.get("Lisa"));

// Check whether the Map contains a given key
if (simpsons.containsKey("Homer")) {
System.out.println("Homer is a Simpson.");
} else {
System.out.println("Where did Homer go??");
}

// Check whether the Map contains a given value
if (simpsons.containsValue(42)) {
System.out.println("42 is a cool age!");
} else {
System.out.println("None of the Simpsons are 42.");
}

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

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

// Iterate over the entries in the map, printing each key and value
for (Map.Entry<String,Integer> person : simpsons.entrySet()) {
System.out.println(person.getKey() + " is " + person.getValue() + " years old.");
}

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

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

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

// Divide the number by another number using integer division
int dividend = remainder / 2;
System.out.println(dividend);

/*
* Reminder!
Expand Down
26 changes: 21 additions & 5 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
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 + " " + age;
}

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

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person ari = new Person("Ari", 1);

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

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

// Get the name of the first person and store it in a local variable
String myFirstName = rebecca.name;
System.out.println("My first name is " + myFirstName);

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

// In a separate statement, print the local variable holding the birth year.
System.out.println("If my birthday had already happened this year, my birth year would be " + birthYear);

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

// Add 3 elements to the set
// (It's OK to do it one-by-one)
movies.add("Anna and the Apocalypse");
movies.add("Wicked");
movies.add("Lilo and Stitch");
movies.add("Memento");
System.out.println(movies);

// Check whether the Set contains a given String
if (movies.contains("The Matrix")){
System.out.println("INSERT QUOTE FROM THE MATRIX HERE");
} else {
System.out.println("You should add 'The Matrix' to this movie collection!");
}

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

// Get the size of the Set
System.out.print("The size of this set is ");
System.out.println(movies.size());

// Iterate over the elements of the Set, printing each one on a separate line
for (String movie : movies) {
System.out.println(movie);
}

/*
* Warning!
Expand Down
53 changes: 53 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,66 @@
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 greeting = "Hello";
System.out.println(greeting);

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

// Concatenate (add) two strings together and reassign the result
String name = "Rebecca";
String nameGreeting = greeting + ", " + name + "!";
System.out.println(nameGreeting);

// Find the value of the character at index 3
System.out.println(nameGreeting.charAt(3)); // expect 'l'

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
boolean hasABitOfCalifornia = nameGreeting.contains("ca");

if (hasABitOfCalifornia) {
System.out.println("There's a little bit of California in that! ('ca')"); // expected output
} else {
System.out.println("No California here!");
}
Comment on lines +24 to +28
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

😛


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

// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> zoo = new ArrayList<>();
Copy link
Copy Markdown
Owner

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 (List)

System.out.println(zoo);

// Add multiple strings to the List (OK to do one-by-one)
zoo.add("zebra");
zoo.add("giraffe");
zoo.add("penguin");
zoo.add("tiger");
System.out.println(zoo);

// 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 listOfAnimals = zoo.toString();
listOfAnimals = listOfAnimals.substring(1, listOfAnimals.length() - 1);
System.out.println(listOfAnimals);


// Check whether two strings are equal
String string1 = "apples";
String string2 = "apples";
String string3 = "oranges";

System.out.println(compareTwo(string1, string2));
System.out.println(compareTwo(string1, string3));




/*
* Reminder!
Expand All @@ -29,4 +70,16 @@ public static void main(String[] args) {
* We use == when comparing primitives (e.g. int or char).
*/
}

public static String compareTwo (String a, String b) {
String compared = "";
if (a.contentEquals(b)) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Today I learned! I didn't know contentEquals existed. Neat!

compared = "Comparing apples to apples!";
} else {
compared = "Not comparing apples to apples!";
}
return compared;
}
}


Loading