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
25 changes: 23 additions & 2 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
public class ArrayPractice {
public static void main(String[] args) {
public class ArrayPractice
{
public static void main(String[] args)
{
// Create an array of Strings of size 4
String arrayItems[] = {"One", "Two", "Three", "Four"};
System.out.println(arrayItems[0] + " " + arrayItems[1] + " " + arrayItems[2] + " " + arrayItems[3]);

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
arrayItems[0] = "Java";
arrayItems[1] = "C++";
arrayItems[2] = "C#";
arrayItems[3] = "Python";

System.out.println(arrayItems[0] + " " + arrayItems[1] + " " + arrayItems[2] + " " + arrayItems[3]);

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

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

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

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

/*
* Reminder!
*
* Arrays start at index 0
*/
}

}
40 changes: 37 additions & 3 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
public class ListPractice {
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

public static void main(String[] args)
{
// Create an empty ArrayList of Strings and assign it to a variable of type List
List<String> listContainer = new ArrayList<String>();
System.out.println(listContainer);

// Add 3 elements to the list (OK to do one-by-one)
listContainer.add("One");
listContainer.add("Two");
listContainer.add("Three");
System.out.println(listContainer);

// Print the element at index 1
System.out.println(listContainer.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)
listContainer.set(1, "New");
System.out.println(listContainer);

// Insert a new element at index 0 (the length of the list will change)
listContainer.addFirst("Sky");
System.out.println(listContainer);

// Check whether the list contains a certain string
System.out.println(listContainer.contains("New"));

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

// Sort the list using the Collections library
Collections.sort(listContainer);
System.out.println(listContainer);

// Iterate over the list using a for-each loop
// Print each value on a second line
String listItems = "";

for (Object item : listContainer)
{
System.out.println(item);
listItems += (item + " ");
}

System.out.println(listItems);

/*
* Usage tip!
Expand All @@ -33,4 +66,5 @@ public static void main(String[] args) {
* index values a for-each loop is cleaner.
*/
}

}
35 changes: 32 additions & 3 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
import java.util.HashMap;
import java.util.Map;


public class MapPractice {
public static void main(String[] args) {
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> mapPractice = new HashMap<String, Integer>();
System.out.println(mapPractice);

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
mapPractice.put("Java", 27);
mapPractice.put("C", 11);
mapPractice.put("Python", 120);

System.out.println(mapPractice);

// Get the value associated with a given key in the Map
System.out.println(mapPractice.get("C"));

// Find the size (number of key/value pairs) of the Map
System.out.println(mapPractice.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
mapPractice.put("Python", 2);
System.out.println(mapPractice);

// Check whether the Map contains a given key
System.out.println(mapPractice.containsKey("Java"));

// Check whether the Map contains a given value
System.out.println(mapPractice.containsValue(27));

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

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

// Iterate over the entries in the map, printing each key and value
for (String keys : mapPractice.keySet())
{
System.out.println(keys + " and " + mapPractice.get(keys));
}
Comment on lines +50 to +53
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This works! In the future, you could also consider entrySet


/*
* Usage tip!
Expand All @@ -40,4 +68,5 @@ public static void main(String[] args) {
* and you don't care about any ordering.
*/
}

}
21 changes: 19 additions & 2 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
public class NumberPractice {
public static void main(String args[]) {
public class NumberPractice
{
public static void main(String args[])
{
// Create a float with a negative value and assign it to a variable
float negNum = -5.666f;
System.out.println(negNum);

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

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

// Divide the number by another number using integer division
System.out.println(posNum / 2);

/*
* Reminder!
Expand All @@ -22,4 +38,5 @@ public static void main(String args[]) {
*/

}

}
33 changes: 31 additions & 2 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
* the Person class.
*/

public class Person {
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
@Override
public String toString()
{
return name + ": " + age;
}


// Implement the below public instance method "birthYear"
Expand All @@ -29,24 +42,39 @@ public class Person {
*/
// (create the instance method here)

public int birthyear(int currentYear)
{
return currentYear - age;
}

public static void main(String[] args) {

public static void main(String[] args)
{
// Create an instance of Person
Person personOne = new Person("Clair", 32);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person personTwo = new Person("Jill", 35);

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

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

// Get the name of the first person and store it in a local variable
String personName = personOne.name;
System.out.println(personName);

// 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 born = personOne.birthyear(2025);
System.out.println(born);

// In a separate statement, print the local variable holding the birth year.
System.out.println(personOne.age);

/**
* Terminology!
Expand All @@ -63,4 +91,5 @@ public static void main(String[] args) {
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
*/
}

}
25 changes: 23 additions & 2 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
public class SetPractice {
public static void main(String[] args) {
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> hashItems = new HashSet<String>();
System.out.println(hashItems);

// Add 3 elements to the set
// (It's OK to do it one-by-one)
hashItems.add("One");
hashItems.add("Two");
hashItems.add("Three");

System.out.println(hashItems);

// Check whether the Set contains a given String
System.out.println(hashItems.contains("Two"));

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

// Get the size of the Set
System.out.println(hashItems.size());

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

/*
* Warning!
Expand All @@ -26,4 +46,5 @@ public static void main(String[] args) {
* Also remember that sets do NOT have duplicates.
*/
}

}
Loading