Win a copy of Head First Android this week in the Android forum!

Red Smith

Ranch Hand
+ Follow
since Aug 05, 2007
Red likes ...
Netscape Opera Ubuntu
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
8
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Red Smith

Brian Bykenhaal wrote:Okay I have I have changed my project so that each class other then main in under my game board package.
I would like to make my main in outside the game package where I have all my classes should this be any issue?



No, you can have the class that has the main() method in a package different from code that is is going to use. But you do have to specify to the compiler or the JVM where to start looking for your package via the CLASSPATH env variable or -classpath command line option. But I guess Netbeans has an options dialog that is used to specify that info.



I have not been able get past these errors. I have 6 classes including my main.


run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable EMPTY
location: class games.board.Mark
at games.board.Cell.<init>(Cell.java:10)
at games.board.Board.<init>(Board.java:10)
at BoardGameTester.main(BoardGameTester.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)



Can you add the code line that corresponds to Cell.java:10, Board.java:10 and BoardGameTester.java:13 ?

This error is from running your program or compiling your program?
7 years ago

Brian Bykenhaal wrote:





This won't solve the problem you are having below, but I don't think you need to declare the Board variables as private static in your class and also local in your function. If you do it in one or the other, you should be able to get rid of the @SurpressWarning .
7 years ago

Mike Philips wrote:

Cheers Red, that went over my head a bit to be honest, probably a few thing's I need to get through first ;)




It would look like this:

7 years ago

Mike Philips wrote:I have a do while loop that's just asking the user to input a number until the number 6 is input, at which point the user gets congratulated for entering the number 6...


however I'd like to make an if statement that changed the SystemOutput if an incorrect value was entered. If a letter is entered at the moment the program returns an error, so I was wondering how to make it so that if letters are entered instead of numbers the program will return something along the lines of asking the user to enter a number instead, and re prompting for a number...

I'm not sure where the If statement should live, inside the do code block or after it where the while part is?



The nextInt() method throws an exception if it doesn't find a number as the next token in the input. So you should call it in a try block and catch InputMismatchException . However, in doing that just now, it seems like the bad characters just remain in the scanner and have to be removed in order for nextInt() to work. Calling nextLine() got me past the alpha chars, but it seems like there should be a better way to do that.
7 years ago
The String class has a method charAt() that is used to get a particular character in the String. You won't be able to get a specific character by casting a String to Char.

What does the getData() method return?
7 years ago

Natasha Teokotai wrote:

// build classifier - do this once only for each classifier
fcs = new FilteredClassifier[models.length]; // Create a FilteredClassifier object



That line will not create a FilteredClassifier object. It will create an array of size 7 which can hold FilteredClassifier objects. Since you are doing it inside of a loop, you will end up creating 7 arrays to hold FilteredClassifiers, only the last of which will be accessible via the fcs variable. That line needs to be moved above the for loop and that should get rid of the Null Pointer Exception.


((OptionHandler)models[j]).setOptions(optionsArray[j]);



I would guess that line has already been used somewhere previously. It is out of place in that spot and I think it is just doing something that was already done. At least it appears that it should be done where you create the models array.



