0

I have to select one of the following objectives:

//1. Make a parallel array of the number of correct answers. Send the method //answers and the answerKey. Return the filled parallel array that contains //the number correct.

correct = correctAnswer(answers, answerKey);

//2. Make a parallel array containing students' letter grades. Send correct
//and return the parallel array.

grade= getGrade(correct);

//3. Count how many of each grade

numOfEach=count(grade);

import java.io.*;

public class template
{   
 public static void main(String[] args) throws IOException
{
    //creating objects and variables    
    int lineCount;    
    //count how many lines in the file
    lineCount = countLines(counter); 
    String answerKey;
    String[] studentID = new String[lineCount];//store student id
    String[] answers = new String[lineCount];//store student answers
    int[] correct = new int[lineCount];//store number they got correct
    char[] grade = new char[lineCount];//store letter grade
    char[] key = new char[10];//holds answer key
    char[] numOfEach=new char[5];//hold number of each grade
    //initialize file
    initFile()

    //Page 323 tells us that if I pass arrays the actual array will be modified so I can call a //method to read in the entire file
    readFile(answers,studentID,key,lineCount);      
    //make parallel array of number of correct answers. Send the methods answers and // //answerKey return the filled parallel array containing the number correct.
    correct= correctAnswer(answers,  answerKey);
    //Make a parallel array containing student’s letter grade.  Send correct and return parallel //array.
    grade= getGrade(correct);
    //Count how many of each grade
    numOfEach=count(grade);
    //Output the desired results
    outputResults(studentID, correct, grade);
}

}

I decided to select the last one "//3. Count how many of each grade

numOfEach=count(grade);"

I'm kind of stuck, so far I came up with this:

public static void count(char[] grade){

int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0;

for (int i = 0; i < grade.length; i++){
    if (grade[i] == 'A'){
        numofa++;
    }else if (grade[i] == 'B'){
    numofb++;
    }else if (grade[i] == 'C'){
        numofc++;
    }else if (grade[i] == 'D'){
        numofd++;
    }else if (grade[i] == 'F'){
        numoff++;
    }   
}

} 

However, I don't know how to populate the numOfEach char array with the data I analyze (How many of each letter grade)

user3397631
  • 3
  • 1
  • 1
  • 2

2 Answers2

0

Instead of

public static void count(char[] grade)  {
   int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0;

   for (int i = 0; i < grade.length; i++){
      if (grade[i] == 'A'){
         numofa++;
      }else if (grade[i] == 'B'){
      numofb++;
      }else if (grade[i] == 'C'){
         numofc++;
      }else if (grade[i] == 'D'){
         numofd++;
      }else if (grade[i] == 'F'){
         numoff++;
      }   
   }
}

Use

public static int[] count(char[] grade)  {
   int[] numOfEach = new int[5];  //5: a, b, c, d, f
   int idxA = 0;
   int idxB = 1;
   int idxC = 2;
   int idxD = 3;
   int idxF = 4;

   for (int i = 0; i < grade.length; i++){
      if (grade[i] == 'A'){
         numOfEach[idxA]++;
      }else if (grade[i] == 'B'){
         numOfEach[idxB]++;
      }else if (grade[i] == 'C'){
         numOfEach[idxC]++;
      }else if (grade[i] == 'D'){
         numOfEach[idxD]++;
      }else if (grade[i] == 'F'){
         numOfEach[idxF]++;
      }   
   }
   return  numOfEach;
}

You cannot hold the amount of each grade in a char array.

Well...you could, but I do not understand the purpose of doing so:

public static char[] count(char[] grade)  {
   char[] numOfEachAsChars = new char[5];  //5: a, b, c, d, f
   int idxA = 0;
   int idxB = 1;
   int idxC = 2;
   int idxD = 3;
   int idxF = 4;

   for (int i = 0; i < grade.length; i++){
      if (grade[i] == 'A'){
         numOfEachAsChars[idxA]++;
      }else if (grade[i] == 'B'){
         numOfEachAsChars[idxB]++;
      }else if (grade[i] == 'C'){
         numOfEachAsChars[idxC]++;
      }else if (grade[i] == 'D'){
         numOfEachAsChars[idxD]++;
      }else if (grade[i] == 'F'){
         numOfEachAsChars[idxF]++;
      }   
   }
   return  numOfEachAsChars;
}

As you can see in the below example, the raw-numeric value of the character 1 is not one, so I don't recommend the char-array-returning function.

/**
   <P>{@code java RawNumericValueOfChar}</P>
 **/
public class RawNumericValueOfChar  {
   public static final void main(String[] ignored)  {
      int intValueOfCharOne = '1';
      int intValueOfCharALower = 'a';
      int intValueOfCharAUpper = 'A';

      System.out.println("intValueOfCharOne=" + intValueOfCharOne);
      System.out.println("intValueOfCharALower=" + intValueOfCharALower);
      System.out.println("intValueOfCharAUpper=" + intValueOfCharAUpper);

      int intValueOfCharOneInc = ((int)'1') + 1;
      int intValueOfCharALowerInc = ((int)'a') + 1;
      int intValueOfCharAUpperInc = ((int)'A') + 1;

      System.out.println("intValueOfCharOneInc=" + intValueOfCharOneInc);
      System.out.println("intValueOfCharALowerInc=" + intValueOfCharALowerInc);
      System.out.println("intValueOfCharAUpperInc=" + intValueOfCharAUpperInc);

   }
}

Output:

[C:\java_code\]java RawNumericValueOfChar
intValueOfCharOne=49
intValueOfCharALower=97
intValueOfCharAUpper=65
intValueOfCharOneInc=50
intValueOfCharALowerInc=98
intValueOfCharAUpperInc=66

Here is some information that may be helpful:

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • This sounds great, however, the array has to be a char array (as assigned) I have no idea as to how I can store the things in a char array. – user3397631 Mar 09 '14 at 15:41
0

First of all numOfEach should be an int array and not a char array

You can write count function as :

public static void count(char[] grade,int []count)//changed
{

int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0;

for (int i = 0; i < grade.length; i++){
    if (grade[i] == 'A'){
        numofa++;
    }else if (grade[i] == 'B'){
    numofb++;
    }else if (grade[i] == 'C'){
        numofc++;
    }else if (grade[i] == 'D'){
        numofd++;
    }else if (grade[i] == 'F'){
        numoff++;
    }   

}

//changed
count[0]=numofa;
count[1]=numofb;
count[2]=numofc;
count[3]=numofd;
count[4]=numoff;
} 

and the call to this function would be like

count(grade,numOfEach);
  • This seems to be a really efficient solution, however, the only parameter I can pass is the "grade", and it has to be "numOIfEach = count(grade)" to call the method. – user3397631 Mar 09 '14 at 15:42
  • For that you can declare count as a local array and return it – AdityaPande Mar 10 '14 at 12:43