Yangyang Liu
CSE 15L Section B02
PID: A17360266
Here is an implementation for a web server called StringServer
.
It keeps track of a single string that get added to by incoming requests.
Note: It makes use of the Server.java
file found here.
// In StringServer.java file
import java.io.IOException;
import java.net.URI;
class StringHandler implements URLHandler {
String str = "";
public String handleRequest(URI url) {
if (url.getPath().equals("/")) {
if (str.equals("")) {
return "Start adding words";
}
return str;
}
else if (url.getPath().contains("/add-message")) {
String[] parameters = url.getQuery().split("=");
if (parameters[0].equals("s")) {
str += (parameters[1] + "\n");
return str;
}
}
return "404 Not Found!";
}
}
class StringServer {
public static void main(String[] args) throws IOException {
if(args.length == 0){
System.out.println("Missing port number! Try any number between 1024 to 49151");
return;
}
int port = Integer.parseInt(args[0]);
Server.start(port, new StringHandler());
}
}
/add-message?s=firstWord
Which methods in your code are called? What are the relevant arguments to those methods, and the values of any relevant fields of the class?
Note: Some method headers were found from Java documentation
handleRequest(URL url)
method of the StringHandler
class
url
with value http://localhost:5454/add-message?s=firstWord
str
with initial value ""
getPath()
method of the URI
class
https://localhost:5454/add-message?s=firstWord
in handleRequest
method/add-message
equals(Object obj)
handleRequest
method to compare path component of the URI with the string "/"
obj
with value "\"
false
contains(CharSequence s)
method
handleRequest
method to check if path component of URI contains the string "/add-message"
s
with value "/add-message"
true
getQuery()
method of the URI
class
https://localhost:5454/add-message?s=firstWord
in handleRequest
method"s=firstWord"
split(String regex)
method
"s=firstWord"
in handleRequest
methodregex
with value "="
{"s", "firstWord"}
How do the values of any relevant fields of the class change from this specific request? If no values got changed, explain why.
StringHandler
class:
""
"firstWord\n"
/add-message?s=secondWord
Which methods in your code are called? What are the relevant arguments to those methods, and the values of any relevant fields of the class?
The called methods are the same as in Screenshot #1.
handleRequest(URL url)
method of the StringHandler
class
url
with value http://localhost:5454/add-message?s=secondWord
str
with initial value "firstWord\n"
getPath()
method of the URI
class
https://localhost:5454/add-message?s=secondWord
in handleRequest
method/add-message
equals(Object obj)
handleRequest
method to compare path component of the URI with the string "/"
obj
with value "\"
false
contains(CharSequence s)
method
handleRequest
method to check if path component of URI contains the string "/add-message"
s
with value "/add-message"
true
getQuery()
method of the URI
class
https://localhost:5454/add-message?s=secondWord
in handleRequest
method"s=secondWord"
split(String regex)
method
"s=secondWord"
in handleRequest
methodregex
with value "="
{"s", "secondWord"}
How do the values of any relevant fields of the class change from this specific request? If no values got changed, explain why.
StringHandler
class:
"firstWord\n"
"firstWord\nsecondWord\n"
reverseInPlace
in ArrayExamples.java
@Test
public void testReverseInPlace() {
int[] input = {1, 2, 3, 4, 5};
ArrayExamples.reverseInPlace(input);
assertArrayEquals(new int[]{5, 4, 3, 2, 1}, input);
}
@Test public void testReverseInPlace() {
int[] input = {0};
ArrayExamples.reverseInPlace(input);
assertArrayEquals(new int[]{0}, input);
}
Bug
BEFORE code change:
static void reverseInPlace(int[] arr) {
for(int i = 0; i < arr.length; i += 1) { // bug in for-loop
arr[i] = arr[arr.length - i - 1]; // bug in method body
}
}
There is a bug in this implementation for the reverseInPlace
method. The method iterates through every element in the int
array,
replacing it with its compliment. However, this implementation causes every element to be switched twice, return an array with the same
items in the same order as the original array.
AFTER code change:
static void reverseInPlace(int[] arr) {
for(int i = 0; i < arr.length/2; i += 1) { // change to arr.length
int temp = arr[i]; // added line
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp; // added line
}
}
Here, the method iterates only through the first half of the array and involves a new temp
variable that stores the value of the original
indexed value. As a result, every element is switched once and a reversed array is returned.
I learned a lot about servers during the Week 2 Lab. I didn’t know that a (simple) server was so easy to start. It only requires two short Java files for you start your own server. From that lab, I also learned how the “?” query could be used in websites. All the things we learned about servers and URLs relates back to the first couple weeks where we learned about paths.
☺︎