I am working on a parsing a file .
This is my problem.
There are classes and subclasses. Classes/Subclasses are defined by attributes. The sub classes will inherit the attributes of its super class.

For example:
Employee(attrbts: name, salary)
manager(attrbts: responsibilities)
clerks(attrbts: department)

Emp is a class with manager nd clerk as sub cls and these sub class will inherit the attrbts .

Now I have to parse the file and store the values such that I should be able to add a super class with its subclasses or only a subclass into a new project. My problem worsens wen i decide to add a super class after i add a subclass since the program doesnt not make the relation btw the classes.
i hope i am not very confusing and any help will be really really appreciated.

P.s: i have used linked lists presently. Is there a better way??


11 Years
Discussion Span
Last Post by chrisbliss18

I don't understand what your question is.

Do you need help with creating an object structure that can handle data of that sort?
Do you need help with how to read the file?


I think there are a lot of issues here...

First, look into how to extend objects. This will allow you to add specialized behavior to your parent objects.

Next, since the subclasses are defined by attributes you might need to use Introspection. However, my guess is that your teacher wouldn't have you using introspection. Ask him if that is the right path to go down.




To make things clear let me show you the output file of the program.

defclass Advertisement
is-a User)
(role abstract)
(attribute purchaser
(create-accessor read-write))
(attribute salesperson
(create-accessor read-write))
(attribute name_
(type STRING)
(create-accessor read-write)))

defclass Personals_Ad
(is-a Advertisement)
(role concrete)
(attribute text
(type STRING)
(create-accessor read-write)))

defclass Standard_Ad
(is-a Advertisement)
(role concrete)
(attribute image
(type STRING)
(create-accessor read-write)))
I need to parse this file. Here advertisements (since it is a user) is the main class with personal ad and standard ad as subclass. I parse this file such that i create a list of all classes and their attributes. If it is subclass(say personal ad) then it should inherit the attributes of the super class(purchaser, salesperson,name). Also there should be a way in which i am able to make the program undersatnd who their super /sub classes are.



Actualy after re-reading what you're trying to do...This is most likely the second best school problem i've ever seen posted.

So, you are given a classes form in text form and then you have to create the class .java files from the text. Is this correct?

So your output might look like the following:

pulbic abstract class Advertisement extends User
  private User purchaser = null;
  private User salesPerson = null;
  private String name_ = null;

  public void setPurchaser(User purchaser)
    this.purchaser = purchaser;

  public User getPurchaser()
    return purchaser;

  public void setSalesPerson(User salesPerson)
    this.salesPerson = salesPerson;

  public User getSalesPerson()
    return salesPerson;

  public void setName(User name)
    name_ = ;

  public Name getName()
    return name_;




Good call hooknc. I think that's what he's going for. Can you confirm this Chipsncoke?

I have to say that the file definition is extemely poorly structured. You can tell your professor I said that.

import java.io.*;
import java.util.*;
import java.lang.*;

