-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finished Practice PR #8
base: main
Are you sure you want to change the base?
Changes from all commits
a2584cd
d86b64b
9ab5a85
88c4bdc
2d86dd0
9d0d7ca
133d7ca
ef5f8d1
31e52e2
c0faab1
c4a40b5
5d2d659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
public class ArrayPractice { | ||
public static void main(String[] args) { | ||
// Create an array of Strings of size 4 | ||
String[] 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] = "A"; | ||
myArray[1] = "B"; | ||
myArray[2] = "C"; | ||
myArray[3] = "D"; | ||
|
||
// 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; i > myArray.length; i++){ | ||
System.out.println(myArray[i]); | ||
} | ||
|
||
// Iterate over the array using a for-each loop and print out each item | ||
for(String letter : myArray){ | ||
System.out.println(letter); | ||
} | ||
|
||
/* | ||
* Reminder! | ||
* | ||
* Arrays start at index 0 | ||
*/ | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
|
||
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> mapsPractice = new HashMap<>(); | ||
// Put 3 different key/value pairs in the Map | ||
// (it's OK to do this one-by-one) | ||
|
||
mapsPractice.put("hello", 0); | ||
mapsPractice.put("neutral", 1); | ||
mapsPractice.put("goodbye", 2); | ||
// Get the value associated with a given key in the Map | ||
|
||
mapsPractice.get("hello"); | ||
// Find the size (number of key/value pairs) of the Map | ||
|
||
mapsPractice.size(); | ||
// Replace the value associated with a given key (the size of the Map shoukld not change) | ||
|
||
mapsPractice.replace("hello", 3); | ||
// Check whether the Map contains a given key | ||
|
||
mapsPractice.containsKey("goodbye"); | ||
// Check whether the Map contains a given value | ||
|
||
mapsPractice.containsValue(5); | ||
// Iterate over the keys of the Map, printing each key | ||
|
||
for(String entry : mapsPractice.keySet()){ | ||
System.out.println(entry); | ||
} | ||
// Iterate over the values of the map, printing each value | ||
|
||
for(int entry : mapsPractice.values()){ | ||
System.out.println(entry); | ||
} | ||
// Iterate over the entries in the map, printing each key and value | ||
|
||
for(String entry : mapsPractice.keySet()){ | ||
System.out.println("Key: " + entry + ". Value: " + mapsPractice.get(entry)); | ||
} | ||
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works! In the future, also consider |
||
/* | ||
* Usage tip! | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,25 @@ | |
* the Person class. | ||
*/ | ||
|
||
import java.util.*; | ||
|
||
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 = -99; | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these it's probably best not to set the defaults. They will automatically default to null and 0. |
||
|
||
// Create a constructor that takes the name and age of the person | ||
// and assigns it to the instance variables | ||
|
||
public Person(String x, int y){ | ||
name = x; | ||
age = y; | ||
} | ||
Comment on lines
+16
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, but try to use more descriptive variable names in the future. A caller of this class will be confused as to what x and y represent. |
||
|
||
// Create a toString method that gives the name and age of the person | ||
|
||
public String toString(){ | ||
return("Name: " + name + ". Age: " + age + "."); | ||
} | ||
|
||
// Implement the below public instance method "birthYear" | ||
// There should NOT be any print statement in this method. | ||
|
@@ -28,26 +36,32 @@ public class Person { | |
* @return The year the person was born | ||
*/ | ||
// (create the instance method here) | ||
|
||
public int birthYear(int currentYear){ | ||
return(currentYear - age); | ||
} | ||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and elsewhere we don't need the parentheses after return. You can just say |
||
// Created a method to get name easier. | ||
public String getName(){ | ||
return(name); | ||
} | ||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need getters for a public instance variable. |
||
|
||
public static void main(String[] args) { | ||
// Create an instance of Person | ||
|
||
Person jim = new Person("Jim", 45); | ||
// Create another instance of Person with a different name and age and | ||
// assign it to a different variable | ||
|
||
Person tim = new Person("Tim", 25); | ||
// Print the first person | ||
|
||
System.out.println(jim); | ||
// Print the second person | ||
|
||
System.out.println(tim); | ||
// Get the name of the first person and store it in a local variable | ||
|
||
String grabbedName = jim.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 jimBirthYear = jim.birthYear(2025); | ||
// In a separate statement, print the local variable holding the birth year. | ||
|
||
System.out.println(jimBirthYear); | ||
/** | ||
* Terminology! | ||
* | ||
|
There was a problem hiding this comment.
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)