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
28 changes: 22 additions & 6 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] arrayOfStrings = {"None", "None", "None", "None"};

// Set the value of the array at each index to be a different String
arrayOfStrings[0] = "I";
arrayOfStrings[1] = "Was";
arrayOfStrings[2] = "In";
arrayOfStrings[3] = "Class";

// It's OK to do this one-by-one

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

// Get the length of the array

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

for(int i = 0; i < item; i++)
{
System.out.println(arrayOfStrings[i]);
}

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

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

Reminder!
Arrays start at index 0*/
}
}
51 changes: 37 additions & 14 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
import java.util.ArrayList;
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> EmptyList = 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.

Remember to use interface types where appropriate (List)

Also, in general we have our variables and methods in Java be in camelCase, where the first letter is lowercase. We have classes and interfaces be capitalized.


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

EmptyList.add("Hello");
EmptyList.add("GoodBye");
EmptyList.add("See you around");
// Print the element at index 1

System.out.println(EmptyList.get(2));
// Replace the element at index 1 with a new value
EmptyList.set(1, "GoodMorning");
// (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)

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

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

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


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

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

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.*/
}
}
56 changes: 38 additions & 18 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,63 @@

import java.util.HashMap;

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> workingMap = new HashMap<>();
Copy link
Copy Markdown

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 where appropriate (Map)


// Put 3 different key/value pairs in the Map
workingMap.put("Words", 10);
workingMap.put("fun", 20);
workingMap.put("excited", 30);
// (it's OK to do this one-by-one)

System.out.println(workingMap);

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

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

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

workingMap.replace("fun", 15);
System.out.println(workingMap);
// Check whether the Map contains a given key

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

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

for(int i = 0; i < workingMap.size(); i++)
{
System.out.println(workingMap.keySet());
}
// Iterate over the values of the map, printing each value


for(int i = 0; i < workingMap.size(); i++)
{
System.out.println(workingMap.values());
}
// Iterate over the entries in the map, printing each key and value

for(int i = 0; i < workingMap.size(); i++)
{
System.out.println(workingMap);
}
Comment on lines +34 to +49
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These don't quite work. This goes and repeatedly prints out all of the keys, then all of the values, then the entire set. We want them one at a time. Consider using a for-each loop for these. Make sure to run your code to check whether it does what you're wanting.

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

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
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
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.*/
}
}
42 changes: 42 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,59 @@ public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float negativeValue = -1.0f;

float applyNegative = negativeValue;

System.out.println(applyNegative);

// Create an int with a positive value and assign it to a variable

int positiveValue = 10;

int applyPostive = positiveValue;

System.out.println(applyPostive);

// Use the modulo % operator to find the remainder when the int is divided by 3

int equation = 7 % 2;

System.out.println(equation);

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

int value = 100;

if(value % 2 == 0)
{
System.out.println("Even");
}

// Use an if-else to print "Even" if the number is even and "Odd"
// if the number is odd.

for(int i = 0; i < 100; i++)
{
if(i % 2 == 0)
{
System.out.println("Even" + "" + i);
}
else if(i % 3 == 0)
{
System.out.println("Odd" + "" + i);
}
}


// Divide the number by another number using integer division

int numberValue = 5;
int divideValue = 10;

System.out.println(divideValue / numberValue);

/*
* Reminder!
*
Expand Down
26 changes: 25 additions & 1 deletion src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,32 @@

public class Person {
// Declare a public String instance variable for the name of the person
String name;
// Declare a private int instance variable for the age of the person

int age;
Comment on lines +8 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You need to public and private access modifiers to make these have the access specified by the comments.


// Create a constructor that takes the name and age of the person
// and assigns it to the instance variables
public Person(int personAge, String personName)
{
age = personAge;
name = personName;
}


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

public int birthYear(int year)
{
return year - age;
}
// There should NOT be any print statement in this method.
/**
* birthYear returns the year the person was born.
Expand All @@ -32,21 +47,30 @@ public class Person {

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

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

// 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
String fName = person1.name;
System.out.println(fName);

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

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

/**
* Terminology!
Expand Down
18 changes: 18 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
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> hashSetValue = new HashSet<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.

Remember to use interface types where appropriate (Set)


// Add 3 elements to the set
// (It's OK to do it one-by-one)
hashSetValue.add("red");
hashSetValue.add("blue");
hashSetValue.add("green");

// Check whether the Set contains a given String

System.out.println(hashSetValue.contains("red"));

// Remove an element from the Set

System.out.println(hashSetValue);
hashSetValue.remove("red");
System.out.println(hashSetValue);

// Get the size of the Set

System.out.println(hashSetValue.size());

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

/*
* Warning!
Expand Down
Loading