Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Inheritance question!

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Inheritance question!

    Hey there! I have a question involving the code below. The question is:

    Which methods does the ChoiceQuestion class inherit from its superclass? Which methods does it override? Which methods does it add?


    I'm having trouble deciphering each part of the code from the other... Like, I don't know what we're referring to as the "superclass", so I really can't know what we're overwriting if we can't see the superclass... Anyone able to help me out?

    Part 1 of code:

    (link removed)

    Part 2 of code:

    (link removed)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Inheritance question!

    I've removed your links, as they look pretty spammy to me. Can't you just post the code as an SSCCE, using the highlight tags?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    PhHein (September 23rd, 2013)

  4. #3
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance question!

     
     
     
    1import java.util.Scanner;
    2
    3/**
    4 This program shows a simple quiz with two choice questions.
    5*/
    6 public class QuestionDemo2
    10 ChoiceQuestion first = new ChoiceQuestion();
    11 first.setText("What was the original name of the Java language?");
    12 first.addChoice("*7", false);
    13 first.addChoice("Duke", false);
    14 first.addChoice("Oak", true);
    15 first.addChoice("Gosling", false);
    16
    17 ChoiceQuestion second = new ChoiceQuestion();
    18 second.setText("In which country was the inventor of Java born?");
    19 second.addChoice("Australia", false);
    20 second.addChoice("Canada", true);
    21 second.addChoice("Denmark", false);
    22 second.addChoice("United States", false);
    23
    24 presentQuestion(first);
    25 presentQuestion(second);
    26 }
    27
    28 /**
    29 Presents a question to the user and checks the response.
    30 @param q the question
    31 */
    32 public static void presentQuestion(ChoiceQuestion q)
    33 {
    34 q.display();
    35 System.out.print("Your answer: ");
    36 Scanner in = new Scanner(System.in);
    37 String response = in.nextLine();
    38 System.out.println(q.checkAnswer(response));
    }
     
    }


    
    import java.util.ArrayList; 
     /**
    A question with multiple choices.
     */
     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)); }
    } }

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Inheritance question!

    Yikes, is that really how you format your code? I'd recommend using proper indentation, if only for your own sanity.

    I also don't see a Question class, but I could be missing it in your unformatted code.

    But what's your question now? What part of your homework are you stuck on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance question!

    It's not my code... it's directly from a textbook.

Similar Threads

  1. Inheritance
    By vasanthjayaraman in forum Java Theory & Questions
    Replies: 5
    Last Post: July 10th, 2013, 12:28 PM
  2. Help with inheritance
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2012, 12:07 AM
  3. Inheritance question
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: February 24th, 2012, 07:19 AM
  4. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  5. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM

Tags for this Thread