You are viewing our Forum Archives. To view or take place in current topics click here.
Java Program Challenge Help
Posted:

Java Program Challenge HelpPosted:

jkush12
  • Challenger
Status: Offline
Joined: Jan 10, 201212Year Member
Posts: 197
Reputation Power: 8
Status: Offline
Joined: Jan 10, 201212Year Member
Posts: 197
Reputation Power: 8
Make sure that you use two program files, the DriverExam class and a DemoExam class that makes use of the DriverExam class.

I am not sure how to make a demoExam class. I have the whole source code done but i do not know how to make use of the DriverExam class to make a demo class.

This is the whole code for DriverExam:

import java.util.Scanner; // Needed for the Scanner class

public class DriverExam
{
public static void main(String[] args)
{
// Create an array init to 0
char[] answerKey = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'},
studentAns = new char[answerKey.length];

double passing = 65.0;

// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
String input; // To hold a line of input
char answer; // To hold a single character;

// Ask the user to enter a student's answers.
for (int i = 0; i < answerKey.length; i++)
{
System.out.print("What is the answer to question #" + (i + 1) + "? ");
input = keyboard.nextLine();
studentAns[i] = input.charAt(0);
}

// Demonstrate the totalCorrect() function.
System.out.println("Total correct: " + totalCorrect(studentAns, answerKey) );


// Demonstrate the totalncorrect() function.
System.out.println("Total incorrect: " + totalIncorrect(studentAns, answerKey) );


// Demonstrate the questionsMissed() function.
int[] missedQuestions = questionMissed(studentAns, answerKey);
printMissedQuest(missedQuestions);


// Demonstrate the passed() function.
if (passed(studentAns, answerKey, passing) )
System.out.print("You passed!");
else
System.out.print("You failed. Better luck next time.");

System.out.println(" Passing is " + passing + "%.");


}

// Returns true if the student passed the exam or false if the student failed.
private static boolean passed(char[] exam, char[] answers, double passing)
{
double percentageCorrect = (totalCorrect(exam, answers) / answers.length * 100);
return (percentageCorrect >= passing);
}

// Returns the total number of correctly answered questions
private static int totalCorrect(char[] exam, char[] answers)
{
int numCorrect = 0;
for (int i = 0; i < exam.length; i++)
if (exam[i] == answers[i])
numCorrect++;

return numCorrect;
}

// Returns the total number of incorrectly answered questions
private static int totalIncorrect(char[] exam, char[] answers)
{
int numIncorrect = 0;
for (int i = 0; i < exam.length; i++)
if (exam[i] != answers[i])
numIncorrect++;

return numIncorrect;
}

// An int array containing the question numbers of the questions that the student missed.
private static int[] questionMissed(char[] exam, char[] answers)
{
int[] missedQuestions = new int[exam.length];
for (int i = 0; i < exam.length; i++)
if (exam[i] != answers[i])
missedQuestions[i] = (i+1);

return missedQuestions;
}

// Prints the values in a char array.
private static void printArray(char[] exam)
{
for (int row = 0; row < exam.length; row++)
{
System.out.print(exam[row] + " ");
}
System.out.println();
}

// Prints the values in an int array that are non-zero.
private static void printMissedQuest(int[] missedQ)
{
System.out.print("Questions missed: ");
for (int row = 0; row < missedQ.length; row++)
{
if (missedQ[row] != 0)
System.out.print(missedQ[row] + ", ");
}
System.out.println();
}

}
#2. Posted:
Eternity
  • 2 Million
Status: Offline
Joined: Jun 12, 201112Year Member
Posts: 590
Reputation Power: 2237
Status: Offline
Joined: Jun 12, 201112Year Member
Posts: 590
Reputation Power: 2237
Unless the skeleton for DriverExam is set in stone, you could change to make use of both classes. You could DemoExam as your "main" class, asking the user for their input (as you did in the main method for DriverExam). Then, in DriverExam, you would take the information from the user and put it through the various methods you have implemented (aka totalCorrect(), totalIncorrect(), questionMissed(), etc).

In short, the DemoExam class asks for the user input and the DriverExam class takes that information, processes it, generates an output, and sends it back to DemoExam for output
#3. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
For a DemoExam class, you need to take the user's input and check it against the answers, like you already have with the DriverExam.

What you will need to do is break the DriverExam class down to remove any specific things related to the driving exam.

e.g. Every exam has questions and answers. Every exam will need to check the answers given vs the correct answers.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.