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 3 of 3

Thread: Creating an open-ended class that can be extended:

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating an open-ended class that can be extended:

    So my assignment is to create a Quiz main driver class, and an abstract question class. The purpose is to create a question class so that a user may come up with a type of question and incorporate it in the main driver class. We already supply some of our own question class types that "extend" question such as true/false and multiple choice to name a few. The driver has to take in a text file that is filled with questions like the format below. The first letters tells what kind of question it will be. The next line is a string of the question, and the next thing is the answer.



    N
    How many parameters does a copy constructor accept?
    1
    S
    What standard Java class do all classes inherit from?
    Object
    T
    To compare two char values, use the equals() method.
    false
    M
    Which types are good for storing numbers with decimal points?
    - String
    + double
    - boolean
    + float
    - int
    m
    What is "computer".substring(3, 5)?
    - mpute
    + pu
    - pute


    The single characters identify what type of question it is going to be. For example the letter T stands for a true/false question. The massive roadblock i cant get by is how to incorporate the one letter identifier if the user can only A.) use the driver program and B.) extend question to make their own. My code for the question class is below. I cannot use if-else statements, or even mention question classes I am building myself in this class. It has to be completely separate and cannot be edited, yet somehow any user should be able to come along and say they want to create an "essay question" class and add its own identifier and put it in a text file and have the driver run to read in the question and create that question object. Each question has it's own method isCorrect() to identify when the user answers a question, if it is right or not. I also added a sample question type to kind of show how a user would incorporate their own.

    To my main point, does anyone have an idea on how I can incorporate identifiers to create the certain type of question that is needed?

    Thanks
    David


    package Questions;
     
    public abstract class Question {
    	private String question;
    	private Object answer;
     
    	public String getQuestion() {
    		return question;
    	}
     
    	public Object getAnswer() {
    		return answer;
    	}
     
    	public void setQuestion(String s) {
    		this.question = s;
    	}
     
    	public abstract boolean isCorrect();
     
     
     
    }

    package Questions;
     
    public class TrueFalse extends Question {
    	boolean answer;
     
    	public TrueFalse(String question,Boolean answer) {
    		this.setQuestion(question);
    		this.answer = answer;
    	}
     
    	public boolean isCorrect() {
    		return ();
    	}
     
     
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Creating an open-ended class that can be extended:

    Welcome to Java Programming Forums. Read the Forums Rules.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an open-ended class that can be extended:

    Please don't get the wrong idea. I am not attempting to get someone to do my homework, I just need a suggestion for one part which is the identifier. The only thing I can think of is something that works similar to a static variable. Question would be the class that all other classes extend, and upon extending Question those classes inherit Question's methods. But also, if there was a way for question to have a variable, and when a class extends question, they too inherit that variable that needs to be declared, which would be the identifying letter.
    David

Similar Threads

  1. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  2. [SOLVED] Creating A Class
    By cb5950 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 31st, 2011, 07:27 AM
  3. [SOLVED] Class Creating Help
    By pitchblack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 10:25 PM
  4. Help with creating a class
    By cdawg_2010 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 1st, 2010, 07:04 AM
  5. Creating a class file
    By ipatch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2009, 06:19 PM

Tags for this Thread