Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
578fab0
Added float, int, and found 8/3 % 3 = 2. Addded print statements
ConnorJohnHughes Oct 2, 2025
d4a774e
Added If statement to check for even number, If/Else statment for odd…
ConnorJohnHughes Oct 2, 2025
b4c108b
Divided an int by another int
ConnorJohnHughes Oct 2, 2025
4580fc4
Created new Arraylist with List variable type. Added 3 elements one b…
ConnorJohnHughes Oct 2, 2025
3d74a5d
Replaced index 1, and added new element to index 0
ConnorJohnHughes Oct 2, 2025
dac66d7
Checked for a certain string, and for loop to print each element with…
ConnorJohnHughes Oct 2, 2025
0675e0f
Added what i can brush up on
ConnorJohnHughes Oct 2, 2025
9c07751
Sorted list, used for-each loop, and cleaned up code
ConnorJohnHughes Oct 2, 2025
cbc63df
Added Array with 4 strings and then chnaged values
ConnorJohnHughes Oct 2, 2025
991e8c1
Printed index value 2 and length of array
ConnorJohnHughes Oct 2, 2025
7efc01b
Printed tradional and for-each loop
ConnorJohnHughes Oct 2, 2025
0ddbe29
Added Arrays to work on list
ConnorJohnHughes Oct 2, 2025
a0842cd
Added a string with atleast 5 characters and found length
ConnorJohnHughes Oct 2, 2025
7273675
Concat two strings and assign new variable and found char at index 3
ConnorJohnHughes Oct 2, 2025
3631cf1
Checked if string conatined a certain char and loop/ print each char
ConnorJohnHughes Oct 2, 2025
c4d48b3
Created Arraylist and added 5 string elements
ConnorJohnHughes Oct 2, 2025
85722f3
Joined Strings together with commas and check for equal strings
ConnorJohnHughes Oct 2, 2025
a9f92ff
Added item to work on list
ConnorJohnHughes Oct 2, 2025
a6b5147
Commented out util
ConnorJohnHughes Oct 2, 2025
62d885c
Created new HashSet and added 5 elements
ConnorJohnHughes Oct 2, 2025
fb1f764
Check if set contained a string and removed element
ConnorJohnHughes Oct 2, 2025
ce3fbc5
Get size of Hashset and loop/ print each item
ConnorJohnHughes Oct 2, 2025
f99beb0
Created new Hashmap and added 3 keys/ values
ConnorJohnHughes Oct 2, 2025
7a97a55
Got value of index 2 and size of Hashmap
ConnorJohnHughes Oct 2, 2025
db740a0
Replaced value and checked if map contains a certain key
ConnorJohnHughes Oct 2, 2025
aceeaa9
Checked for certain value. Loop key, value and key/value and print
ConnorJohnHughes Oct 2, 2025
aa3472e
Created public string instance, private int instance and Person const…
ConnorJohnHughes Oct 2, 2025
5506c9e
Created toString method and birthyear instance method
ConnorJohnHughes Oct 2, 2025
6492a75
Edited constructor, created two people using constructor
ConnorJohnHughes Oct 2, 2025
17dda16
Fixed variables, toString and printed two different people
ConnorJohnHughes Oct 2, 2025
a41751c
Stored newName/birthyear in variable and printed
ConnorJohnHughes Oct 2, 2025
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ Most of these taks should be easy, but there will likely be some that ask you to
Make a Pull Request (PR) against the original repository. Copy the link into Canvas to submit.


## What i can work on:

1. For loops
2. Arrays
3. Built in methods
22 changes: 18 additions & 4 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import java.util.Arrays;

public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] ray = {"one", "two", "three", "four"};
System.out.println(Arrays.toString(ray) + '\n');

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one

ray[0]= "1";
ray[1]= "2";
ray[2]= "3";
ray[3]= "4";
System.out.println(Arrays.toString(ray) + "\n");
// Get the value of the array at index 2
System.out.println(ray[2] + "\n");

// Get the length of the array

System.out.println(ray.length + "\n");
// Iterate over the array using a traditional for loop and print out each item

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

