Is it possible to put several objects into one file?

J

John

I am designing a generic survey/test taking system. I want the aplication to
be designed this way so that the adminitrator can group several questions
into one survey question file stored on the hard drive, which can latter be
retrieved by other user who wants to do the survey.

My question is: is it possible to write several questions into one file?

I make a question class, which is abstract. Multiple choice is a subclass of
question. True/false is subclass of multiple choice. Later, I may expand all
the classes.

I checked java doc, and it seems related with object input output stream, or
serialization?

I am a new bee. Any suggestion is welcome.
 
L

Lew

John said:
I am designing a generic survey/test taking system. I want the aplication to
be designed this way so that the adminitrator can group several questions
into one survey question file stored on the hard drive, which can latter be
retrieved by other user who wants to do the survey.

My question is: is it possible to write several questions into one file?
Yes.

I make a question class, which is abstract. Multiple choice is a subclass of
question. True/false is subclass of multiple choice. Later, I may expand all
the classes.
I checked java doc, and it seems related with object input output stream, or
serialization?

You might use Java serialization, but there are many other ways to do it. For
example, JAXB will let you translate objects to an XML text representation and
back. This is also a form of serialization, but not Java serialization.
 
J

John

Are there delimiters in the stored file?

How does Java know from where to where an objec is in the file?

When JAXB will let you translate objects to an XML text representation, what
part of the object is translated?

For an object of True/Flase question class, is just the question translated?
what does the JAXB do with the method?
 
L

Lew

Please do not top-post.
Are there delimiters in the stored file?

If you mean XML, yes. They are called "tags".
How does Java know from where to where an objec is in the file?

I suggest that you read the documentation for JAXB and for Java serialization.

I do not know the details of how Java serialization works in "the stored
file", because I've never needed to know.

If you are looking to implement a file storage based on storing entire objects
it might get very complicated. You could just store the questions and the
answers as text, since they are strings to begin with. So instead of
serialization, which is difficult, you have reading and writing Strings, which
is easy.
When JAXB will let you translate objects to an XML text representation, what
part of the object is translated?

As much as you want, but it takes some thought to plan it properly. You may
need to learn XML Schema to use it right.
For an object of True/Flase question class, is just the question translated?
what does the JAXB do with the method?

It does only what you tell it to do.

It's a sort of complicated way to do what a PrintWriter println() and
BufferedReader readLine() will probably be able to do for you just fine.

You also might consider doing clever things with properties files. They are
set up as key=value pairs. You could interpret that as question=answer and
take advantage of what Java can do with Properties objects.

The Javadocs will help you with such standard things as I/O and Properties.
 
J

John

I don't think that to seperate the text for the question from other parts of
a question object is an easy job.

I am not sure whether I am correct.

But I think that serilization can help me group all the question objects in
a survey into an array or arraylist, which can then be written into a file.

Then when the user wants to take the survey, the file can be conversely read
into an array in memory with its elements being references to the question
objects.

Each element can then be displayed on the screen to expect the user to input
his answer. Of course, different kinds of questions expects different
answer, which can be realized through polymorphism.


But if I only store the question text part of the question object, I might
lose the strucre of the question object. And when the text is read back to
memory, how can it be restored to what its coresponding object used to be?

John
 
L

Lew

Please don't top-post.
I don't think that to seperate [sic] the text for the question from other parts of
a question object is an easy job.

It can be.

It all depends on how you wrote the object. Personally, for a question the
object type I use is String. Same for the answer. Based on the answers, I
would construct on object that represented an entity in my object model, not
the question and answer unless that were what my program managed (CBT, surveys).

In that case "to separate the text for the question from other parts of a
question object is an easy job", as easy as

public String getQuestion();

I would in my own code do as I suggested to you in my last post.
 
J

John

Lew said:
Please don't top-post.
I don't think that to seperate [sic] the text for the question from other
parts of a question object is an easy job.

It can be.

It all depends on how you wrote the object. Personally, for a question
the object type I use is String. Same for the answer. Based on the
answers, I would construct on object that represented an entity in my
object model, not the question and answer unless that were what my program
managed (CBT, surveys).

In that case "to separate the text for the question from other parts of a
question object is an easy job", as easy as

public String getQuestion();

I would in my own code do as I suggested to you in my last post.


Could you please offer me some sample code? Here or my mailbox.
I would like to see how you construct a question class/object with both
question and answer embedded.

Thanks!

John
 
J

John

Lew said:
Please don't top-post.
I don't think that to seperate [sic] the text for the question from other
parts of a question object is an easy job.

It can be.

It all depends on how you wrote the object. Personally, for a question
the object type I use is String. Same for the answer. Based on the
answers, I would construct on object that represented an entity in my
object model, not the question and answer unless that were what my program
managed (CBT, surveys).


If for a question the object type you use is String, how can you organize
the hierarchy of the all those types questions?

My design is that there should be a abstract question class, with subclass
such as mutiple choice, matching, short answers, true/false etc.

True/false can be the subclass of multiple choice.
 
A

Andrew Thompson

John wrote:
..
Could you please offer me some sample code?

Here, have some sample code..

<sscce>
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.io.*;

public class AnsweredQuestion {

public String question;
public String answer;

public AnsweredQuestion() {
}

public AnsweredQuestion(
String question,
String answer) {

this.question = question;
this.answer = answer;
}

public void setQuestion(String question) {
this.question = question;
}

public String getQuestion() {
return question;
}

public void setAnswer(String answer) {
this.answer = answer;
}

public String getAnswer() {
return answer;
}

public String toString() {
return "Q. " + question + " A. " + answer;
}

public static void main(String[] args) throws FileNotFoundException {
String filename = "qanda.xml";

AnsweredQuestion[] qAndAStore = new AnsweredQuestion[3];

qAndAStore[0] = new AnsweredQuestion(
"Is an opal animal, mineral, or vegetable?",
"Mineral.");
qAndAStore[1] = new AnsweredQuestion(
"What is the meaning of life?",
"42.");
qAndAStore[2] = new AnsweredQuestion(
"What is your favorite color?",
"Green, ..no, BLUE. AAaaaargh!");

XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(filename)));

encoder.writeObject( qAndAStore );
encoder.close();

XMLDecoder decoder = new XMLDecoder(
new BufferedInputStream(
new FileInputStream(filename)));
AnsweredQuestion[] qAndARetrieve =
(AnsweredQuestion[])decoder.readObject();
encoder.close();

for(int ii=0; ii<qAndARetrieve.length; ii++) {
System.out.println( qAndARetrieve[ii] );
}
}
}
</sscce>

This probably does not do what you need, but
further questions might require some more effort
on your part, or a consultant or help-desk.

Note that c.l.j.programmer is neither of the last two.

Note that I have not been following the thread closely
enough to ensure this is sample code according to
what Lew was saying. I just churned it out because
I'm *extremely* bored.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
L

Lew

John said:
If for a question the object type you use is String, how can you organize
the hierarchy of the all those types questions?

My design is that there should be a abstract question class, with subclass
such as mutiple choice, matching, short answers, true/false etc.

True/false can be the subclass of multiple choice.

That's fine. There are a zillion ways to solve the problem.

I see that what you mean by a question is an organization of the information.
I'd probably separate the issue of whether something is implemented via
checkboxes or a multi-select from the content of the information, similarly to
how Swing and JSF do it.

There are structures to hold the information. One example is

Map< String, List<String>>

that can hold the question text and the list of answers (as for multi-select),
initially length 0 for unanswered questions.

The presentation of the text would be handled by separate layout components.

But your way seems OK, too.
 
J

John

Lew said:
That's fine. There are a zillion ways to solve the problem.

I see that what you mean by a question is an organization of the
information. I'd probably separate the issue of whether something is
implemented via checkboxes or a multi-select from the content of the
information, similarly to how Swing and JSF do it.

There are structures to hold the information. One example is

Map< String, List<String>>

that can hold the question text and the list of answers (as for
multi-select), initially length 0 for unanswered questions.

The presentation of the text would be handled by separate layout
components.

But your way seems OK, too.

I think that it is possible to have another field and corresponding method
to log suryvery result.

What do you think?

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top