• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Resident Java Programmers? Minor help needed

Status
Not open for further replies.

Hero

Member
I have to create an application that creates a quiz. The quiz is supposed to be at least 5 questions long, with 4 choices for each multiple choice question. If the answer is wrong, it is to display a message saying that its wrong and to give the right answer, and if it is right to display a congratulatory message. At the end of the quiz it should display the number of incorrect and correct answers, and the percentage of correct answers.

The problem I'm having is how to move onto the next question, and what code to add in to calculate the number of correct/incorrect answers and the percentage.

This is my code so far:

Code:
public class Quiz
{
    public static void main(String[] args) throws Exception
    {
        char response='y';
        int choice=0;
        String order="";
        //while(response=='y')
        do
        {
        System.out.println("What is the capital of Pennsylvania?");
        System.out.println("***************");
        System.out.println("1. Philadelphia");
        System.out.println("2. Harrisburgh");
        System.out.println("3. Easton");
        System.out.println("4. Pittsburgh");
        System.out.print("Enter your answer ");
        choice=System.in.read();
        System.in.read(); System.in.read(); //dummy System.in.read() to take care of Enter keystroke
        choice=choice-48;

        switch(choice)
        {
        case 1: order +="Philadelphia ";
          System.out.println("Sorry, that is not the correct answer");
          System.out.println("The correct answer is Harrisburgh");
            break;
            case 2: order+="Harrisburgh ";
              System.out.println("That is the correct answer!");
                break;
                case 3: order+="Easton ";
                  System.out.println("Sorry, that is not the correct answer");
                  System.out.println("The correct answer is Harrisburgh");
                    break;
                    case 4: order+="Pittsburgh ";
                      System.out.println("Sorry, that is not the correct answer");
                      System.out.println("The correct answer is Harrisburgh");
                    default: order+="";
        }
        System.out.println("Do you want to stop");
        response=(char)System.in.read();
        System.in.read(); System.in.read();
        }while(response=='y');//close while
      

        }


    }

I know it's not supposed to be that hard, but I'm kind of stuck. I imagine I should have a counter after each question that should increment depending on the answer?

Thanks in advance.
 

Vlad

Member
You've got the right idea, actually. A counter that's incremented after each question (right or wrong) could be used to control which question is put on the screen each time.

You'd actually need two counter variables, one to determine which question the user is on, and one to keep track of how many right answers they have.
 

iapetus

Scary Euro Man
That is not Java. Please slap your teacher for me. Anyone who's teaching 'Java' without at least some pretence of OO before the end of the course deserves it.
 

Vlad

Member
iapetus said:
That is not Java. Please slap your teacher for me. Anyone who's teaching 'Java' without at least some pretence of OO before the end of the course deserves it.

Well, that looks like something from the beginning of a course, actually. At least, that's what I assumed.

But yeah, if that's an assignment from near the end, then something's quite wrong.
 

iapetus

Scary Euro Man
Even if it's day one of the course, it's a bad course. OO should be at the heart of any good Java course, not introduced half way through, and certainly not an afterthought.

Still, what do I know? I only used to write Java training courses for IBM...
 

Hero

Member
The code I posted isn't going to be what I hand in, I just wrote that to try and see where I should put counters and the such.

Most likely I'm going create an applet and use ActionListener to create the final build.
 

Phoenix

Member
Hero said:
The code I posted isn't going to be what I hand in, I just wrote that to try and see where I should put counters and the such.

Most likely I'm going create an applet and use ActionListener to create the final build.

And hopefully OO-ify it by making the Questions themselves objects.
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
Code:
public class Quiz
{
    public static void main(String[] args) throws Exception
    {
        char response='y';
        int choice=0;
        String order="";
        //while(response=='y')
        do
        {
        System.out.println("What is the capital of Pennsylvania?");
        System.out.println("***************");
        System.out.println("1. Philadelphia");
        System.out.println("2. Harrisburgh");
        System.out.println("3. Easton");
        System.out.println("4. Pittsburgh");
        System.out.print("Enter your answer ");
        choice=System.in.read();
        System.in.read(); System.in.read(); //dummy System.in.read() to take care of Enter keystroke
        choice=choice-48;

        switch(choice)
        {
        case 1: order +="Philadelphia ";
          System.out.println("Sorry, that is not the correct answer");
          System.out.println("The correct answer is Harrisburgh");
            break;
            case 2: order+="Harrisburgh ";
              System.out.println("That is the correct answer!");
                break;
                case 3: order+="Easton ";
                  System.out.println("Sorry, that is not the correct answer");
                  System.out.println("The correct answer is Harrisburgh");
                    break;
                    case 4: order+="Pittsburgh ";
                      System.out.println("Sorry, that is not the correct answer");
                      System.out.println("The correct answer is Harrisburgh");
                    default: order+="";
        }
        System.out.println("Do you want to stop");
        response=(char)System.in.read();
        System.in.read(); System.in.read();
        }while(response=='y');//close while
      

        }


    }

