# Thread: Problem modifying existing code

1. Member
Join Date
Oct 2012
Posts
6
Rep Power
0

## 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:

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

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();
section++;
}
}
}```

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

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:

Java 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);

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

int[] points = new int[5];
for (int i = 0; i < points.length; i++) {
grades[i] = 100.0 * points[i] / 20.0;
}
}
}```

*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!

2. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
10

## Re: Problem modifying existing code

You've never define the type of these three variables in your program: attend, point and grade.

Java Code:
```int[] attended = getAttendance(attend);
int[] points = calcScores(point);