Java program not producing the proper output

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.

New contributor

Jrich096 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật