In the StringServer.java file, the method being called is: public String handleRequest(URI url)
The relevant arguments are:
- java.net.URI.getPath() - No arguments; Returns the decoded path component of this URI, or null if the path is undefined
- equals() - Takes an argument called anObject: The object to compare the String in getPath() against
- format() - Takes a string argument and outputs a formatted string
- handleRequest() - Takes any URI
- println() - Takes a string argument, prints it, then terminates the line
- contains() - Takes a sequence of char values to search for in a string, and outputs true/false
- java.net.URI.getQuery() - Returns the decoded query component of this URI.
- java.lang.String.split() - Takes a string argument, then splits this string around matches of the given regular expression.
- java.util.ArrayList.add() - Takes a String argument and appends the string to the end of the ArrayList
- java.util.ArrayList.get() - Takes an integer index and returns the element at the specified index of the ArrayList
- java.util.ArrayList.size() - No arguments; returns the number of valid elements in the ArrayList
The string "s" is the string that the user inputs into the URL to be printed on the server, while the string "s" helps to concatenate the individual strings together (with a newline inbetween each string).
As we can see from the image of the terminal (shown above), the ArrayList called stringArray got updated with the first string input provided by the user, which is "Hello!"
In the StringServer.java file, the method being called is: public String handleRequest(URI url)
The relevant arguments are:
- java.net.URI.getPath() - No arguments; Returns the decoded path component of this URI, or null if the path is undefined
- equals() - Takes an argument called anObject: The object to compare the String in getPath() against
- format() - Takes a string argument and outputs a formatted string
- handleRequest() - Takes any URI
- println() - Takes a string argument, prints it, then terminates the line
- contains() - Takes a sequence of char values to search for in a string, and outputs true/false
- java.net.URI.getQuery() - Returns the decoded query component of this URI.
- java.lang.String.split() - Takes a string argument, then splits this string around matches of the given regular expression.
- java.util.ArrayList.add() - Takes a String argument and appends the string to the end of the ArrayList
- java.util.ArrayList.get() - Takes an integer index and returns the element at the specified index of the ArrayList
- java.util.ArrayList.size() - No arguments; returns the number of valid elements in the ArrayList
The string "s" is the string that the user inputs into the URL to be printed on the server, while the string "s" helps to concatenate the individual strings together (with a newline inbetween each string).
As we can see from the image of the terminal (shown above), the ArrayList called stringArray got updated with a second string input provided by the user, which is "This is showing a single string getting added to by incoming requests." There are now two elements in the ArrayList, which increments in size whenever there is a new input message in the URL after /add-message?s=.
The original code, as provided in GitHub:
static int[] reversed(int[] arr) {
int[] newArray = new int[arr.length];
for(int i = 0; i < arr.length; i += 1) {
arr[i] = newArray[arr.length - i - 1];
}
return arr;
}
A failure-inducing input:
@Test
public void testReversed2() {
int[]input2 = {1, 2, 3};
assertArrayEquals(new int[]{3, 2, 1}, ArrayExamples.reversed(input2));
}
A non failure-inducing input:
@Test
public void testReversed() {
int[] input1 = { };
assertArrayEquals(new int[]{ }, ArrayExamples.reversed(input1));
}
Bug FIXED:
static int[] reversed(int[] arr) {
int[] newArray = new int[arr.length];
for(int i = 0; i < arr.length; i++) {
newArray[arr.length-i-1] = arr[i];
}
return newArray;
}
The reason why there was a bug that resulted in the symptom shown above was that we were updating the old array, with reversed elements rather than the new one and were also returning the old array.
These changes results in a passed test for the same non failure-inducing input as aforementioned:
In lab 2, I learned how to clone repositories using Github desktop and how to implement a program on Visual Studio Code that takes a URL string as an input and responds with the text of a web page. I then learned how to build and run the server on my local computer.
In lab 3, I learned how to use tester java files to help me debug methods and how to write a web server that takes incoming requests in a URL and outputs a concatenated string, separated by new lines.