Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Is it possible to put several objects into one file?

Thread view: 
John - 14 Apr 2007 02:53 GMT
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.
Lew - 14 Apr 2007 03:06 GMT
> 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.

Signature

Lew

John - 14 Apr 2007 03:35 GMT
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?

>> 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
[quoted text clipped - 15 lines]
> representation and back.  This is also a form of serialization, but not
> Java serialization.
Lew - 14 Apr 2007 04:58 GMT
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.

Signature

Lew

John - 14 Apr 2007 13:32 GMT
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

> Please do not top-post.
>
[quoted text clipped - 37 lines]
> The Javadocs will help you with such standard things as I/O and
> Properties.
Andrew Thompson - 14 Apr 2007 14:24 GMT
>I don't think ...

The same thing occurs to me, whenever I see a
top-posted reply to the only responder, who requested..

(Lew)
>> Please do not top-post.

For more infomration on top-posting, see..
<http://www.physci.org/codes/javafaq.html#toppost>

Signature

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

Lew - 14 Apr 2007 15:49 GMT
Please don't top-post.

John wrote:
> 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.

Signature

Lew

John - 14 Apr 2007 16:41 GMT
> Please don't top-post.
>
[quoted text clipped - 16 lines]
>
> 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
Andrew Thompson - 14 Apr 2007 17:34 GMT
..
>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.

Signature

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

John - 14 Apr 2007 17:19 GMT
> Please don't top-post.
>
[quoted text clipped - 9 lines]
> 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.

> 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.
Lew - 14 Apr 2007 20:24 GMT
> If for a question the object type you use is String, how can you organize
> the hierarchy of the all those types questions?
[quoted text clipped - 3 lines]
>
> 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.

Signature

Lew

John - 14 Apr 2007 22:53 GMT
>> If for a question the object type you use is String, how can you organize
>> the hierarchy of the all those types questions?
[quoted text clipped - 22 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.