Results 1 to 2 of 2
Thread: Problem modifying existing code
- 12-10-2012, 12:31 AM #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); } 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:
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); 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, 03:52 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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); int[] grades = calcGrades(grade);
Website: Learn Java by Examples
Similar Threads
-
self-modifying code
By gyijhbk in forum New To JavaReplies: 1Last Post: 06-27-2012, 07:42 AM -
Modifying Given Code
By jmscarlet9 in forum New To JavaReplies: 5Last Post: 04-01-2012, 05:13 PM -
Modifying Automato Code
By whateverme in forum New To JavaReplies: 0Last Post: 02-06-2011, 02:36 PM -
Need help modifying code for hex conversion
By gamer765 in forum New To JavaReplies: 14Last Post: 10-25-2010, 06:59 AM -
java method injection in existing code
By abanmitra in forum Advanced JavaReplies: 6Last Post: 04-30-2010, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks