1

This is assessed work so please don't give a straight answer.

My program is supposed to calculate the users grade: "pass" , "fail" or "pass with compensation". However, it doesn't return the answer. I'm unable to figure out why - can anyone help?

public class MarkCalculator { static int[] marks = new int[12];

//public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    int weighting;
    int coursework;
    int exammark;

    System.out.println("Please enter course work weighting");
    marks[0]= kb.nextInt();

    System.out.println("Please enter course work mark");
    marks[1]= kb.nextInt();

    System.out.println("Please enter exam mark");
    marks[2]= kb.nextInt();


    MarkCalculator mc = new MarkCalculator();
    mc.computeMarks(marks);

}




public String[] computeMarks(int[] marks) {

    final int[] WEIGHTING = {55,66,55,44,33,44};
    String[] results = new String[WEIGHTING.length];
    for (int i = 0; i < marks.length / 2; i++) {

        int exam = marks[i];
        int cw = marks[i];
        int weight = WEIGHTING[i];
        int formula = ((cw + weight) + (exam * (100 - weight)) / 100);

        if (formula >= 40){
            results[i]="PASS";
        } else if ((formula < 39) && (formula > 35)){
            results[i]="COMPENSATION PASS";
        }else{
            results[i]="FAIL";

        }

    }

    return results;
}

}

1
  • Just a side note, when formula==39 result will be FAIL, I don't think this is what you are expecting Commented Nov 6, 2013 at 16:38

3 Answers 3

3
final int[] WEIGHTING = {};
String[] results = new String[WEIGHTING.length];

Here's a problem. WEIGHTING has no initial size.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, you can declare variable as final in method.
I had numbers initialized in weighting but took them out just playing around with it to hopefully find a solution
I had six random numbers, but i shouldn't have to initialize numbers because say if someones weighting was outside the 6 numbers?
@user2961364, then you would need a an ArrayList or a some kind of List. Arrays don't auto-increment.
keep in mind also, you haven't given an values to marks in your code above.
|
1

Also: you don't initialize marks with anything. Try this:

System.out.println("Please enter course work weighting");
        marks[0]= kb.nextInt();

        System.out.println("Please enter course work mark");
        marks[1]= kb.nextInt();

        System.out.println("Please enter exam mark");
        marks[2]= kb.nextInt();

        MarkCalculator mc = new MarkCalculator();
        mc.computeMarks(marks);

6 Comments

hey by mark[0], mark[1] and mark[2]. Do you mean marks[0],marks[1] and marks[2] because mark isn't defined?
so i've changed ---- final int[] WEIGHTING = {55,66,55,44,33,44}; ---so WEIGHTING now has some numbers as the weighting of the cw.
but for the markCalculator bit i don't really understand, it doesn't allow me to put numbers inside it to pass marks.
but when i input mark amounts and weighting the program still terminates without any calculation which it should complete
do you print your results somewhere like System.out.println(mc.computeMarks(marks).toString());?
|
1

The problem is WEIGHTING is empty

final int[] WEIGHTING = {}; // empty
String[] results = new String[WEIGHTING.length];
for (int i = 0; i < marks.length / 2; i++) {

    int exam = marks[i];
    int cw = marks[i];
    int weight = WEIGHTING[i]; // You cant access elements from an empty array

Also

    MarkCalculator mc = new MarkCalculator();
    mc.computeMarks(marks);

here you are passing marks which is empty.

EDIT

Reason why your program wasnt working is because you are not catching the result from computeMarks. You should store it in an array inside main like

String[] result = mc.computeMarks(marks);

for(int k=0;k<result.length;k++)
{
    System.out.println(result[k]);
}

2 Comments

I've set weighting as final int[] WEIGHTING = {55,66,55,44,33,44};
and marks[0]= kb.nextInt(); marks[1]= kb.nextInt(); marks[2]= kb.nextInt(); so marks isn't empty anymore? still not receiving answer informing me whether it's pass ect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.