Results 1 to 2 of 2
  1. #1
    bill1991 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

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

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

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

Similar Threads

  1. self-modifying code
    By gyijhbk in forum New To Java
    Replies: 1
    Last Post: 06-27-2012, 08:42 AM
  2. Modifying Given Code
    By jmscarlet9 in forum New To Java
    Replies: 5
    Last Post: 04-01-2012, 06:13 PM
  3. Modifying Automato Code
    By whateverme in forum New To Java
    Replies: 0
    Last Post: 02-06-2011, 03:36 PM
  4. Need help modifying code for hex conversion
    By gamer765 in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 07:59 AM
  5. java method injection in existing code
    By abanmitra in forum Advanced Java
    Replies: 6
    Last Post: 04-30-2010, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •