Your lab on memory references is due by 11:59pm on Wednesday, January 24. You will submit Nullity.java electronically and you will submit your answers from the references lab (part 2) in class on Thursday.
Here is a video version of this lesson: https://www.youtube.com/watch?v=RdOzgnyZZus
1. Write a brief class named Nullity.java. ALL METHODS IN THIS CLASS WILL BE static. The point of this class is to be familiar with null objects and pointers. Remember to check for null and other base cases FIRST!
You should test these methods from main to make sure they all work properly.
//this static method should input a string. If that String is null, it prints out a warning message and returns Integer.MIN_VALUE. Otherwise, it returns the length of the string. HINT: it is a good habit to always check if something is null at the beginning of your method before calling other methods to avoid runtime errors!
a. public static int nLength(String str)
//this static method should input 2 strings and return a String. If either of the inputs is null, this method should return null. Otherwise, it will return whichever String is shortest. If the length of the two Strings is identical, you may return either one.
b. shortest(String s1, String s2)
//this static method should input two Strings and return a boolean. This will compare the two Strings using == and using .equals() and return true if EITHER ONE (or both) is true. So if s1 == s2 OR s1.equals(s2) it should return true. You need to make sure that your program does NOT have a runtime error in the likely case that s1 and/or s2 are null (so make sure you never call a method on a null object) - remember that if both are null, == will return true.
c. eitherEquals(String s1, String s2)
//this void method should input an array of Strings, a new String to put in that array, and a location. The String in that location should be set to the newString input. For example, if I have an array words with ["hello","there","world"] and I call arraySet(words,"Lebron",2), words should then represent the array ["hello","there","Lebron"]. Make sure that if words is null or spot is out of bounds, your program prints a mean message instead of causing a runtime error.
d. arraySet(String[] words, String newString, int spot)
//this void method should input an array of ints, figure out which integer is the largest, and replace ALL copies of that value with the number 0. So if I send this method the array [6,8,12,4,-4,12,12,0], it should change the array to [6,8,0,4,-4,0,0,0]
e. replaceAllMax(int[] numbers)
//this void method should input an array of Strings and shift all of them one unit to the right. The String at the end of the list should be put in spot 0. So if you send it an array containing ["hello", "there", "billy", "joel"] it should be changed to ["joel","hello", "there", "billy"]. Make sure your solution actually changes the original array rather than just creating a new one. Be careful with your loop - depending on how you set up your method, you may need to go through the loop backwards.
f. shiftDownOne(String[] phrases)
//this void method should input an ArrayList of String objects and remove all Strings in that list which contain either a "k" or a "K" anywhere in them. For example, if I send this method the list ["Kangaroo", "donkey kong", "mario", "diddy kong"], after calling the method, the list should just contain ["mario"].
g. removeKs(ArrayList<String> words)
Make sure every method in this class is tested thoroughly. There are some tricky ones here! It is probably easiest to do this in a main method - create your variables, call the method, and then print out whatever the method returned or the arrays/arraylists after calling that method.
2. Please answer the following questions (parts a. through i.). Your answers may be typed or handwritten and may include drawings to help you see what is going on. You will show me this in class.
These questions refer file Person.java, which can be seen at the following link after you click on "person file": https://drive.google.com/drive/folders/1VuTI2RXdQ3o-Ri8YX4W04Q0ciuo4GIXo?usp=sharing
Consider the following code written in a separate program with questions intermingled in the code (please feel free to type up the code yourself if you are confused as to what is going on). Treat this as one program, with each part following the others and not being commented out or anything:
public static void main (String[] args)
{
Person person1 = new Person ("Rachel", 6);
Person person2 = new Person ("Elly", 4);
Person person3 = new Person ("Sarah", 19);
System.out.println (person1 + ", " + person2 + ", " + person3);
// Reassign people
person1 = person2;
person2 = person3;
person3 = person1;
System.out.println (person1 + ", " + person2 + ", " + person3);
a. What are the results of these two previous println statements? Think carefully about where each object is pointing. Draw a picture!
person2.changeName ("Bozo");
System.out.println (person1 + ", " + person2 + ", " + person3);
person3.changeName ("Clarabelle");
System.out.println (person1 + ", " + person2 + ", " + person3);
person1.changeName("Harpo");
System.out.println (person1 + ", " + person2 + ", " + person3);
b. What are the results of these three previous println statements?
c. After all the previous code, which of the following statements are true?
i. person2 == person1
ii. person1 == person3
iii. person2 == person3
d. Suppose the programmer had meant to do a circular shift in reassigning the three people -- that is he/she wanted to have the original person2 object become person1, the original person3 become person2 and the original person1 become person3. Write the assignment statements necessary to make this happen. [if you are stuck on this, it may be useful to look back at how you swapped two elements in an array]
}
Now consider the following code and questions:
public static void main (String[] args)
{
System.out.println ("Enter three ages...");
int age1 = 12;
int age2 = 15;
int age3 = 18;
// Instantiate three Person objects with the ages read in
Person person1 = new Person ("Rachel", age1);
Person person2 = new Person ("Elly", age2);
Person person3 = new Person ("Sarah", age3);
System.out.println ("The original three people...");
System.out.println (person1 + ", " + person2 + ", " + person3);
// Reassign ages in the int variables
age1 = age2;
age3 = age2;
age2 = 20;
person1 = person2;
person3 = person2;
System.out.println ("Ages (ints): " + age1 + ", " + age2 + ", " + age3);
System.out.println (person1 + ", " + person2 + ", " + person3);
e. What would be printed out from these previous two printlns? Why?
// Make some changes to the integer values and corresponding objects
System.out.println ("\nChanging the second age to 99...");
age2 = 99;
person2.changeAge(age2);
System.out.println ("Ages (ints): " + age1 + ", " + age2 + ", " + age3);
System.out.println (person1 + ", " + person2 + ", " + person3);
f. What is printed out from these previous two printlns? Why?
age1 = 100;
person1.changeAge(age1);
System.out.println ("Ages (ints): " + age1 + ", " + age2 + ", " + age3);
System.out.println (person1 + ", " + person2 + ", " + person3);
g. What is printed out from these previous two printlns?
}
h. Why do the ages of person1, person2, and person3 all remain equivalent to each other after all the different assignments but the three integers (age1,age2,age3) do not?
Now, suppose I have the following code in another class somewhere:
public static void main()
{
String x = new String("hello friend");
String y = x;
y = y + "s";
Person a = new Person("Jacqueline", 19);
Person b = a;
a = new Person("Ricardo", 26);
a.changeAge(400);
System.out.println(x);
System.out.println(b);
}
i. What will be printed out by the two printlns? Why?
When you are finished, submit Nullity.java to your named google drive folder. Part 2 (the answers to questions a through i) will be checked in class. If you finish early, feel free to work on the review or help your classmates.