-1

There is a "Question" class which are designed to create questions for users.

checkAnswer method of the Question class is designed for taking response which does not take into account different spaces or upper/lowercase characters. For example, the response "abcd" should match an answer of "A b Cd".

/** A question with a text and an answer. */

public class Question
{
   private String text;
   private String answer;

/** Constructs a question with empty question and answer. */

   public Question() 
   {
      text = "";
      answer = "";
   }

/** Sets the question text. @param questionText the text of this question */

   public void setText(String questionText)   
   {
      text = questionText;
   }

/** Sets the answer for this question. @param correctResponse the answer */

   public void setAnswer(String correctResponse)
   {
      answer = correctResponse;
   }

/** Checks a given response for correctness. @param response the response to check @return true if the response was correct, false otherwise */

   public boolean checkAnswer(String response)
   {
      response = response.toLowerCase();
      for(int i = 0; i <= response.length() - 1; i++)
      {
         if(response.charAt(i) == ' ')
         {
            response = response.subString(0, i) + 
            response.subString(i, response.length());
         }
      }

      response.equals(answer.defaultAnswer());
   }

/** Displays this question. */

   public void display()
   {
      System.out.println(text);
   }

   public void displayAnswer()
   {
      System.out.println(answer);
   }

   private String defaultAnswer()
   {
      answer = answer.toLowerCase();
      for(int i = 0; i <= answer.length() - 1; i++)
      {
         if(answer.charAt(i) == ' ')
         {
            answer = answer.subString(0, i) + 
            answer.subString(i, answer.length());
         }
      }
   }
}

enter image description here

X.Steve
  • 39
  • 6
  • 1
    Java and JavaScript are different languages, so I've removed the "JavaScript" tag from this question, but both languages are **case sensitive**. – nnnnnn Mar 19 '17 at 06:40
  • 2
    it `substring` not `subString`. Method name is all lowercase in Java. – Chetan Mar 19 '17 at 06:43

1 Answers1

2

You are using methods subString() and defaultAnswer() that are not defined for String class.

  • The first one actually seems like a typo. Replace it with substring().

  • For the second one - you have defined the method, defaultAnswer() for the Question class and the variable, answer is of String class. You can fix this by replacing response.equals(answer.defaultAnswer()); with - response.equals(this.defaultAnswer());

Yogesh
  • 699
  • 1
  • 6
  • 21