Google Answers Logo
View Question
 
Q: Java generic class implementation question ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Java generic class implementation question
Category: Computers > Programming
Asked by: gooseman-ga
List Price: $10.00
Posted: 16 Oct 2002 18:14 PDT
Expires: 15 Nov 2002 17:14 PST
Question ID: 77521
I have a GUI where the user can select three options to send/generate
data to a simulation class.

The first option is by specifiying the name and location of a
configuration file.

The second and third options are by selecting a coded routine that
generates configuration data that is formatted in an identical manner
to the file (i.e:

   simulation0.variable0 = 3
   simulation0.variable1 = 10
   simulation0.variable2 = 5 )

So the simulation class must know which option was selected. So the
GUI class can instantiate the simulation class and pass the option
like so:

Simulation simulation = new Simulation(optionNumber)

The idea is that the simulation class can then instantiate whichever
configuration class the user selected for use. But I'm not sure what
the best way to do this is.

Problems:

The file name (if selected) needs to be passed to the simulation class
at some point - how can I do this without using polymorism, as the
simulation class is very long and complex.

The simulation class needs to deal with any configuration class
generically. For example, there is a method in each configuration
class, called getNext() that will output to the simulationClass the
next config set. But I don't know which configuration method (and
therefore configuration class) the user selected.

I was thinking about doing:

if (option 1 == true)
  fileInput input = new fileInput(fileName)
else if (option 2 == true)
  configB input = new configB(fileName)
else if (option 3 == true)
  configC input = new configC(fileName)

and then i can do 

  input.getNext();

But this obviously won't work, as you'd need to declare the "input"
name at the top of the simulation class (to use throughout), and you
can't do that with three different constructors, using the same name.

Maybe I have to pass the configuration classes themselves to the
simulation class? If so, how?

I'd like some examples of how this works, but please ask me to clarify
as I'm not sure if I've explained my problem well enough.

Thanks!

Request for Question Clarification by secret901-ga on 16 Oct 2002 18:30 PDT
Hi gooseman,
Have you tried writing an interface for the config classes?  It
appears that ConfigB, ConfigC, and FileInput all have the method
getNext in them.  I am not sure what FileInput is.  Is it the same
generically as the other two Config classes?
secret901

Request for Question Clarification by secret901-ga on 16 Oct 2002 18:46 PDT
Please verify this so that I'm sure that I understand you correctly:
FileInput is a class that you made that handles the case when the user
chooses a configuration file.
ConfigB is a class that you made that has a coded routine, equivalent
of a configuration file.
ConfigC is a different configuration class similar to B.
All three classes have a method called getNext() that returns the same
data type.
You want to be able to have one variable, called input, that can be a
FileInput, ConfigB, or ConfigC depending on what the user chose.
You will call getNext() on input, no matter which type it is.
Is this correct?

Clarification of Question by gooseman-ga on 17 Oct 2002 03:24 PDT
Please verify this so that I'm sure that I understand you correctly: 
FileInput is a class that you made that handles the case when the user
chooses a configuration file.
ConfigB is a class that you made that has a coded routine, equivalent
of a configuration file.
ConfigC is a different configuration class similar to B. 
All three classes have a method called getNext() that returns the same
data type.

Yep!

You want to be able to have one variable, called input, that can be a
FileInput, ConfigB, or ConfigC depending on what the user chose.

Yes, if that's appropriate.

You will call getNext() on input, no matter which type it is. 
Is this correct?

Yes.

But also, I have two classes, one a GUI class, where the user
determines which input method he wants, and one the simulation class,
which executes the option. But in the fileInput case, the file name
may need to be passed as well as the option, unless there is a better
way. That needs to be dealt with too.

Thanks!
Answer  
Subject: Re: Java generic class implementation question
Answered By: secret901-ga on 17 Oct 2002 06:53 PDT
Rated:5 out of 5 stars
 
Hello gooseman,
I think that the best solution is to make an interface which the
configuration classes can implement.  This interface will declare the
methods that its children classes can implement.  You can make it like
so:

public interface Config {
	public ConfigSet getNext();
	//declare other methods that all the config classes have in common
here this way
}

In each of the children classes, you can change their declaration so
that they implement Config, for example:

public class ConfigB implements Config {
	//constructor and other methods here.
	public ConfigSet getNext() {
		//your code for getNext() here
	}
}
Do this to the other two classes (InputFile and ConfigC).

Now, in your SimulationClass, you can do this:

public Config input = null; 
//if the previous line is inside a method, we need to initialize the
value of input so that we
//can call methods on it later. 
if(option1)
	input = new FileInput(fileName);
else if(option2)
	input = new ConfigB(fileName);
else if(option3)
	input = new ConfigC(fileName);
input.getNext();

It appears that ConfigB and ConfigC do not need a fileName to be
passed onto them, so I think that it's unnecessary to make their
constructors require fileName parameters.  Your GUI should also pass
in the fileName to the simulation class (it can be null if there's no
filename needed).

You’ll notice that input is a Config, thus you can only call methods
that are declared in Config.  But, what if you know that input is a
FileInput, and you want to call a method that is unique to FileInput
on input?  You can accomplish this by first casting input into a
FileInput, then call the method on it:

(FileInput)(input).myUniqueMethod();

Thank you for using Google Answers.  I hope this answered your
question.  If you need clarification, please request for it before
rating this answer.
secret901-ga

Search strategy:
Personal knowledge, plus Sun’s Java website:
http://java.sun.com

