...

View Full Version : Needs a bit of help with javascript program.



seabass
11-14-2010, 05:08 AM
For class NumericQuestion Add setAnswer, a method that takes a double as input, converts it to a string and
calls setAnswer in the superclass Question to store the answer.
o Add checkAnswer, a method that takes an answer in string form, converts it to a
double, gets the correct answer in string form from Question, converts it to a
double, and returns true if they are within 0.01 of each other and false otherwise.

For class quiz Change main so that all three questions are asked. presentQuestion that prints out the correct answer, only when the
answer is wrong

public class NumericQuestion {
private String text;
private Double answer;


/**
* Constructs a question with empty question and answer.
*/
public NumericQuestion() {
text = "";
answer = 3.1416;
}

/**
* 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(Double 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) {
return response.equals(answer);
}

/**
* Displays this question.
*/

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

}




import java.util.Scanner;

public class Quiz {
public static void main(String[] args)
{
Question first = new Question();
first.setText("What inherits data and behavior from a superclass?");
first.setAnswer("subclass");

ChoiceQuestion second = new ChoiceQuestion();
second.setText("Which modifies the object on which it operates in some way?");
second.addChoice("Accessor", false);
second.addChoice("Mutator", true);
second.addChoice("Method", false);
second.addChoice("Variable", false);

NumericQuestion third = new NumericQuestion();
third.setText("What is the numeric value for PI?");
third.setAnswer(3.1416);


presentQuestion(first);
presentQuestion(second);
//presentQuestion(third);
}

/**
Presents a question to the user and checks the response.
@param q the question
*/
public static void presentQuestion(Question q)
{
q.display();
System.out.print("Your answer: ");
Scanner in = new Scanner(System.in);
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}


}

Old Pedant
11-14-2010, 05:14 AM
NO NO NO NO!!!

That code *IS* JAVA code.

It is *NOT* JavaSCRIPT code!!!

DO NOT CONFUSE THE TWO LANGUAGES!!!!

This is the WRONG FORUM for that question.

************

NO MORE Java questions in this forum, PLEASE!

Anyway, you have not even *STARTED* you program correctly!


For class NumericQuestion Add setAnswer, a method that takes a double as input, converts it to a string and
calls setAnswer in the superclass Question

Where is your class Question? You have to have it first before you can make it the superclass of the class NumericQuestion !!!

http://www.java-tips.org/java-se-tips/java.lang/what-is-a-java-superclass.html

seabass
11-14-2010, 05:54 AM
I apologize for that..thank you that helped.. i now have..but my question 3 is wrong and i cant figure it out.. here is my new codes..thank you for the help so far.


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



/**
* 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;
}
/**
* Sets the users answer for this question.
* @param getAnswer the answer
*/
public String getAnswer(String response){
response = getAnswer;
getAnswer = answer;

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

}

/**
* Displays this question.
*/

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


}
public class NumericQuestion extends Question{
private Double answer;
/**
* Sets the answer for this question.
* @param correctResponse the answer
*/
public void setAnswer(Double 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(Double response) {
return response.equals(answer);

}

}

public class ChoiceQuestion extends Question {
private ArrayList<String> choices;

/**
* Constructs a choice question with no choices.
*/
public ChoiceQuestion() {
choices = new ArrayList<String>();
}

/**
* Adds an answer choice to this question.
* @param choice the choice to add
* @param correct true if this is the correct choice, false otherwise
*/
public void addChoice(String choice, boolean correct) {
choices.add(choice);
if (correct) {
// Convert choices.size() to string
String choiceString = "" + choices.size();
setAnswer(choiceString);
}
}

public void display() {
// Display the question text
super.display();
// Display the answer choices
for (int i = 0; i < choices.size(); i++) {
int choiceNumber = i + 1;
System.out.println(choiceNumber + ": " + choices.get(i));
}


}
}

public class Quiz {
public static void main(String[] args)
{
Question first = new Question();
first.setText("What inherits data and behavior from a superclass?");
first.setAnswer("subclass");

ChoiceQuestion second = new ChoiceQuestion();
second.setText("Which modifies the object on which it operates in some way?");
second.addChoice("Accessor", false);
second.addChoice("Mutator", true);
second.addChoice("Method", false);
second.addChoice("Variable", false);

NumericQuestion third = new NumericQuestion();
third.setText("What is the numeric value for PI?");
third.setAnswer(3.1416);


presentQuestion(first);
presentQuestion(second);
presentQuestion(third);
}

/**
Presents a question to the user and checks the response.
@param q the question
*/
public static void presentQuestion(Question q)
{
q.display();
System.out.print("Your answer: ");
Scanner in = new Scanner(System.in);
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}


}

Old Pedant
11-14-2010, 06:14 AM
You are not following instructions!


For class NumericQuestion Add setAnswer, a method that takes a double as input, converts it to a string and
calls setAnswer in the superclass Question to store the answer.

WHERE does it say to have a field named answer in the NumericQuestion class????

In fact, it *EXPLICITLY* tells you *NOT* to do that!


o Add checkAnswer, a method that takes an answer in string form, converts it to a
double, gets the correct answer in string form from Question, converts it to a
double, and returns true if they are within 0.01 of each other and false otherwise.

You are doing *NONE* of that. Not one single part of it, other than you have the method named checkAnswer. But it doesn't take the answer in string form, it doesn't convert it to double, it doesn't... Well, it just doesn't.

Look here: http://www.codingforums.com/rules.htm

Read rule 1.5.

If you make an effort to do the work, you can expect help here. But we can't write the code for you, and you haven't even started following instructions yet. Get at least *SOME* of the instructions followed. Okay, I guess at least now you have the class and superclass. But I don't feel that's enough.

Anyway, I'm off to bed.

Try to at least START doing what the instructions say. And then post in the JAVA forum. I'm not going to answer any more JAVA questions in this forum.

Maybe you'll get lucky and a moderator will move this thread to the Java forum. But if not, start posting questions there.

Philip M
11-14-2010, 08:57 AM
When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags.
This means use the octothorpe or # button on the toolbar which will insert the tags. You can (and should) edit your previous post.

Example:-


alert ("This is some JavaScript code!");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum