简体   繁体   中英

Java exercise for checkout

I'm trying some Java recently and look for some review of my style. If You like to look at this exercise placed in the image, and tell me if my style is good enought? Or maybe it is not good enought, so You can tell me on what aspect I should work more, so You can help me to improve it?

exercise for my question

/*
 * File: MathQuiz.java
 * 
 * This program produces Math Quiz. 
 */
import acm.program.*;
import acm.util.*;

public class MathQuiz extends ConsoleProgram {

    /* Class constants for Quiz settings. */
    private static final int CHANCES = 3;
    private static final int QUESTIONS = 5;
    private static final int MIN = 0;
    private static final int MAX = 20;

    /* Start program. Number of questions to ask is assigned here. */
    public void run() {
        println("Welcome to Math Quiz");
        while(answered != QUESTIONS) {
            produceNumbers();
            askForAnswer();
        }
        println("End of program.");
    }

    /* Ask for answer, and check them. Number of chances includes
     * first one, where user is asked for reply. */
    private void askForAnswer() {
        int answer = -1;
        if(type)
            answer = readInt("What is " + x + "+" + y + "?");
        else
            answer = readInt("What is " + x + "-" + y + "?");
        for(int i = 1; i < CHANCES+1; i++) {
            if(answer != solution) {
                if(i == CHANCES) {
                    println("No. The answer is " + solution + ".");
                    break;
                }
                answer = readInt("That's incorrect - try a different answer: ");
            } else {
                println("That's the answer!");
                break;
            }
        }
        answered++;
    }

    /* Produces type and two numbers until they qualify. */
    private void produceNumbers() {
        produceType();
        produceFirst();
        produceSecond();
        if(type)
            while(x+y >= MAX) {
                produceFirst();
                produceSecond();
            }
        else
            while(x-y <= MIN) {
                produceFirst();
                produceSecond();
            }
         calculateSolution();
    }

    /* Calculates equation solution. */
    private void calculateSolution() {
        if(type) solution = x + y;
        else solution = x - y;
    }

    /* Type of the equation. True is from plus, false is for minus. */
    private void produceType() {
        type = rgen.nextBoolean();
    }

    /* Produces first number. */
    private void produceFirst() {
        x = rgen.nextInt(0, 20);
    }

    /* Produces second number. */
    private void produceSecond() {
        y = rgen.nextInt(0, 20);
    }

    /* Class variables for numbers and type of the equation. */
    private static boolean type;
    private static int x;
    private static int y;

    /* Class variables for equation solution. */
    private static int solution;

    /* Class variable counting number of answered equations,
     * so if it reaches number of provided questions, it ends */
    private static int answered = 0;

    /* Random generator constructor. */
    RandomGenerator rgen = new RandomGenerator();

}

One thing I noticed was that all of your methods take no parameters and return void.

I think it would be clearer if you use method parameters and return values to show the flow of data through your program instead of using the object's state to store everything.

Looks like a very clean program style. I would move all variables to the top instead of having some at the bottom, but other than that it is very readable.

Here is something I'd improve: the boolean type that is used to indicate whether we have an addition or subtraction:

private void produceType() {
    type = rgen.nextBoolean();
}

produceType tells, that something is generated and I'd expect something to be returned. And I'd define enums to represent the type of the quiz. Here's my suggestion:

private QuizType produceType() {
   boolean type = rgen.nextBoolean();
   if (type == true)
      return QuizType.PLUS;
   else 
      return QuizType.MINUS;
}

The enum is defined like this:

public enum QuizType { PLUS, MINUS }

There are a few things you should do differently, and a couple you could do differently.

The things you should do differently:

  1. Keep all fields together.
  2. static fields should always be in THIS_FORM
  3. you've used the static modifier for what clearly look like instance fields. (type,x,y,solution, answered). This means you can only ever run one MathsQuiz at a time per JVM. Not a big deal in this case, but will cause problems for more complex programs.
  4. produceFirst and produceSecond use hardcoded parameters to nextInt rather than using MAX and MIN as provided by the class
  5. There is no apparent need for answered to be a field. It could easily be a local variable in run .

Things you should do differently:

  • There is a small possibility (however tiny), that produceNumbers might not end. Instead of producing two random numbers and hoping they work. Produce one random number and then constrain the second so that a solution will always be formed. eg. say we are doing and addition and x is 6 and max is 20. We know that y cannot be larger than 14. So instead of trying nextInt(0,20) , you could do nextInt(0,14) and be assured that you would get a feasible question.
  • For loop isn't really the right construct for askForAnswer as the desired behaviour is to ask for an answer CHANCES number of times or until a correct answer is received, whichever comes first. A for loop is usually used when you wish to do something a set number of times. Indeed the while loop in run is a good candidate for a for loop. A sample while loop might look like:

     int i = 1; boolean correct = (solution == readInt("What is " + x + "+" + y + "?")); while (i < CHANCES && !correct) { correct = (solution == readInt("Wrong, try again.")); i++; } if (correct) { println("Well done!"); } else { println("Nope, the answer is: "+solution); } 

Almost good I have only a few improvements:

  1. variables moves to the top

  2. Inside produceNumbers and your while you have small repeat. I recommend refactor this

  3. Small advice: Code should be like books - easy readable - in your run() method firstly you call produceNumber and then askForAnswer. So it will be better if in your code you will have the same order in definitions, so implementation askForAnswer before produceNumber. But it isn't necessary

  4. Pay attention to have small methods. A method shouldn't have much to do - I think that askForAnswer you could split to two methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM