Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Needs a bit of help with javascript program.

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


    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    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-tip...uperclass.html
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    May 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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));
    }


    }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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:-
    Code:
    alert ("This is some JavaScript code!");


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •