How can I solve these problems using my program? (Many problems are explained internally)

This question is not for the faint of heart and will take a lot of time.

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

My whole program (if necessary): http://pastie.org/private/xpbgpypetvfmivcs88gbcq

Appointment:

You cannot use global variables! You were asked to write a program to evaluate the results of a true false quiz and display the results in a tabular form. The test consists of 10 questions. The data file for this problem consists of 1 set of correct answers (answer) in line 1 and subsequent lines containing the four-digit student identification number and then this student has 10 answers. (The number of students taking the quiz is currently unknown.)

The data file is in the form:

TFFTFTTFTT

0461 TTFTTFTFTT

3218 TFFTTTTTFT

.....................

....................

. , - , .

: A (10), B (9), C (8-7), D (6-5), F (4-0).

:

  • , .

  • .

  • .

  • A, B, C, D F .

EDIT:

, . , , :( , .

, . ( ).

: 1) , . , , , answerKey, studentAnswers, numericGrade, .., , . , .

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

2) , T F , T F , .

, , - , "" == , .

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) , , ! , , , ++ , , .

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) ... , . , tokenize , , 1 1. , , , ' ...

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;
    }

. , :)

+3
4

, , .

, , , .

, , , , , . , - , .

-, . -, , , , . . , , , , ,

, - , , - , .

, JUnit , , . (JUnit , Eclipse. , )..

ECLIPSE NetBeans!

, . .

+4

, , .

, , , "". , , , . Anyhoo...

:

someField cannot be resolved

, , "" . - , , , .

main(). . , answerKey studentAnswers, . - , , . answerKey, studentAnswers , :

public static String[] getData() throws IOException {
  // other code

  String[] studentAnswers = new String[50];

  // other code 

  String answerKey = strTkn.nextToken();

  // other code
}

, , studentAnswers answerKey " getData()". , main().

- - , public static String answerKey; public class GradeResults {, , , ( , ).

- "" . , , , , . , ( , ). , , , Student.

 // this is a new class, and will have to be saved in a file called Student.java
public class Student {

    // these are field declarations
    private int id;
    private String answers;

    // this is the constructor, it creates a new instance of Student
    public Student(int newStudentsId, String newStudentsAnswers) {
        // these lines take the parameters (in brackets on the line above)
        // and assign them to the fields of this instance
        this.id = newStudentsId;
        this.answers = newStudentsAnswers;
    }

    // after this you could have getters for the fields,
    // these are often referred to as "accessors" in introductory texts
    // this is all I'll give you of this example, to get you started

} // End of class Student.java

, , :

Incompatible operand types String and char

:

public static int gradeTally(String[] letterGrade) {
  // other code
    if(letterGrade[i] == 'A') a++;  // error on this line

, letterGrade[i], , 'A' char.

  • 'A' "A", , char, double String. :
  • letterGrade char[] letterGrade, ( ).

, :

Cannot invoke charAt(int) on the primitive type int

:

total = studentAnswers[i].charAt(a);

charAt - String, studentAnswers[i] String. int[] studentAnswers, " ".

, , . - Java ( Objects First With Java) , .

+3

2) 3):

2) , - , , studentAnswers answerKey. , , :

  • ? , , ?
  • , (, i), .
  • charAt ; java.lang.String, String.

3) , . , Map<Character, Integer>, - , - .

0

Java - - . . , C Fortran, Java. , .

, , , . , Java.

, Student, Class, Grade (, ), ReportCard. , , . - , Java.

0
source

Source: https://habr.com/ru/post/1730790/


All Articles