Java – How to solve these issues with the program? (Many issues explained inside)

java

This question is not for the faint of heart and will be time consuming..

I am learning Java, and I was wondering if anyone could walk me through my program to tell me what I did wrong and how I can fix it so that I can learn.

My whole program (in case this is needed): http://pastie.org/private/xpbgpypetvfmivcs88gbcq

Assignment:

Not allowed to use global variables!
You have been asked to write a program
to grade the results of a true-false
quiz and display the results in
tabular form. The quiz consists of 10
questions. The data file for this
problem consists of 1 set of correct
responses (answerkey) on line one and
succeeding lines containing a four
digit student identification number
followed by that student's 10
responses. (The number of students
taking the quiz in currently unknown.)

The data file is in the form :

TFFTFTTFTT

0461 TTFTTFTFTT

3218 TFFTTTTTFT

…………………

………………..

Your program should read the key and
store it in a one dimensional array.
It should then read and process each
succeeding line, storing the student
identification numbers in a one
dimensional array and the grades in
two separate one dimensional arrays-
one for numeric grades and the for
letter grades.

Numeric grades are converted to letter
according to the following system :
A(10), B(9), C(8-7), D (6-5), F(4-0).

Program output should include the
following :

  1. Each Student's four digit identification number, their numeric
    grade and their letter grade.

  2. A printed count of how many students took the quiz.

  3. The numeric quiz average for the entire class.

  4. A frequency count of the number of A's, B's, C's,D's and F's for the
    entire class.

EDIT:

Oh, hey. Sorry, I am still getting the hang of how to do all of this 🙁 Alright, here goes.

The program is not compiling, and I am pretty sure it's down to my logic. (As well as some syntactical stuff).

What I'm having trouble with:
1) How to pass all of the parameters to each of the different methods, as seen here. What I would LIKE to do is have all of my values that I return such as answerKey, studentAnswers, numericGrade, average etc be up here so that I can use them and print them. But I do not know how to do that.

int[] numericGrade = computeNumericGrade(answerKey, studentAnswers);
      int[] letterGrade = computeLetterGrade(numericGrade);
      double average = average(numericGrade);
      int gradeTally = gradeTally(letterGrade);
      int studentCount = studentCount(numericGrade);

2) I cannot figure out how to individually check through each of the T or F values and check if it corresponds with the T or F in the answer key, as seen below.

What I am trying to do is to check through each letter at a specific element, and if the value is "correct" or == to answerKey, then add to the total in numericGrade for that element.

public static int[] computeNumericGrade(int[] answerKey, int[] studentAnswers) {
      int[] numericGrade = new int[50];
      int total = 0;
      for(int i = 0; i < 50;i++) {
        for(int a = 0;a == 10;a++) {
          total = studentAnswers[i].charAt(a);
          if(total == answerKey[i]) numericGrade[i]++;
        }
      }
    return numericGrade;
    }

3) I do not know how to return all of these different values, as I need them all above! I want this set of code to check the letter grade of each element and if this element is found, ++ the variable that corresponds to it, so I know how many of this letter grade was called.

public static int gradeTally(String[] letterGrade) {
      int a, b, c, d, f = 0;
      for(int i = 0;i < letterGrade.length;i++) {
        if(letterGrade[i] == 'A') a++;
        else if (letterGrade[i] == 'B') b++;
        else if (letterGrade[i] == 'C') c++;
        else if (letterGrade[i] == 'D') d++;
        else if (letterGrade[i] == 'F') f++;
      }
      return a + b + c + d + f;
    }

4) I can't figure this out… I think I am close. Here, I am trying to read the file, tokenize it, and then for each token, add it to the array 1 by 1. However, I don't think that's it's doing that properly, but I can't check since I can't get it to compile…

public static String[] getData() throws IOException {
      int[] studentID = new int[50];
      String[] studentAnswers = new String[50];
      int total = 0;

      String line = reader.readLine();
      strTkn = new StringTokenizer(line);
      String answerKey = strTkn.nextToken();

      while(line != null) {
        studentID[total] = Integer.parseInt(strTkn.nextToken());
        studentAnswers[total] = strTkn.nextToken();
        total++;
      }
    return studentAnswers;
    }

I am now done editing my post. I hope this is sufficient 🙂

Best Answer

I guess you realize from the comments that this is kind of difficult to help with.

I'm not sure how much it will help, but I'd like to give some general tips about how you might approach this.

First of all, I'd probably throw away what you have and start over. I've NEVER regretted throwing away code--it always helps your code in the long run and you learn a lot more the second time through.

Secondly, start small. Make something that works--even if it just reads in the values and proves they were read in properly, then expand it. Test CONSTANTLY. Whenever you add to your code, add a line to your test to ensure the new code works as well as everything that has come before it

This way if anything breaks, you'll know exactly what broke it--the last thing you edited.

To do this kind of quick test, you should either learn to use JUnit for unit testing or have a main made just for testing so that you can hit a single button and get an immediate answer. (JUnit really isn't hard and it's built into Eclipse. Oh yeah, next hint). .

USE ECLIPSE or NetBeans!

These are all things that experienced programmers just know and do automatically. A little investment of time and effort in the beginning can save a LOT of pain in the long run.

Related Topic