I’m currently working on an assignment where I have to fix java code compilation errors in an IDE (VSCode) and then push the changes to a GitHub repository.
The program should start running and then crash (We are going to work on debugging the reason for that crash in a later assignment), but I cannot find out why the program isn’t working.
Here is what the classes look like:
package cs252;
import java.util.Arrays;
/**
* Class to compute median averages.
*
* The median average of a sequence of numbers is the "middle" value
* if you have an odd number of values or the midpoint between the two middle
* values if you have an even number of values.
*/
public class Median {
double[] values;
public Median(double[] values) {
this.values = values;
}
/**
* Compute the median average of the supplied values.
* @return the median average
*/
double median() {
double[] sorted = new double[values.length];
for (int i = 0; i < values.length; ++i) {
sorted[i] = values[i];
}
Arrays.sort(sorted);
int middle = values.length / 2;
if (values.length % 2 == 0) {
return 0.5 * (sorted[middle - 1] + sorted[middle]);
} else {
return sorted[middle];
}
}
}
package cs252;
import java.util.Arrays;
public class TestMedian {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Supply the number of tests to run on the command line.");
System.exit(1);
}
int n = Integer.parseInt(args[0]);
boolean result = test(n);
if (result) {
System.out.println("Passed all " + n + " tests.");
} else {
System.out.println("Failed one or more tests.");
}
}
private static boolean test(int numTests) {
double[] inputs = { 1.0, 4.0, 6.5, 2.4, 24.0, 5.0 };
double[] expected = { 1.0, 2.5, 4.0, 3.7, 5.25, 4.75 }; // Corrected expected median averages
for (int i = 0; i < numTests; ++i) {
double[] trimmedInputs = Arrays.copyOf(inputs, i + 1);
Median med = new Median(trimmedInputs);
double avg = med.median();
if (avg != expected[i])
return false;
}
return true;
}
}
We were also supposed to create a Run Configuration in your IDE to execute the program, supplying the number 5 as a command-line argument after fixing the errors and this is what I did for mine
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "TestMedian",
"request": "launch",
"mainClass": "cs252.TestMedian",
"args": ["5"], // Specify the command-line argument here
"projectName": "idesAsst_469d298e"
}
]
}
I was expecting to see something along the lines of “Passed all ” + n + ” tests.” in the output followed by the program crashing but this is the output I got when running the program instead:
cd /home/cs_jrich096/UnixCourse/idesAsst ; /usr/bin/env /usr/lib/jvm/java-17-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:41297 -XX:+ShowCodeDetailsInExceptionMessages -cp /home/cs_jrich096/.vscode-server/data/User/workspaceStorage/dbb270263255ab5da80e24ff3634cd86/redhat.java/jdt_ws/idesAsst_469d298e/bin cs252.TestMedian 5
Failed one or more tests.
I tried to run the command to finish the assignment after pushing my results to git and this is the output I got:
Which IDE did you use to complete this assignment?
1. VSCode
2. Eclipse
1
Checking VSCode...
javac -g -d bin -cp src:bin src/cs252/TestMedian.java
jar cfe median.jar cs252.TestMedian -C bin/ .
Something is wrong with the generated executable at (eval 1) line 129.
Jrich096 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2