Question: HAVING TROUBLE GETTING MY CODE TO RUN. THE ERROR IS AT PART 5 WITH MY FOR LOOP. CANNOT I NOT USE AN i INT TO

HAVING TROUBLE GETTING MY CODE TO RUN. THE ERROR IS AT PART 5 WITH MY FOR LOOP. CANNOT I NOT USE AN i INT TO RUN THE LOOP?

 

/**

A FillInQuestion is a type of question where the responder has to fill in the

blanks. You should complete the constructor for this question.


 

An object of this class is constructed with

a string that contains the question and the answer.

The answer is marked by underscores at the start and end,

for example:


 

"The inventor of Java was _James Gosling_".


 

The question should be displayed as


 

The inventor of Java was _______________.


 

Note the number of underscores in the question is

2 plus the length of the answer string, in this case,

2 + 13 = 15.


 

You should complete the following tasks:


 

- Complete the constructor

*/

class FillInQuestion extends Question

{

/**

A constructor method to initialize the inherited text and answer variables (of type string)

For example, if parameter questionText is:

"The inventor of Java was _James Gosling_".

then text should be set to

"The inventor of Java was _______________".

and the answer should be set to

"James Gosling"

The answer string may be in the middle, as in questionText:

"A Canadian, _James Gosling_, invented Java".

in which case text should be:

"A Canadian, _______________, invented Java".

and the answer should be:

"James Gosling"


 

You can assume only valid inputs are given.

@param questionText the text of this question, also containing answer surrounded by one underscore on each end of the answer

*/

public FillInQuestion(String questionText)

{

char underscore = '_' ;

//-----------Start below here. To do: approximate lines of code = 7

//-----------Start below here. To do: approximate lines of code = 7

// 1. find position of first underscore, hint indexOf(char)

//Note, indexOf(char) and indexOf(char, from) can be used here


 

int first = questionText.indexOf("_");


 

//2. second underscore, hint indexOf(char, from)

 

int second = questionText.indexOf("_", first + 1);


 

//3. extract the answer string Hint: use substring

 

String answer = questionText.substring(first + 1);


 

//4. setAnswer (see superclass Question - remember you inherit all variables and methods

 

super.setAnswer(answer);


 

//5. String underscores = ... underscores string as long as answer string is

//replaceAll() method of class String helps, or you can do it in a loop

 

String underscores;

for( i = first + 1; i

{

underscores = underscores.concat("_");

}


 

//6. replace answer embedded in questionText with the underscores before setting text string

 

questionText.replaceAll(answer, underscores);


 

//7. setQuestion to set text variable

super.setQuestion(questionText);

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}

}

A class for a Question on a quiz or test. There is 

A class for a Question on a quiz or test. There is nothing to do for this class. */ class Question { //instance variables private String text; private String answer; /** A default constructor method that initializes text and answer */ public Question ( ) { text = "1 + 1 = " ; answer = "2" ; } } /** A constructor method to initialize text to parameter questionText, set answer to empty String @param questionText the text of this question */ public Question (String questionText) { text questionText; answer = ""; } /** Sets the answer for this question. @param correctResponse the answer */ public void setAnswer(String correctResponse) { } /** answer = correctResponse; Gets the answer for this question. @return the correct answer */ public String getAnswer() { return answer; } /** 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) { return response.equals (answer); } /** Sets the question text @param questionText the text of this question */ public void setQuestion (String questionText) { text = questionText; } /** */ Displays this question. public void display ( ) { } System.out.println(text);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!