# Problem modifying existing code

• 12-10-2012, 01:31 AM
bill1991
Problem modifying existing code
So for my programming class, I've been assigned a problem where I have to modify an existing code, break it into methods, and basically make it much neater. It takes this file:

yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna
ayyanyyyyayanaayyanayyyananayayaynyayayynynya
yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny

and calculates points and grade based on the letters. We have to modify that to this:

Ann Mike Fred Mary Bob
yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna
Bob Carol Ted Alice Nancy
ayyanyyyyayanaayyanayyyananayayaynyayayynynya
Alex Laura Karyn Chava Wendy
yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny

and then make it so it will take the person's name, their attendance (y and n mean they attended, a means absent), their points (y = 3, n = 2, a = 0, maximum of 20) and their grade based on the 20 points.

Here's the existing code:

Code:

```import java.io.*; import java.util.*; public class Sections {     public static void main(String[] args) throws FileNotFoundException {         Scanner input = new Scanner(new File("sections.txt"));         int section = 1;         while (input.hasNextLine()) {             String line = input.nextLine();      // process one section             int[] points = new int[5];             for (int i = 0; i < line.length(); i++) {                 int student = i % 5;                 int earned = 0;                 if (line.charAt(i) == 'y') {    // c == 'y' or 'n' or 'a'                     earned = 3;                 } else if (line.charAt(i) == 'n') {                     earned = 2;                 }                 points[student] = Math.min(20, points[student] + earned);             }             double[] grades = new double[5];             for (int i = 0; i < points.length; i++) {                 grades[i] = 100.0 * points[i] / 20.0;             }             System.out.println("Section " + section);             System.out.println("Student points: " + Arrays.toString(points));             System.out.println("Student grades: " + Arrays.toString(grades));             System.out.println();             section++;         }     } }```

So the methods we have to break it down into are as follows:

Quote:

getAttendance: This method counts the classes attended by each student for one section.
Input parameters: a string containing the class attendance data for one section only
Output parameters: none
Return value: an array of counters giving the attendance of each student.
--------------------------
printSection: This method prints out the entire report for a single section.
Input parameters:
an array to hold the names of students
an array for the attendance of each student
an arrray for the score for each student
an array for the grade of each student
an integer for the section number
Output parameters: none, i.e. this method should not change the contents of any array
Return value: none
--------------------------
calcScores: computes the score (maximum of 20) for each student of one section.
Input parameters: a string containing the class attendance data for one section only
Output parameters: none
Return value: an array for the score of each student of this section
--------------------------
calcGrades: calculates the grade (maximum of 100.0) for each student of a section.
Input parameters: an array for the score for each student of one section only
Output parameters: none (the score array should not be changed)
Return value: an array for the grade of each student for this section

Now, my code that is giving me problems:

Code:

```import java.io.*; import java.util.*; public class HW4 {     public static void main(String[] args) throws FileNotFoundException {         Scanner input = new Scanner(new File("sections.txt"));         int section = 1;                        while (input.hasNextLine()) {             String namesLine = input.nextLine();             String[] names = namesLine.split(" ");                 int[] attended = getAttendance(attend);                 int[] points = calcScores(point);                 int[] grades = calcGrades(grade);             System.out.println("Section " + section);             System.out.println("Name: " + Arrays.toString(names));             System.out.println("Attendance: " + Arrays.toString(attended));             System.out.println("Student points: " + Arrays.toString(points));             System.out.println("Student grades: " + Arrays.toString(grades));             System.out.println();             section++;         }     }         public static int getAttendance (String attend) {             Scanner input = new Scanner(attend);             int studentAttend = 0;             while(input.hasNextLine()){                     String line = input.nextLine();                     for(int i = 0; i < line.length(); i++){                             if (line.charAt(i) == 'y'){                                     studentAttend++;                             } else if (line.charAt(i) == 'n'){                                     studentAttend++;                             }                     }             }             return studentAttend;     }     /*    public static void printSection(String[] student, int[] attend, int[] score, double[] grade, int section) {     } */         public static int[] calcScores(String point){         Scanner input = new Scanner(point);                                 String line = input.nextLine();             int[] points = new int[5];             for (int i = 0; i < line.length(); i++) {                 int student = i % 5;                 int earned = 0;                 if (line.charAt(i) == 'y') {                     earned = 3;                 } else if (line.charAt(i) == 'n') {                     earned = 2;                          }                 points[student] = Math.min(20, points[student] + earned);             }             return points;     }         public static double[] calcGrades(double[] grade){             int[] points = new int[5];             double[] grades = new double[5];         for (int i = 0; i < points.length; i++) {             grades[i] = 100.0 * points[i] / 20.0;         }         return grades;     } }```

*NOTE* I didn't use the printSections method yet, because I wanted to get this all working in main.

My problem is that when I call the methods in the while loop, they couldn't be resolved to variables. I don't know if I'm calling them incorrectly or if my methods are wrong, and if they are I don't know how I'm meant to fix them. I'd really appreciate any help on making this code work up until the printSections method so I can continue to fix up this code.

Thanks to anyone who helps!
• 12-10-2012, 04:52 AM
```int[] attended = getAttendance(attend); int[] points = calcScores(point); int[] grades = calcGrades(grade);```