// Run for each classifier model
for(int j = 0; j < models.length; j++) {



Does that comment belong with the lower for loop?


61 fcs[j].buildClassifier(training);



Because you created the array inside the loop, only the array created in the seventh iteration is accessible, and it only has data in the index-6 element.
So when you go to access index-0 element in this loop, you get a null pointer exception since that is all the index-0 element has. As stated above, moving the creation of the array that fcs refers to outside of the for loop should get rid of the exception.


fcs = new FilteredClassifier[models.length]; // Create a FilteredClassifier object

comment should be "Create array to hold each FilteredClassifier"



7 years ago

Natasha Teokotai wrote:
Absolutely correct
I wish to retain seven filtered classifiers

We start with a base classifier such as KStar
We then apply filters, options etc.
And end up with a filtered classifier e.g. KStar_filtered

However, whilst I have the base classifier array initialized near the beginning of my overall program I do not have the same initialization array for the filtered classifier
And this is where I am stuck

I need something along the lines of

// create an array of Filtered Classifiers
FilteredClassifier [] fcs = new FilteredClassifier[] {
???
???
}

but what exactly do I put in between the {} ??

Natasha



Since you are going to do seven, you can either create seven different named variables KStar_filtered, etc., or put them in an array (or ArrayList as Fred suggested).

Assuming an array, since everything else is in an array, then you should have your same 7-times loop and inside is the statements you are currently using to do one fc:

7 years ago

Natasha Teokotai wrote:

Hi Red
Thank you so much for answering my query.........................

I have tried to separate out all code which should only be run once !
********************************************
// called once
Classifier[] models = createModels(optionsArray);

// called repeatedly and contains the part of your current logic that must be done each processing cycle, versus being created once and forgotten about

for (int j = 0; j < models.length; j ++) {

training.setClassIndex(classAttributeArray[j]);
testing.setClassIndex(classAttributeArray[j]);

fc.buildClassifier(training);

// test the model
Evaluation eval = new Evaluation(training);
eval.evaluateModel(fc, testing);

// print out results, etc.

}

process(models);

Classifier [] createModels(Options[] optionsArray)
{

// Choose a set of classifiers
Classifier[] models = new Classifier[]
{
new KStar(),
new J48(),
new JRip(),
new NaiveBayes(),
new LMT(),
new KStar(),
new LibSVM()
};

for(int j = 0; j < models.length; j++)
{
Remove filter new Remove();
filter.setAttributeIndicesArray(filtersArray[j]);
FilteredClassifier fc = new FilteredClassifier();
((OptionHandler)models[j]).setOptions(optionsArray[j]);
fc.setClassifier(models[j]);
fc.setFilter(filter);
}

return models;
}
*********************************************

Question: In the repeated over and over section there are two references to 'fc' (filtered classifier) which originates in the once-only section

How do I refer to the current 'filtered classifier' in the repeated over and over section ?
'fc' will not cut it !

Natasha

p.s. The data is the only thing that changes at each new time interval
As you suspect - the seven classifiers with their options, filters, etc. is static



You can make fc member data, like the models data in my other answer today. But are you using fc correctly? You are looping and continually creating a new FilteredClassifier and assigning each object created to the same fc variable. Which means 6 of those you created will not be accessible, only the seventh one created. If you do it in a loop of seven in initialization, then there should be an array of FilteredClassifiers to store all seven creations of a FilteredClassifier.
7 years ago
One way to do it is to pass the data that was created once per session in initialization (or setup) routines as parameters to the process method

process(filters, something_else, and_another_arg, ...). But since you process multiple times (6 hour intervals), maybe there should be process() and processData() routines.

But if everything is in one class, instead of passing the initialization data in for processing, you can also share any data between methods by having them as class member variables. Like this:

7 years ago
I would have chosen B in the past since I had no qualms about passing in parameters that were updated. However, the current talk of functional programming and pure functions makes me prefer functions that do not update their parameters and reflect all work in their return value. So I would choose A because of the parameter update in B.


You didn't ask for any code suggestions, but with regard to the EmailContent class, since the Email() class has a default constructor, I think you could create an Email in the helper function and avoid a separate class to pass back the data in A and also avoid updating a parameter as in B. Although the name of the function is problematic:




Also, maybe the EmailContent class could be a nested/inner/local class declared inside of createEmail() since it has no purpose outside of that function. Although the static createEmailContent would have to become a part of EmailContent.
The nested class could do all the work, with the createEmail() method just creating one (passing the three parameters to it's construcdtor) and then getting the newly created Email from it.

7 years ago

The problem not to use a custom constructor or move "createEmail" inside of the class Email is that it's part of an external API. I should have mentioned it in my example, sorry.




Is a custom constructor a constructor that takes parameters or are you saying you don't want to modify the Email class from what it already is? For instance, by making a constructor for Email that takes all three parameters to createEmail and adding either of the static methods to Email as a regular method?
7 years ago
What is unique between each run inside the for loop? Is the data object where changes between loop iterations take place? You can move things that only need to happen once to separate methods and just have what needs to occur each time interval in the method that does the processing for loop.

I think
((OptionHandler)models[j]).setOptions(optionsArray[j]);

can definitely be moved outside the processing loop as I am assuming that the options remain the same from one performance of the loop to another.

The call to setOptions should be in a separate initialization for loop right after the models() array is created. The models array should be created outside of the method that contains the processing for loop so the models aren't created over and over, assuming that they only need be created once.



Anything that can be created just once, can be added to the process() parameter list.

process(models, filtersArray, .... );
7 years ago

Avor Nadal wrote:Red Smith: I guess that I should have clarified that, sorry. "createEmail" is a method of an utility class that has nothing to do with Email. It's only used to encapsulate code that is going to be used in many places of my application.



What is the advantage of having a createEmail static method in the utility class, versus making an Email constructor that takes the same parameters?
7 years ago
Is createEmail being used (versus a constructor) so that in the future subclasses of email will be created based on the parameters to it?

7 years ago

Natasha Teokotai wrote:

This I don't understand at all (and maybe I need to talk to the weka architects to find out what is going on?)

That is, unless you have any further suggestions ?

Natasha



It probably is a question for the Weka Mailing List and Forum but before you do that you might want to put

myConsole.getOut().println( Arrays.toString( ((OptionHandler)models[j]).getOptions() ) ) ;

after every line so you can say exactly what method-call/line is causing the change.
7 years ago