I have this assignment for my java class, it runs fine, but the professor wants us to incorporate a private gradeExam method, which I don't know how it work fit, since I cannot call it from the DriverTest class which contains my main.

package project6;
/* A demonstration of how to Create Classes and Arrays and Test them.
 * Javier Falcon
 * Cop2250-U04 Project6
 * #5 on page 529 of the textbook
 *
*/
public class DriverExam {
    private char [] correct = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 
                               'C', 'D','B', 'C', 'D', 'A', 'D', 'C', 
                               'C', 'B', 'D', 'A'};
    private char [] student;
    private int [] missed;
    private int numCorrect = 0;
    private int numIncorrect = 0;
    /*
     * Constructor fills student with content in s
     * 
     * @param s char array filled with student answers
     * 
    */
    public DriverExam(char[] s)
    {
        student = s;
    }
    /**
    * This method makes sure the paper is graded.
    *
    * @param  
    * @return 
    */

    private void gradeExam()
    {
        totalCorrect();
        totalIncorrect();
        passed();
        questionsMissed();
    }
    /**
    * This method creates the array for the missed questions.
    * 
    * @param  
    * @return 
    */
    private void makeMissedArray()
    {
        int[] missed = {};
    }

    /**
    * This method determines whether you pass or fail.
    *
    * @param  
    * @return boolean which decides pass or fail
    */
    public boolean passed()
    {
        return (totalCorrect() > 14);
    }
    /**
    * This method calculates the number of correct answers.
    *
    * @param  
    * @return number of correct answers
    */
    public int totalCorrect()
    {
        int sameAnswers = 0;

        for (int i = 0; i < correct.length; i++)
        {
            if (student[i] == correct[i])
            {
                sameAnswers++;
            }
        }
        return sameAnswers;
    }
    /**
    * This method calculates and returns number of wrong.
    * 
    * @param             
    * @return number of incorrect answers
    */
    public int totalIncorrect()
    {
        int missedAnswers = 0;
        missedAnswers = correct.length - totalCorrect();
        return missedAnswers;
    }
    /**
    * This method determines which spots in the student array were
    * incorrect.
    * 
    * @param            
    * @return array with numerical values of those missed
    */
    public int[] questionsMissed()
    {
        int size = correct.length - totalCorrect();
        makeMissedArray();
        if (size < 1)
            return missed;
        else
            missed = new int [size];
        int pos = 0;
        for (int i = 0; i < correct.length; i++)
        {
            if (correct[i] != student[i])
            {
                missed[pos] = (i + 1);
                pos = pos + 1;
            }
        }
        return missed;
    }

}

Though you cannot call the method directly from another class since it is not visible to other classes(because of private access modifier).
So you can create another pubilc method that calls private method "gradeExam()" like below inside the class DriverExam.

public void callGradeExam(){
     gradeExam();        
}

Instead of making a call to "gradeExam()" method from other classes please make a call to callGradeExam().

Another alternative is to use java reflection api. Using reflection api you can access any method / field having any access modifier of any class from someother class .But it is highly discouraged to use reflection api for the current purpose.

If it is rquired for testing only ( that is test the class DriverExam ) then you can use Junit or some mocking framework like easymock.
Link for easymock is given here http://www.easymock.org/. Easymock provides the api to acess private methods and fields of other classes for unit testing.

Edited 4 Years Ago by subramanya.vl

yes, that's possible, but would make the private method useless. private methods are normally used to perform "private" stuff. at this point, it is clear that your teacher does not want your main method to have access to your private method (otherwise he would've told you to make it public).

so, you'll have to call it in the class itself. what stops you from calling it from within the constructor?

that either way I'm going to be calling the individual methods from DriverTest to display, so I would find calling it in the constructor just repetition and a waste of memory.

if you're supposed to call them from within a private method, you should consider them as being 'private'.
did you get any guiding on what that method should do?
if that method is supposed to call those methods, it's calling those methods in your main method that is a waste of memory.

we got no comments on what grade exam is suposed to do, and the others have return types so if i were to call them from gradeExam id make them voids. or maybe display output in the gradeExam, which destroys the logic behind even having the Driver test class in my opinion.

Edited 4 Years Ago by Nandomo

well, for instance:

public int[] questionsMissed(){

}

if you keep data in instance variables, you can set those values using the gradeExams() method

so it would be something like:

int[] missed; // even though I don't see why this should be an array

public int[] questionsMissed(){
  gradeExams();
  return this.missed;
}

private void gradeExams(){
  this.missed = new int[5];
}

its an array because it holds the numerical value of the questions you got wrong.

and for what you did there i have : makeMissedArray() which is also private for same reason i dont understand since it would just be more simple to make a local array in questionssMissed(), but this professor is not very bright.

Edited 4 Years Ago by Nandomo

or you haven't figured out yet what he's trying to teach you. make no mistake, a lot of students think their teachers are dumb, but that doesn't mean they are.

I must admit, I don't really see the point of this code either, but I do take into account that he has to teach you the topic he's trying to teach you, and can only use things you've already learned, which may be (or not be) much.

the better your knowledge of the language, the easier it would be to make up a context in which to show something new.

I understand, thus im calling gradeExam from the constructor and I'll just display everything from there. Ty for all the help either way.

This question has already been answered. Start a new discussion instead.