public class Sample {
    public static LinkedList name = null; 
    public static LinkedList type = null;
    public static LinkedList role = null;
    public static void  main (String args[]){
       int n = 0;
       int n1 = 0;
       String Filename = "chipi2.txt";
       StringTokenizer st;
       name = new LinkedList();
       type = new LinkedList();
       role = new LinkedList();
       BufferedReader bin = null;
       int NumofClass = 0;
       String line1 = null;
       String V1 = null;
       String V2 = null;
       String clname = null;
        String cltype = null;
         String clrole = null;
       boolean Status = false;
       String filename = Filename;
       try {
            bin= new BufferedReader(new FileReader(filename));
//just testing the output
            PrintWriter outFile = new PrintWriter(new FileWrite("totest.alb"));
            while((line1 = bin.readLine())!= null){
            st = new StringTokenizer(line1);
            if (line1.startsWith("defclass")){
               V1= st.nextToken();
               clname = st.nextToken();
               cltype = st.nextToken();
                V1 = st.nextToken();
               clrole = st.nextToken();
        catch (IOException e) {


ok. Now what i have written will store the file in a linked list.
I will display all the names of classes in order present in the list to the user.
what i was thinking of doing further is, when the user enters the class for its details......(i might have missed out this part in my question previously...problem is I my self am trying to figure out the question)
I will check the type for the class entered. If it is user then i will display the type n role otherwise i will put it in a loop to get all the claases until the type ==user.

I havent come until that part and i hope thats solves the inheriting problem.

I have to somehow fix the attributes......thinking of linked list again.

The approach of your code is different. I am not sure if thats what my prof wants.

But this is what i want to do........Any suggestions for improvement.



I don't like giving answers to programming problems since it defeats the purpose of learning. I think it would help to know exactly what your program is supposed to do. If you could post the actual instructions, project goals, it would help me answer your questions better.

What it sounds like is you need to put all this data in some type of structure. Then when a user requests a class, the program will output all the details of the class. If that is the case, I think I have a good file structure in mind. Using linked lists is simply not enough.


My only advice is to use OO to your advantage.

Think about creating a Class object (different then the java.lang.Class object) that has some behavior... like knowing who its parent class is... a list of interfaces it is implementing and a list of all its attributes. Attributes 'might' be a class by itself. Or it could be an inner class.

Best of luck,



I don't like giving answers to programming problems since it defeats the purpose of learning. I think it would help to know exactly what your program is supposed to do. If you could post the actual instructions, project goals, it would help me answer your questions better.

I don't like doing that either, but this is such a good problem. I can't help myself.




I meant to indicate that I know that Chipsncoke isn't looking for us to give him his whole program, but it would help us give better answers if he posted the actual assignment details.


Thanks for replying. I am not very good at programming and so the first thought that occured to me was using linked list. Since the question seems to be confusing i am attaching the overview of the problem.


An ontology is a controled vocabulary that describes objects and the relation between them in a formal way. The fundamental role of an ontology is to support knowledge sharing and reuse.
Classes are used to define a knowledge domain. These classes have attributes associated with it. Inheritance holds good and the sub-classes defined will inherit all the attributes of its parent class.

Given to you are some files .These files are output files of a popular ontology editor. You have to write a program to read the file . The program should read the classes and their associated attributes.
The complete file may contain upto 100 classes. List all the available classes to the user.The user will select the class he wishes to see. The program should display all the information pertaining to that class.
(sub-classes and attributes).

We have already discussed the file format.An object is a parent class if the 'type' of the class is 'USER'.Otherwise the object will be a sub-class of the class mentioned in 'type'.

You have to create a library of all the objects used. Two points you will have to consider are relationship between parent and a child class and inheritance of the attributes of the parent.

You can run the program on command line or using an improved user interface.



Think about creating a Class object (different then the java.lang.Class object) that has some behavior... like knowing who its parent class is... a list of interfaces it is implementing and a list of all its attributes. Attributes 'might' be a class by itself. Or it could be an inner class.

This should still work for you.

HOWEVER, I do not agree with his statement that a when a parent class is displayed, that you should see all its children classes. Why whould a parent class care about its children. I don't get it.

However, it would be to your advantage to try and make a Class called Clazz or something like that that represents a Object Oriented class.

So it would have a parent attribute, a get parameters attribute (most likely a linked list) and then if you really have to a children attribute. The children attribute should most likely be a list too.




By definition a parent can't know what it's children are.
There is no way to determine all children of a class that may exist. You can look through all existent classes you know about and determine their superclasses all the way up to the root of the class hierarchy and remember which have the class you're interested in as a parent but that's an expensive operation if you have a large class hierarchy.
It would mean reading every class into memory and determining not only its direct parent bur the complete inheritence tree of that class all the way up to the root.
If you have thousands of classes in a tree that's many levels deep that takes a lot of time.


I think you should make a nice set of objects. Here's some data structures that I think would work well:

class MyAttribute
	MyAttribute(name, value)



class MyClass

If you use a structure like this, you can easily maintain the relationships between the classes and attributes this way. This structure uses linked lists to maintain the relationships. I'd also recommend using vectors to groups of similar objects (the children inside a class).

jwenting, you're right that in the OO model, parents don't explicitly know what children they have. It would seem that the instructor is not interested in that. I believe the instructor wants to see if they can store such hierarchal data in a logical structure and pull data back out of it.

Chipsncoke, does this look like a good approach to your problem?

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.