Clarification of Answer by secret901-ga on 17 Oct 2002 07:21 PDT
Alternatively, you can instantialize input inside the GUI class,
depending on what the user chose.  Then all you have to do is to pass
that instance into the SimulationClass.  Of course, you have to
declare that variable a Config, so that it can handle the different
implementations of Config.  Doing this will eliminate the need to pass
in the SimulationClass the fileName.
If any of this is unclear, please let me know.

Request for Answer Clarification by gooseman-ga on 17 Oct 2002 12:40 PDT
Hi. Thanks for the answer. I'm interested in the second
implementation. Could you go into a bit more detail, examples etc.
Also, what do you mean by ConfigSet?

Thanks again

Request for Answer Clarification by gooseman-ga on 17 Oct 2002 13:41 PDT
OK. I've gone over a bit more, so I can maybe ask a clearer question
regarding configSet. I'm guessing that contains the data

simulation0.variable0 = 4
simulation0.variable1 = 12 etc...

How do I define it, and where?

I hope I've understood that right!

Thanks again

Clarification of Answer by secret901-ga on 17 Oct 2002 13:45 PDT
Hi gooseman,
By "second implementation," I assume that you mean the implementation
I provided in my clarification.  But first, I shall explain what I
mean by "ConfigSet."
You indicated that in your question that all three classes have a
method called "getNext()" that returns the same data type.  You said
that they return the "next config set."  I assume that you have a
class defined for this data type.  Thus, the return type of the
getNext() function should be whatever that class is.

In your GUI class, you can have a variable, called input:

Config input = null;

Now, when the user selects one of the options, you can instantialize
input depending on what the user selected:

if(option1) 
 input = new FileInput(fileName); 
else if(option2) 
 input = new ConfigB(); 
else if(option3) 
 input = new ConfigC(); 

After this is done, you can pass input into the SimulationClass, which
accepts it as a Config.  Since it is a Config, you can call the method
getNext() on input in the SimulationClass no matter which class it is
an instance of.

If you still need clarifications, please let me know.

Request for Answer Clarification by gooseman-ga on 17 Oct 2002 14:04 PDT
You've been really good, and I promise this is the last question! I
don't have a class for the data sent. I initally used and array,
although I'm trying to do things properly now.

Does configSet act like, say a double or an int, that happens to
define how the data is contained. If that is the case, what is the
terminology for this, so I can look it up, and if it's simple can you
give a very brief example of how it would hold the example data I gave
you (if it's too long to explain, don't worry - I'm really happy with
your help!)

Clarification of Answer by secret901-ga on 17 Oct 2002 15:07 PDT
It is perfectly all right to use an array for the return type of the
method getNext().
For example, if your config set contains an array of integers, you can
declare the method getNext() in the interface like this:

public int[] getNext();

I assumed that you'd put the config set into a class which
encapsulates all the configuration, and all the methods associated
with it.  But if all the methods dealing with the config set is put
somewhere else, that's all right as well.
There is no data type called ConfigSet in the Java API.  Sorry for the
confusion.
Feel free to ask for clarification if my explanation is not clear
enough,
secret901-ga

Request for Answer Clarification by gooseman-ga on 18 Oct 2002 03:36 PDT
Hi,

I've got the input working at the GUI fine, and the "implements"
approach also works. I've still got one problems with defining
"configData" (that's what we called configSet before). I get a
"Missing method body, or declare as abstract" error. I just want
configData to accept any number of strings, each one in a separate
index in a one dimential array.

public interface inputConfig
{
  public configData getNext();
}

class configData
{ 
  // PROBLEM IS HERE 
  public String[] configData();
  {
    
  }
}

class fileInputConfig implements inputConfig
{
  configData data = new configData();
  fileInputConfig(String fileName)
  {
    
  }
  public configData getNext()
  {
    // Populate data with Strings
    return data;
  }
}

class routineInputConfig implements inputConfig
{
  configData data = new configData();
  public configData getNext()
  {
    // Populate data with Strings
    return data;
  }
}

class adaptiveInputConfig implements inputConfig
{
  configData data = new configData();
  public configData getNext()
  {
    // Populate data with Strings
    return data;
  }
}

============GUI==========
  void mainExecuteButton_actionPerformed(ActionEvent e)
  {
    if(userOption == 0)
    {
      input = new fileInputConfig(fileName);
    }
    else if(userOption  == 1)
    {
      input = new routineInputConfig();
    }
    else if(userOption == 2)
    {
      input = new adaptiveInputConfig();
    }
    simulationControl simulation = new simulationControl(input);
  }

Request for Answer Clarification by gooseman-ga on 18 Oct 2002 06:00 PDT
I've worked it out, so no problem:

class configData
{
  public String[] configData()
  {
    String[] a = {"10","10"};
    return a;
  }
}

Just to test it.

If you have any comments, just post them here! Thanks again!

Clarification of Answer by secret901-ga on 18 Oct 2002 10:24 PDT
The problem with your code, which you fixed, is the semicolon
immediately after you declared the method.  When you do so, the method
is considered an abstract method (not implemented), and a class
containing abstract methods must be declared abstract.
By the way, you might want to consider changing your naming
conventions next time.  Your way of naming method names is correct,
but class name and interface name should have the first letter in each
word capitalized (including the first word).  See
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html for
more info.
Regards,
secret901-ga
gooseman-ga rated this answer:5 out of 5 stars
Very helpful!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy