Name:
PID: (page 2)
import java.io.IOException;
import java.net.URI;
// This interface is defined in another file (we provide it in lab 2)
// interface URLHandler { String handleRequest(URI url); }
// URI is a built-in Java class with methods like getPath() and getQuery()
class Counter implements URLHandler {
int num = 0;
public String handleRequest(URI url) {
System.out.println(url);
if (url.getPath().equals("/")) { return String.format("Number: %d", num); }
// FILL in the block below to match the behavior in the browser below!
else if (url.getPath().equals("/count")) {
} else {
return "Don't know what to do with that path!";
}
}
}
class CounterMain {
public static void main(String[] args) throws IOException {
int port = Integer.parseInt(args[0]);
// We wrote Server; it is a very short class (you can see it in Server.java in lab 2)
Server.start(port, new Counter());
}
}
local $ javac Server.java Counter.java
local $ java CounterMain
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at CounterMain.main(Counter.java:24)
local $ java CounterMain 4000
Server Started! Visit http://localhost:4000 to visit.
Counter.java
What do you notice and wonder about this program?
Assume the WhereAmI.java file we've been using as an example is stored in the root folder of a GitHub repository at https://github.com/ucsd-cse15l-f23/WhereAmI
What commands should we use to compile and run the WhereAmI.java file at this point?
local $ ssh cs15lfa23zz@ieng6.ucsd.edu
remote $ git clone https://github.com/ucsd-cse15l-f23/WhereAmI
remote $
Name:
PID: (page 1)
Symptom: The behavior you see (terminal output, error messages, web page contents)
Bug: A flaw in a program that causes symptoms
Failure-inducing input: Data, values, or other input(s) that demonstrate a bug's symptom(s)
Definitions from John Regehr https://blog.regehr.org/archives/199
class MainExample1 {
public static void main(String[] args) {
int current = 0;
while(current < args.length) {
System.out.println(args[current]);
}
}
}
class MainExample2 {
public static void main(String[] args) {
int length = args.length;
for(int i = 0; i < length; length += 1) {
System.out.println(args[i]);
}
}
}
$ javac Examples.java
$ java MainExample1 apple banana cranberry
$ java MainExample2 apple banana cranberry
class EvensExample {
static int sumEvenIndices(int[] nums) {
int sum = 0;
for(int i = 0; i < nums.length; i += 2) {
sum += nums[i + 1];
}
return sum;
}
}
What are two different failure-inducing inputs for sumEvenIndices that demonstrate different symptoms?
How many bugs are there?
class NumsExample {
static double reciprocal(int n) {
return 1 / n;
}
static double ratio(int n, int d) {
return n / d;
}
static double formula(int a, int b, int c) {
return ratio(a, b) * reciprocal(c);
}
}
What are two different failure-inducing inputs for formula that demonstrate the same symptom?
How many bugs are there?