Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3979586
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:11:17+00:00 2026-05-20T05:11:17+00:00

I’m trying some Java recently and look for some review of my style. If

  • 0

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();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:11:18+00:00Added an answer on May 20, 2026 at 5:11 am

    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);
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.