diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..e379a63 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,22 +1,36 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[]myArray; + myArray = 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 + + myArray[0] = "Green River"; + myArray[1] = "Seattle Central"; + myArray[2] = "Highline"; + myArray[3] = "South Seattle"; // Get the value of the array at index 2 + System.out.println(myArray[2]); // Get the length of the array + System.out.println(myArray.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i =0; ilist = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + list.add("Ebtisam"); + list.add("Manar"); + list.add("Afnan"); + list.set(1,"Semah"); + list.add(0,"Abdulaziz"); // Print the element at index 1 - + System.out.println(list.get(1)); // Replace the element at index 1 with a new value + //I used the set method to replace the list with new value. + System.out.println(list); // (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) - + // I used add method to add the element. // Check whether the list contains a certain string - + System.out.println("Contains a certain string manar :" + list.contains("manar")); + // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for (int i =0; i people = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + people.put("Manar", 10); + people.put("Afnan", 6); + people.put("Semah", 4); + // Get the value associated with a given key in the Map + for(String i:people.keySet() ){ + System.out.println("key: " + i + " value: " + people.get(i)); + } // Find the size (number of key/value pairs) of the Map + System.out.println(people.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + String keyToReplace ="Afnan"; + Integer newValue = 5; + people.replace(keyToReplace,newValue); + System.out.println(people); // Check whether the Map contains a given key - + System.out.println("Is the given key Semah present?" + people.containsKey("Semah")); // Check whether the Map contains a given value + System.out.println("Is the given value 3 present?" + people.containsValue(3)); // Iterate over the keys of the Map, printing each key - + for(String name: people.keySet()){ + System.out.println("key:" + name); + } // Iterate over the values of the map, printing each value - + for(Integer age: people.values()){ + System.out.println("valu:" + age); + } // Iterate over the entries in the map, printing each key and value - - /* + for(String i: people.keySet()){ + Integer age = people.get(i); + System.out.println("key: " + i + " valu: " + age); + }/* * Usage tip! * * Maps are great when you want a specific key to value mapping. diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..59dbdb5 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,32 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float myFloat = -6.1f; + System.out.println(myFloat); // Create an int with a positive value and assign it to a variable - + int myInt = 250; + System.out.println(myInt); // Use the modulo % operator to find the remainder when the int is divided by 3 - + int a = 8; + int b = 3; + int x = a%b; + System.out.println(x); // 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 (a % 2 == 0){ + System.out.println(a + " Even"); + }else{ + System.out.println(a + " Odd"); + } + // Divide the number by another number using integer division - + int c = 18; + int d = 5; + int y =c/d ; + System.out.println(y); /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..beb490a 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,12 +5,15 @@ 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 - - - // Create a constructor that takes the name and age of the person - // and assigns it to the instance variables - + 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 + this.name = personname; + this.age= personage; // Create a toString method that gives the name and age of the person diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..a452c33 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,30 @@ +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 schools = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + schools.add("Green River"); + schools.add("Seattle Central"); + schools.add("lake youngs"); + System.out.println(schools); + // Check whether the Set contains a given String + System.out.println(schools.contains("Green River")); // Remove an element from the Set + System.out.println(schools.remove("Seattle Central")); // Get the size of the Set + System.out.println(schools.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String i : schools){ + System.out.println(i); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..528945f 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,20 +1,33 @@ +import java.util.ArrayList; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String message = "Struggle Coding"; + System.out.println(message); // Find the length of the string + System.out.println(message.length()); // Concatenate (add) two strings together and reassign the result - + String message1= "Happy"; + String message2= "Friday!"; + String message3 =message1 + message2; + System.out.println(message3); // Find the value of the character at index 3 + System.out.println(message3.indexOf("p")); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println(message3.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line - + x = message + message3; + System.out.println(x); + + // Create an ArrayList of Strings and assign it to a variable - + ArrayList colors = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + colors.add ("Red"); + colors.add ("Brown"); // 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