for (String item: ray){
System.out.println(item);
}
/*
* Reminder!
*
Expand Down
26 changes: 26 additions & 0 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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("First");
list.add("Second");
list.add("Third");
System.out.println(list + "\n");


// Print the element at index 1
System.out.println(list.get(1) + "\n");

// 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,"Replaced");
System.out.println(list + "\n");

// Insert a new element at index 0 (the length of the list will change)
list.add(0, "Added");
System.out.println(list + "\n");

// Check whether the list contains a certain string
System.out.println(list.contains("Third") + "\n");

// 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(i + ": " + list.get(i));
}

// Sort the list using the Collections library
Collections.sort(list);
System.out.println("\n" + list + "\n");


// Iterate over the list using a for-each loop
// Print each value on a second line
for(String item : list){
System.out.println(item + "\n");
}

/*
* Usage tip!
Expand Down
24 changes: 22 additions & 2 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@

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

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.

In summary:

  • interface type on left to declare type (List, Map, etc.)
  • Concrete type on right to instantiate instance (HashMap, ArrayList etc.)



// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
map.put("Tacos", 5);
map.put("Burgers", 8);
map.put("Cheesecake", 10);
System.out.println(map + "\n");

// Get the value associated with a given key in the Map
System.out.println(map.get("Tacos") + '\n');

// Find the size (number of key/value pairs) of the Map
System.out.println(map.size() + "\n");

// Replace the value associated with a given key (the size of the Map shoukld not change)
map.replace("Tacos", 5, 7);
System.out.println(map + "\n");

// Check whether the Map contains a given key
System.out.println(map.containsKey("Tacos") + "\n");

// Check whether the Map contains a given value
System.out.println(map.containsValue(9) + "\n");

// Iterate over the keys of the Map, printing each key

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

// Iterate over the entries in the map, printing each key and value
map.forEach((key, value) -> {System.out.println(key + ", " + value);});

/*
* Usage tip!
Expand Down
19 changes: 19 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,24 @@ public static void main(String args[]) {
* 7 / 3 = 2 when performing int division
*/

float neg = -7;
System.out.println(neg);
int num = 8;
System.out.println(num);
System.out.println(num / 3 % 3);

int even = 10;
if (even % 2 == 0){
System.out.println("This number is even: " + even);
};

int n = 5;
if (n % 2 == 0){
System.out.println("This number is even: " + n);
}else{
System.out.println("This number is odd: " + n);
};

System.out.println(n / 2);
}
}
29 changes: 23 additions & 6 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@
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 = "Tom";
private int age = 32;




// 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
public String toString(){
return "This person's name is " + name + " and their age is " + age;
}


// Implement the below public instance method "birthYear"
Expand All @@ -28,26 +40,31 @@ 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 tom = new Person("Tom", 32);
// Create another instance of Person with a different name and age and
// assign it to a different variable
Person kari = new Person("Kari", 31);

// Print the first person

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

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

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

System.out.println(birthYear);
/**
* Terminology!
*
Expand Down
19 changes: 19 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
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> hs = new HashSet<>();

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

System.out.println(hs +"\n");

// Check whether the Set contains a given String
System.out.println(hs.contains("red") + "\n");

// Remove an element from the Set
hs.remove("pink");
System.out.println(hs + "\n");

// Get the size of the Set
System.out.println(hs.size() + "\n");

// Iterate over the elements of the Set, printing each one on a separate line
for(String item : hs){
System.out.println(item);
}
System.out.println("\n");

/*
* Warning!
Expand Down
33 changes: 28 additions & 5 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
import java.util.ArrayList;
// import java.util.Arrays;

public class StringPractice {
public static void main(String[] args) {
// Create a string with at least 5 characters and assign it to a variable

String word = "Connor";
System.out.println(word + "\n");
// Find the length of the string

System.out.println(word.length() + "\n");
// Concatenate (add) two strings together and reassign the result

String fName = "Connor";
String lName = "Hughes";
String fullName = fName + " " + lName;
System.out.println(fullName + "\n");
// Find the value of the character at index 3
System.out.println(word.charAt(3) + "\n");

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
System.out.println(word.contains("a") + "\n");

// Iterate over the characters of the string, printing each one on a separate line

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

ArrayList<String> ray = new ArrayList<>();
// Add multiple strings to the List (OK to do one-by-one)
ray.add("car");
ray.add("dog");
ray.add("cat");
ray.add("red");
ray.add("blue");
System.out.println(ray + "\n");

// 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
String concat = String.join(", ", ray);
System.out.println(concat + "\n");

// Check whether two strings are equal
String one = "racecar";
String two = "Truck";
System.out.println(one.equals(two));

/*
* Reminder!
Expand Down