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 8158881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:45:15+00:00 2026-06-06T17:45:15+00:00

I have a design problem: Question class: have a question and answer (String) TextQuestion

  • 0

I have a design problem:

  • Question class: have a question and answer (String)
  • TextQuestion class: normal text question and answer
  • NumberQuestion class: question (String), answer (number – int, float…), and check answer using an epsilon value

But I have a problem with NumberQuestion. I must take the right answer, convert it into number, and compare to the answer from user. So how can I get the answer? Use getter method is not secure. Or my design is not good? How can I change?

public abstract class Question {
  private String question;
  private String answer;

  public Question(String question, String answer) {
      this.question = question;
      this.answer = answer;
  }

  public boolean checkAnswer(String yourAnswer) {
      // default implementation
  }
}

class TextQuestion extends Question {
  // Answer and Question is always string type, it's OK, not problem
}

class NumberQuestion extends Question {
  // Question is String, OK
  // Answer: input is number, accept approximately
  // Ex: Question: 1/3 = ?
  //     Answer: 0.3 or 0.33 or 0.333 are accepted (using an Epsilon value)

  // so must override checkAnswer
  public boolean checkAnswer(String yourAnswer) {
      // HOW can I do?
  }
}
  • 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-06-06T17:45:16+00:00Added an answer on June 6, 2026 at 5:45 pm

    I don’t see why you need to hide the answer from subclasses (or from any client code, for that matter). The client code surely needs to know the answer in the first place in order to create a TextQuestion or NumberQuestion instance.

    You should abstract away the logic of the test, as well as the answer type, from the Question class:

    public abstract class Question<AnswerType> {
      private String question;
      private AnswerType answer;
    
      public Question(String question, AnswerType answer) {
        this.question = question;
        this.answer = answer;
      }
    
      public abstract boolean checkAnswer(AnswerType yourAnswer);
    
      protected AnswerType getAnswer() {
        return answer;
      }
    }
    
    class TextQuestion extends Question<String> {
      public TextQuestion(String question, String answer) {
        super(question, answer);
      }
    
      public boolean checkAnswer(String yourAnswer) {
        return answer.equals(yourAnswer);
      }
    }
    
    class NumberQuestion extends Question<Double> {
      private double epsilon;
    
      public NumberQuestion(String question, Double answer, double epsilon) {
        super(question, answer);
        this.epsilon = epsilon;
      }
    
      public boolean checkAnswer(Double yourAnswer) {
        return Math.abs(getAnswer() - yourAnswer) < epsilon;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is mostly OOP design problem. I have a class which handles the
Just wanted opinions on a design question. If you have a C++ class than
I have a problem with serialisation. The class in question represents a network packet
I have a design problem: I have two threads, a heartbeat/control thread and a
Updated Problem solved, I have some design problem here. The directory looks like that:
I have an n-tier application design problem with Entity Framework 4. Server-side, my application
I have some sort of a design problem with my Django AJAX application. I
This is a design problem I am facing. Let's say I have a cars
I have read Factory Method pattern. I have a problem when design the latter
I have a problem trying to design some generic storage.. Basically I have the

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.