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
22 changes: 16 additions & 6 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
/* Name: Anthony Kravchishin */
import java.util.*;

public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] list = 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

list[0] = "First";
list[1] = "Second";
list[2] = "Third";
list[3] = "end";
// Get the value of the array at index 2

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

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

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

for (String item : list) {
System.out.println(item);
}
/*
* Reminder!
*
Expand Down
26 changes: 17 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
/* Name: Anthony Kravchishin */
import java.util.*;
public class ListPractice {


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

List<String> list = new ArrayList<>();
// Add 3 elements to the list (OK to do one-by-one)

list.add("One");
list.add("Two");
list.add("Three");
// Print the element at index 1

System.out.println(list.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)

list.set(1, "Not Two");
// Insert a new element at index 0 (the length of the list will change)

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

System.out.println(list.contains("Two"));
// 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);
// Iterate over the list using a for-each loop
// Print each value on a second line

for (String item : list) {
System.out.println(item);
}
/*
* Usage tip!
*
Expand Down
32 changes: 20 additions & 12 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@


/* Name: Anthony Kravchishin */
import java.util.*;
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> map = new HashMap<>();
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

map.put("First", 1);
map.put("Second", 2);
map.put("Third", 3);
// Get the value associated with a given key in the Map

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

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

map.replace("First", 11);
// Check whether the Map contains a given key

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

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

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

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

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

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

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

System.out.println(positive%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)
System.out.println(positive%2);
// Use an if-else to print "Even" if the number is even and "Odd"
// if the number is odd.

if (positive%2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
// Divide the number by another number using integer division

System.out.println(positive/5);
/*
* Reminder!
*
Expand Down
48 changes: 39 additions & 9 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Name: Anthony Kravchishin */
import java.util.*;
/*
* In this file you will follow the comments' instructions to complete
* the Person class.
Expand All @@ -6,13 +8,37 @@
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;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

// Create a toString method that gives the name and age of the person
@Override
public String toString() {
return "Person [" + name + ", " + age + "]";
}


// Implement the below public instance method "birthYear"
Expand All @@ -28,26 +54,29 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)

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

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

Person p = new Person("Quinn", 21);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person q = new Person("Paul", 33);
// Print the first person

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

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

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

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

}
18 changes: 12 additions & 6 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
/* Name: Anthony Kravchishin */
import java.util.*;
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> set = new HashSet<>();
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 set
// (It's OK to do it one-by-one)

set.add("Small");
set.add("Medium");
set.add("Large");
// Check whether the Set contains a given String

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

set.remove("Large");
// Get the size of the Set

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

for (String s : set) {
System.out.println(s);
}
/*
* Warning!
*
Expand Down
28 changes: 18 additions & 10 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
/* Name: Anthony Kravchishin */
import java.util.*;
public class StringPractice {
public static void main(String[] args) {
// Create a string with at least 5 characters and assign it to a variable

String str = "Hello";
// Find the length of the string

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

str += " world";
System.out.println(str);
// Find the value of the character at index 3

System.out.println(str.charAt(3));
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

System.out.println(str.contains("abc"));
// Iterate over the characters of the string, printing each one on a separate line

for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

List<String> list = new ArrayList<>();
// Add multiple strings to the List (OK to do one-by-one)

list.add("This");
list.add("is");
list.add("list");
list.add("This");
// 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

System.out.println(String.join(",", list));
// Check whether two strings are equal

System.out.println(list.get(0) == list.get(3));
/*
* Reminder!
*
Expand Down