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
32 changes: 25 additions & 7 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
//Shawn Nguru
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
System.out.println("***************************************************");
// Create an array of Strings of size 4
String[] names = {"Kaladin", "Sam", "David", "Paul"};

// Get the value of the array at index 2
// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
names[0] = "Kal";
names[1] = "Sammy";
names[2] = "Dave";
names[3] = "Saul";

// Get the length of the array

// Iterate over the array using a traditional for loop and print out each item
// Get the value of the array at index 2
System.out.println(names[2]);

// Iterate over the array using a for-each loop and print out each item
// Get the length of the array
int length = names.length;

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

// Iterate over the array using a for-each loop and print out each item
for(String tag : names)
{
System.out.println(tag);
}
/*
* Reminder!
*
Expand Down
63 changes: 44 additions & 19 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
//Shawn Nguru
public class ListPractice {


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

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

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

// Check whether the list contains a certain string
if(list.contains("three"))
{
System.out.println("It contains that value!");
}

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

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

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

// Print the element at index 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)

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

// Check whether the list contains a certain string

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

// Sort the list using the Collections library

// Iterate over the list using a for-each loop
// Print each value on a second line

/*
* Usage tip!
Expand Down
59 changes: 43 additions & 16 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@


//Shawn Nguru
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

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

// Get the value associated with a given key in the Map
// Create a HashMap with String keys and Integer values and
// assign it to a variable of type Map
Map<String,Integer> maps = new HashMap<>();

// Find the size (number of key/value pairs) of the Map
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
maps.put("Kal",1);
maps.put("Dalinar",2);
maps.put("Shallan",3);

// Replace the value associated with a given key (the size of the Map shoukld not change)
// Get the value associated with a given key in the Map
System.out.println(maps.get("Dalinar"));

// Check whether the Map contains a given key
// Find the size (number of key/value pairs) of the Map
System.out.println(maps.size());

// Check whether the Map contains a given value
// Replace the value associated with a given key (the size of the Map should not change)
maps.put("Kal",100);
System.out.println(maps);

// Iterate over the keys of the Map, printing each key
// Check whether the Map contains a given key
if(maps.containsKey("Dalinar"))
{
System.out.println("The Goat");
}
// Check whether the Map contains a given value
if(maps.containsValue(1))
{
System.out.println("Kal The Goat");
}
else System.out.println("N/A");

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

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

/*
* Usage tip!
Expand Down
33 changes: 25 additions & 8 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
//Shawn Nguru
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
// Create a float with a negative value and assign it to a variable
float negativeVal = -12;

// Create an int with a positive value and assign it to a variable
// Create an int with a positive value and assign it to a variable
int positiveVal = 3;

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

// Divide the number by another number using integer division
// Divide the number by another number using integer division
float solution = positiveVal/negativeVal;
/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* 7 / 3 = 2 when performing int division
*/

/*
* Reminder!
Expand Down
122 changes: 87 additions & 35 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,105 @@
//Shawn Nguru
/*
* In this file you will follow the comments' instructions to complete
* the Person class.
*/

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

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


// Create a toString method that gives the name and age of the person
// 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.
/**
* birthYear returns the year the person was born.
*
* The birth year is calculated by subtracting the person's age from currentYear
* that's passed in as an int. It assumes that the person's birthday has already
* passed this year.
*
* @param currentYear an int for the current year
* @return The year the person was born
*/
// (create the instance method here)
// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
/**
* birthYear returns the year the person was born.
*
* The birth year is calculated by subtracting the person's age from currentYear
* that's passed in as an int. It assumes that the person's birthday has already
* passed this year.
*
* @param currentYear an int for the current year
* @return The year the person was born
*/

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

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

// Create another instance of Person with a different name and age and
// assign it to a different variable

// Print the first person

// Print the second person

// Get the name of the first person and store it in a local variable

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

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

Person dude = new Person("Shawn",22);
Person girl = new Person("Alex",23);

// Create another instance of Person with a different name and age and
// assign it to a different variable

// Print the first person
System.out.println(dude);
// Print the second person
System.out.println(girl);

// Get the name of the first person and store it in a local variable
String name = dude.name;
// 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.

// In a separate statement, print the local variable holding the birth year.
int age = dude.birthYear(2025);
System.out.println(age);
System.out.println("***************************************************");
/**
* Person
*/

// Create a float with a negative value and assign it to a variable
float negativeVal = -12;

// Create an int with a positive value and assign it to a variable
int positiveVal = 3;

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

// Divide the number by another number using integer division
float solution = positiveVal/negativeVal;
/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* 7 / 3 = 2 when performing int division
*/
/**
* Terminology!
*
Expand Down
Loading