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

String arr[] = 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
arr[0] = "cake";
arr[1] = "cookies";
arr[2] = "pancake";
arr[3] = "cupcake";

// Get the value of the array at index 2

System.out.println(arr[2]);
System.out.println();
// Get the length of the array

System.out.println(arr.length);
System.out.println();
// Iterate over the array using a traditional for loop and print out each item

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

for(String dessert: arr){
System.out.println(dessert);
}
/*
* Reminder!
*
Expand Down
31 changes: 22 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import java.util.ArrayList;
import java.util.Collections;

public class ListPractice {


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

ArrayList<String> list = 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.

Here and elsewhere, use interface types (List, Map, etc.) where appropriate.
For example:
List<String> strings = new ArrayList<>();
Note that the type on the left is List, not ArrayList. When we do this, we're more flexible to be able to change our code to use a different type of list later.

Similarly for maps:
Map<String, String> myMap = new HashMap<>();
Note that on the left we use Map instead of HashMap.

And sets:
Set<String> strings = new HashSet<>();
Note that on the left we use Set instead of HashSet.

In summary:

  • interface type on left to declare type (List, Map, etc.)
  • Concrete type on right to instantiate instance (HashMap, ArrayList etc.)

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

list.add("Cake");
list.add("Ice Cream");
list.add("Macaron");
// Print the element at index 1

System.out.println(list.get(0));
// 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,"Bingsu");
System.out.println(list.get(1)+ " " + list.size());
// Insert a new element at index 0 (the length of the list will change)

list.add(0,"Cookies");
System.out.println(list+ " " + list.size());
// Check whether the list contains a certain string

System.out.println(list.contains("Cake") );
System.out.println();
// 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(list.get(i));
}
// Sort the list using the Collections library

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

for(String dessert : list){
System.out.println(dessert);
}
/*
* Usage tip!
*
Expand Down
41 changes: 30 additions & 11 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@

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

HashMap<String, Integer> foods = new HashMap<>();
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

foods.put("hot dog", 1);
foods.put("sushi", 2);
foods.put("pizza", 3);
foods.put("cake", 4);
System.out.println();
// Get the value associated with a given key in the Map

System.out.println(foods.get("sushi"));
System.out.println();
// Find the size (number of key/value pairs) of the Map

System.out.println(foods.size());
System.out.println();
// Replace the value associated with a given key (the size of the Map shoukld not change)

foods.put("hot dog", 2);
System.out.println(foods);
System.out.println();
// Check whether the Map contains a given key

System.out.println(foods.containsKey("sushi"));
System.out.println();
// Check whether the Map contains a given value

System.out.println(foods.containsValue(4));
System.out.println();
// Iterate over the keys of the Map, printing each key

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

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

for(Map.Entry<String, Integer> foodInventory : foods.entrySet()){
System.out.println(foodInventory);
}
/*
* Usage tip!
*
Expand Down
14 changes: 10 additions & 4 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float negativeFloat = -2.0f;
// Create an int with a positive value and assign it to a variable

int postiveValue = 4;
// Use the modulo % operator to find the remainder when the int is divided by 3

System.out.println(postiveValue%3);
// 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(postiveValue % 2 == 0){
System.out.println("even");
} else {
System.out.println("odd");
}

// Divide the number by another number using integer division

System.out.println(postiveValue/3);
/*
* Reminder!
*
Expand Down
36 changes: 24 additions & 12 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 = "Jerry";
private int age = 1000;
// 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 + " " + age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,26 +34,32 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)

public static int birthYear(int age){
return 2025 - age;
}

public static void main(String[] args) {
// Create an instance of Person

Person human = new Person("Timmy", 20);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person human2 = new Person("Joe", 30);
// Print the first person

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

System.out.println(human2.toString());
System.out.println();
// Get the name of the first person and store it in a local variable

String firstPersonsName = human.name;
System.out.println(firstPersonsName);
System.out.println();
// 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 firstPersonsBirthYear = birthYear(human.age);
// In a separate statement, print the local variable holding the birth year.

System.out.println(firstPersonsBirthYear);
/**
* Terminology!
*
Expand Down
24 changes: 18 additions & 6 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.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> hashbrown = new HashSet<>();
// Add 3 elements to the set
// (It's OK to do it one-by-one)

hashbrown.add("potatoes");
hashbrown.add("flour");
hashbrown.add("egg");
System.out.println();
// Check whether the Set contains a given String

System.out.println(hashbrown.contains("potatoes"));
System.out.println();
// Remove an element from the Set

hashbrown.remove("flour");
System.out.println(hashbrown);
System.out.println();
// Get the size of the Set

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

for(String Ingredient : hashbrown){
System.out.println(Ingredient);
}
System.out.println();
/*
* Warning!
*
Expand Down
55 changes: 45 additions & 10 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
import java.util.Arrays;
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 = "Chariot";
// Find the length of the string

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

String first = "Number";
String second = "Two";
String firstSecond = first+second;
System.out.println(firstSecond);
System.out.println();
// Find the value of the character at index 3

System.out.println(word.charAt(3));
System.out.println();
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
String Alphabet = "abc";
String noABC = "s";
if (Alphabet.contains("abc")) {
System.out.println("Alphabet contains \"abc\"");
}

if (noABC.contains("abc")) {
System.out.println("Alphabet contains \"abc\"");
} else{
System.out.println("noABC does not contain \"abc\"");
}
System.out.println();
// Iterate over the characters of the string, printing each one on a separate line

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

String dessert = "macaron";
for (int i = 0; i < dessert.length(); i++) {
System.out.println(dessert.charAt(i));
}
System.out.println();
// Create an ArrayList of Strings and assign it to a variable
// ArrayList<String> recipe = new ArrayList<String>(Arrays.asList("cheese","sugar","flour","egg whites"));
ArrayList<String> recipe = new ArrayList<String>();
System.out.println();
// Add multiple strings to the List (OK to do one-by-one)

recipe.add("cheese");
recipe.add("sugar");
recipe.add("flour");
recipe.add("egg whites");
System.out.println();
// 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

ArrayList<String> recipeMixed = new ArrayList<String>(Arrays.asList("cheese","sugar","flour","egg whites"));
System.out.println(recipeMixed);
System.out.println();
// Check whether two strings are equal

if(recipeMixed.get(0).equals(recipeMixed.get(1))){
System.out.println("Same ingredient");
}else{
System.out.println("Not the same");
}
/*
* Reminder!
*
Expand Down