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

// Set the value of the array at each index to be a different String
words={"word1","word2","word3","word4"};
// It's OK to do this one-by-one

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

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

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

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

/*
* Reminder!
Expand Down
41 changes: 35 additions & 6 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
public class ListPractice {
import java.util.ArrayList;
import java.util.Collection;
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> daysOfTheWeek = new ArrayList<String>();
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)
daysOfTheWeek.add("Sunday");
daysOfTheWeek.add("Monday");
daysOfTheWeek.add("Tusday");

// Print the element at index 1

System.out.println(daysOfTheWeek.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)
daysOfTheWeek.set(1, "Saturday");

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

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

boolean found = false;
if daysOfTheWeek.contains("Monday"){
found = true;

}else{
found = false
}
System.out.println(found);
// Iterate over the list using a traditional for-loop.
for(int i = 0; i < daysOfTheWeek.size(); i++){

// Print each index and value on a separate line
System.out.println(daysOfTheWeek.get(i));
}

// Sort the list using the Collections library
Collections.sort(daysOfTheWeek);

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


/*
* Usage tip!
*
* Use a traditional for-loop when you need to use the index or you need to iterate in an
* 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
* Otherwise, if you're iterating the in the conventional order and don't need
* the
* index values a for-each loop is cleaner.
*/
}
Expand Down
43 changes: 34 additions & 9 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@

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> studentRecord = new HashMap<>();
// assign it to a variable of type Map
Map<String, Integer> studentRecordMap = studentRecord;
// Put 3 different key/value pairs in the Map

studentRecordMap.put("Saron", 112233);
studentRecordMap.put("Goitom", 123456);
studentRecordMap.put("Babi", 2345);

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

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

studentRecordMap.get("Saron");
// 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)
studentRecordMap.size();
// Replace the value associated with a given key (the size of the Map shoukld
studentRecordMap.replace("Babi", 24567);
// not change)

// Check whether the Map contains a given key

String key_search = "Azeb";
int value_search_search = 909;
boolean containsKey = studentRecordMap.containsKey(key_search);
System.out.println(containsKey);
// Check whether the Map contains a given value

boolean containesValue = studentRecordMap.containsValue(value_search_search);
System.out.println(containesValue);
// Iterate over the keys of the Map, printing each key
for (String i : studentRecordMap.keySet()) {
System.out.println(i);
}

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

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

/*
* Usage tip!
*
* Maps are great when you want a specific key to value mapping.
* Example: The key could be a person's name, and the value could be their phone number
* Example: The key could be a person's name, and the value could be their phone
* number
*
* However if your keys are simple ascending 0-indexed integers with no gaps
* (0, 1, 2, 3, 4...) then an array or List is likely a better choice.
* Example: If you want to store the order of songs in a playlist.
*
* If you're finding that you're just wanting to store unordered values and the keys
* If you're finding that you're just wanting to store unordered values and the
* keys
* are unimportant, a Set may be a better choice.
* Example: If you want to hold the student ID numbers of everyone in a course,
* and you don't care about any ordering.
Expand Down
13 changes: 12 additions & 1 deletion src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,23 +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 negativeFloat = -1.4F;

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

// Use the modulo % operator to find the remainder when the int is divided by 3
int remainder = positiveInt % 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 (positiveInt % 2 == 0) {
System.out.println("Number is even");

}
if (positiveInt % 2 != 0) {
System.out.println("Number is Odd");
}

// Divide the number by another number using integer division
int intdivision = positiveInt / 7;

/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* Example:
* 7 / 3 = 2 when performing int division
*/

Expand Down
44 changes: 37 additions & 7 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@
* the Person class.
*/

import java.time.LocalDate;

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


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;
}

public String getName() {
return name;
}

public int getAge() {
return 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,39 +45,52 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)

public int birthYear(int age) {
int currentYear = LocalDate.now().getYear();
int yearBorn = currentYear - age;
return yearBorn;
}

public static void main(String[] args) {
// Create an instance of Person
Person person1 = new Person("Saron", 21);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Babi", 28);

// 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
person1.getName();

// 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 currentyear = person1.birthYear(25);

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

System.out.println(currentyear);
/**
* 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.
* 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
* 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.
* Each instance has its own instance variables: Auberon's age can be different
* from Baya's age.
*/
}
}
19 changes: 17 additions & 2 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import java.util.HashSet;

public class SetPractice {
public static void main(String[] args) {
// Create a HashSet of Strings and assign it to a variable of type Set
HashSet<String> books = new HashSet<>();

// Add 3 elements to the set
books.add("To Kill a Mockingbird");
books.add("The Lord of the Flies");
books.add("The Lord of the Rings");
// (It's OK to do it one-by-one)

// Check whether the Set contains a given String
books.contains("The Lord of the Flies");

// Remove an element from the Set
books.remove("To Kill a Mockingbird");

// Get the size of the Set
books.size();

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

}

/*
* Warning!
*
* The iteration order over the items in a HashSet is NOT GUARANTEED.
*
* Even running the exact same program multiple times may give different results.
* Even running the exact same program multiple times may give different
* results.
* Do not use a HashSet if order is important! You can use a TreeSet if you
* want items in sorted order, or an array or List if you want them in a specified
* want items in sorted order, or an array or List if you want them in a
* specified
* order.
*
* Also remember that sets do NOT have duplicates.
Expand Down
Loading