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
37 changes: 24 additions & 13 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@


public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
// Create an array of Strings of size 4
String[] fruits = 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
// Set the value of the array at each index to be a different String
fruits[0] = "Apple";
fruits[1] = "Banana";
fruits[2] = "Cherry";
fruits[3] = "Date";

// Get the value of the array at index 2
// Get the value of the array at index 2
System.out.println("Element at index 2: " + fruits[2]); // Expected: Cherry

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

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

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

/*
* Reminder!
*
* Arrays start at index 0
*/
// Iterate over the array using a for-each loop and print out each item
System.out.println("Using for-each loop:");
for (String fruit : fruits) {
System.out.println(fruit); // Prints each fruit
}
}
}

80 changes: 46 additions & 34 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import java.util.ArrayList;
import java.util.List;
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

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

// Print the element at index 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)

// Insert a new element at index 0 (the length of the list will change)

// Check whether the list contains a certain string

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

// Sort the list using the Collections library

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

/*
* Usage tip!
*
* Use a traditional for-loop when you need to use the index or you need to iterate in an
* unconventional order (e.g. backwards)
*
* Otherwise, if you're iterating the in the conventional order and don't need the
* index values a for-each loop is cleaner.
*/
}
}
public static void main(String[] args) {
// Create an empty ArrayList of Strings and assign it to a variable of type List
List<String> myList = new ArrayList<>();

// Add 3 elements to the list (OK to do one-by-one)
myList.add("Apple");
myList.add("Banana");
myList.add("Cherry");

// Print the element at index 1
System.out.println("Element at index 1: " + myList.get(1)); // Expected: Banana

// Replace the element at index 1 with a new value
myList.set(1, "Grapes"); // Replaces "Banana" with "Grapes"
System.out.println("List after replacement: " + myList); // Expected: [Apple, Grapes, Cherry]

// Insert a new element at index 0 (the length of the list will change)
myList.add(0, "Orange"); // Inserts "Orange" at the beginning
System.out.println("List after insertion: " + myList); // Expected: [Orange, Apple, Grapes, Cherry]

// Check whether the list contains a certain string
boolean containsApple = myList.contains("Apple");
System.out.println("Does the list contain 'Apple'? " + containsApple); // Expected: true

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

// Sort the list using the Collections library
Collections.sort(myList); // Sorts the list alphabetically
System.out.println("List after sorting: " + myList); // Expected: [Apple, Cherry, Grapes, Orange]

// Iterate over the list using a for-each loop
// Print each value on a second line
System.out.println("Using for-each loop:");
for (String item : myList) {
System.out.println(item); // Prints each value on a new line
}
}
}
58 changes: 44 additions & 14 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@

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)
// Method to create and return a Map
public static Map<String, Integer> createMap() {
Map<String, Integer> ages = new HashMap<>();
ages.put("Elena", 27);
ages.put("Demon", 30);
ages.put("Shams", 23);
return ages;
}

public static void main(String[] args) {
// Create the map and populate it
Map<String, Integer> myMap = createMap();

// Get the value associated with a given key in the Map
System.out.println("Age of Elena: " + myMap.get("Elena"));

// Find the size (number of key/value pairs) of the Map
System.out.println("Size of the map: " + myMap.size());
// Replace the value associated with a given key
myMap.put("Demon", 31); // Changing Demon’s age to 31
System.out.println("Updated map: " + myMap);

// Get the value associated with a given key in the Map
// Check whether the Map contains a given key
System.out.println("Does the map contain 'Shams'? " + myMap.containsKey("Shams"));

// Find the size (number of key/value pairs) of the Map
// Check whether the Map contains a given value
System.out.println("Does the map contain the value 30? " + myMap.containsValue(30));

// Replace the value associated with a given key (the size of the Map shoukld not change)
// Iterate over the keys of the Map, printing each key
System.out.println("Keys in the map:");
for (String key : myMap.keySet()) {
System.out.println(key);
}

// Check whether the Map contains a given key
// Iterate over the values of the map, printing each value
System.out.println("Values in the map:");
for (Integer value : myMap.values()) {
System.out.println(value);
}

// Check whether the Map contains a given value
// Iterate over the entries in the map, printing each key and value
System.out.println("Entries in the map (key and value):");
for (Map.Entry<String, Integer> entry : myMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}



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

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

// Create an int with a positive value and assign it to a variable
// 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
// 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 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
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

/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* 7 / 3 = 2 when performing int division
*/
// Divide the number by another number using integer division
int result = positiveInt / 3;
System.out.println("Result of integer division of " + positiveInt + " by 3: " + result);

/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* 7 / 3 = 2 when performing int division
*/
}
}
106 changes: 58 additions & 48 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
/*
* In this file you will follow the comments' instructions to complete
* the Person class.
*/

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;

// Create a constructor that takes the name and age of the person
// and assigns it to the instance variables
// Declare a private int instance variable for the age of the person
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
@Override
public String toString() {
// This method returns a string that represents the Person object with their name and age
return name + " (" + age + " years old)";
}


// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
// Implement the public instance method "birthYear"
// This method returns the year the person was born
/**
* birthYear returns the year the person was born.
*
* The birth year is calculated by subtracting the person's age from currentYear
* The birth year is calculated by subtracting the person's age from the currentYear
* that's passed in as an int. It assumes that the person's birthday has already
* passed this year.
*
* @param currentYear an int for the current year
* @return The year the person was born
*/
// (create the instance method here)

public int birthYear(int currentYear) {
// Calculate the birth year by subtracting the person's age from the current year
return currentYear - age;
}

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

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

// Print the first person

// Print the second person

// Get the name of the first person and store it in a local variable

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

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

/**
* Terminology!
*
* A class is the overall definition, like a blueprint.
* An instance is a specific object made according to that definition.
* We use "instance" and "object" to mean the same thing.
*
* For example, if there is a Person class, we can make an instance of a specific person: Auberon.
*
* There can be many instances for the same class. For example: Auberon, Xinting, Baya are all
* different instances of the Person class.
*
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
*/
}
}
// Create an instance of Person with the name "Elena" and age 27
Person person1 = new Person("Elena", 27);

// Create another instance of Person with a different name and age
Person person2 = new Person("Demon", 30);

// Print the first person (calls toString method)
System.out.println(person1); // This will print: Elena (27 years old)

// Print the second person (calls toString method)
System.out.println(person2); // This will print: John (30 years old)

// Get the name of the first person and store it in a local variable
String personName = person1.name; // personName will be "Elena"

// Using the birthYear method, get the birth year of the first person
// and store it in a local variable. Input the current year (e.g., 2025) as the argument.
int birthYearOfPerson1 = person1.birthYear(2025);

// In a separate statement, print the local variable holding the birth year.
System.out.println(personName + "'s birth year: " + birthYearOfPerson1); // This will print: Elena's birth year: 1998

/**
* Terminology!
*
* A class is the overall definition, like a blueprint.
* An instance is a specific object made according to that definition.
* We use "instance" and "object" to mean the same thing.
*
* For example, if there is a Person class, we can make an instance of a specific person: Auberon.
*
* There can be many instances for the same class. For example: Auberon, Xinting, Baya are all
* different instances of the Person class.
*
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
*/
}
}
Loading