You've got the right idea, but I will say one thing:
you shouldn't use a switch in this case.
 

iapetus

Scary Euro Man
GaimeGuy said:
You've got the right idea, but I will say one thing: you shouldn't use a switch unless a giant hairy man with a chainsaw threatens to separate your testicles from your body if you don't.

Fixed. Although I'm still not quite sure I agree with you that this is a valid case for using switch.
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
iapetus said:
Fixed. Although I'm still not quite sure I agree with you that this is a valid case for using switch.
Wouldn't it make more sense to make the switch statement an if statement? After atll, there's only two possible outcomes to the choices. All incorrect responses return the same string.
 

iapetus

Scary Euro Man
I'd do what Phoenix and GaimeGuy suggest. In this case there are only two possible results - the user gave the right answer, or the user gave the wrong answer, so it's a straightforward if/else, more along the lines of:

Code:
if (question.isAnswerCorrect(answer)) {
  System.out.println("Yes, yes, yes.");
} else {
  System.out.println("Wrong, wrong, absolutely brimming over with wrongability.");
  System.out.println("The right answer was '" + question.getCorrectAnswer() + "'");
}

I must reluctantly point out that Red Dwarf quotes are optional, and that not everyone shares my irrational hatred of switch statements (especially Java ones).
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
iapetus said:
I'd do what Phoenix and GaimeGuy suggest. In this case there are only two possible results - the user gave the right answer, or the user gave the wrong answer, so it's a straightforward if/else, more along the lines of:

Code:
if (question.isAnswerCorrect(answer)) {
  System.out.println("Yes, yes, yes.");
} else {
  System.out.println("Wrong, wrong, absolutely brimming over with wrongability.");
  System.out.println("The right answer was '" + question.getCorrectAnswer() + "'");
}

I must reluctantly point out that Red Dwarf quotes are optional, and that not everyone shares my irrational hatred of switch statements (especially Java ones).
Yeah, the switch statement sucks.

Oh, and if you want to ignore the return key, just make an inner do-while loop like this:

Code:
do {
     ch = (char) System.in.read();
}   while(ch == "\n" | ch == "\r");
 
I've been using Jcreator for all my java editing for the past month (i'm a beginner) but i'm finding it to be a bit to simplistic.
What software should I use that incorporates graphics and windows (not the OS) better. Basically something that helps me in my transiion from the system.out.println to the java.awt
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
gamepro said:
I've been using Jcreator for all my java editing for the past month (i'm a beginner) but i'm finding it to be a bit to simplistic.
What software should I use that incorporates graphics and windows (not the OS) better. Basically something that helps me in my transiion from the system.out.println to the java.awt

I don't know. BlueJ?
 

iapetus

Scary Euro Man
Best Java IDE bar none is IntelliJ IDEA. There's no GUI builder, but I don't see that as an inherently bad thing, given most of the GUI builders I've had the... pleasure of working with. Weighing in at a heck of a lot cheaper (and with GUI builder available) is Eclipse, which is free.
 

CaptainABAB

Member
My suggestion: think about the data structures or class design before you just start coding.


For example, maybe create a Question object that contains a list/array/collection of answer objects where you have an question attribute that identifies which answer is the correct one.

Then create a list/array/collection (your choice) of Questions. Wrap this inside a quiz object.


Create a class dedicated to populating the quiz+questions+answers.

Create another class just to run the quiz, looping through the questions tracking the results in attributes in the Quiz object.

...or something like that.
 

Nerevar

they call me "Man Gravy".
gamepro said:
I've been using Jcreator for all my java editing for the past month (i'm a beginner) but i'm finding it to be a bit to simplistic.
What software should I use that incorporates graphics and windows (not the OS) better. Basically something that helps me in my transiion from the system.out.println to the java.awt

Microsoft J++!

Then, right before you hang yourself, remember me recommending it to you!


BTW, real hardcore programers use vi.
 

Phoenix

Member
iapetus said:
Best Java IDE bar none is IntelliJ IDEA. There's no GUI builder, but I don't see that as an inherently bad thing, given most of the GUI builders I've had the... pleasure of working with. Weighing in at a heck of a lot cheaper (and with GUI builder available) is Eclipse, which is free.

IntelliJ IDEA *does* have a GUI builder. Picked it up in version 4.x.

Eclipse and Netbeans are good free alternatives which both have GUI builders as well.
 
Status
Not open for further replies.
Top Bottom