JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
if (stringExpo(p)==""){
  ...
else {
  result= result+stringCoef(p)+stringExpo(p);
  if (stringExpo(p)==""){

Looks like the second if will always be false, since it's in an else clause following an if with the same test?

Plus, the old old chestnut, you can't compare Strings with == unless you actually mean "are exactly the same Object". Use string1.equals(string2) to test of they contain the same sequence of characters.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java emuns are like abstract classes, and the values of the enum are like subclasses, so the pattern is the same. But enums handle all the initialisation, give you the array, as well as lookup by name, and ensure the rest of the "plumbing" works ok. Here's an eg:

enum Fns {
   ADD {
      int calc(int a, int b){return a+b;}
   },
   SUBTRACT {
      int calc(int a, int b){return a-b;}
   };  // etc
   abstract int calc(int a, int b);// calc must be implemented by each enum value
}
...
      // use an enum value to call the right method..
      System.out.println(Fns.ADD.calc(3,4));

      Fns[] fns = Fns.values(); // array of Fns in order that they were defined
      System.out.println(fns[0].calc(3,4)); // select function by index number
      System.out.println(fns[1].calc(3,4));
      // or just...
      System.out.println(Fns.values()[i].calc(3,4)); // i==0 | i == 1

      // call Fn by name,  
      // Enum.valueOf(Fns.class, str)  returns the enum value named by str
      System.out.println(Enum.valueOf(Fns.class, "ADD").calc(3,4));
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not quite a table yet... you still need the array that you had in your first code

Functor[] fns = new Functor[2];
fns[0] = new Addition(); 
fns[1] = new Subtraction(); 

// do function "n" from table (n==0 | n==1)...
double result = fns[n].compute(20, 30);

ps: Java enums(not to be confused with the feeble "enum"s in certain other languages) give you a neater way to package and error-proof this exact construction. I can share a quick example if anyone is interested.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Cheers JamesCherrill, thats exactly what i wanted, Thought it would throw exception if used null.

It would throw an NPE exception IF you tried to use that parameter, but you don't. Passing null as an Object param is always valid syntax (unless it leaves you with an ambiguous method ref, which won't happen in this case).
cheers
James

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If they are in the Eclipse project you can select them under "select resources to export" when you export to jar. That will include them in the jar and all should be OK.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... then in that listener just setVisible(true) for the JTextField - or am I missing the point of this question???

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Wow! That's a load of code with no indentation or syntax highlighting. I hope you don't expect anyone to read it. Code tags next time please.
Anyway, what help do you need, exactly?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Well done Steveo. Isn't it doubly satisfying when you get something to work AND you know it's done "right", not a bodge?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Google the Swing Event Dispatch Thread - this is a standard problem that's well documented - but basically your ActionPerformed method blocks all screen updates etc until it has finished.
The right way to handle delays in a Swing GUI is to use a javax.swing.Timer - once again this is well documented on the web.

StevoLord commented: Quick and helpful. Thanks again +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. Look at the brackets in your while statement.
2. Next time post your code in code tags and post the complete compiler error message (including the line number)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

line 6 instead of using 3 as the upper limit of your loop you should use the actual length of the words array. (It's like, but not the same syntax as, line 7, where you use the actual length of the String as the upper limit for the loop)

Latvija13 commented: Helped solve the problem! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

lines 82/83 you add together the numerators (after getting them over the LCM denominator), but you forgot to divide by the LCM denominator - ie you calc
(2*6) + (1*6)
when it should be
((2*6) + (1*6))/(6*6)

But in any case shouldn't add return a RationalNumber, rather than an int (which will be 0 in your test case because the answer is <1

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

the front tires can be within 3 psi of each other:

-3 <= (right front pressure - left front pressure) <= +3

back tyres ditto.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Where do you load the word into letterArray?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

i think that using the try catch finall block is more efficient than using the throws
clause..
:)

It's nothing to do with "efficiency". If your method can sensibly handle the Execption then it should catch it and handle it. If it can't sensibly handle it it should throw it up to the caller.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Well done Vichu - you just rewarded Rona25 for being lazy and dishonest. What did he/she learn from this? no Java, that's for sure, just that cheating is easier.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

NullPointerException ("NPE") means either you are using a variable that hasn't been initialised, or you are using the result of a method that has returned null.
Search down the error listing until you find a reference to one of your classes, and that's where the error happened, in this case:
at OptionWindow.<init><OptionWindow.java:63>
<init> means during initialisation of a new instance (ie in the constructor), but 63 is the exact line number in file OptionWindow.java

I had a very quick look at the code, and it looks like you have initialised all the JLables. but not the JTextFields, so when you try to add a text field to the window, the variables holding the text fields are still null.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Each time you call your method test you create a new ArrayList. Just declare and initialise it outside that method.

shack99 commented: thanx, it helped +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's probably OK for a college project, but just remember that it's poor practice for real life - Java is designed for use with multiple languages, so the text in your GUI could end up in a user-defined choice of English, Arabic, or Japanese; you can't use it reliably to identify things in your code.
Anyway. mark this one as "solved" and start a new thread if you get a new roblem.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's some code you can use as a starting point. Please share your final solution with everyone here - it's a common problem.

public  JRadioButton getSelectedButton(ButtonGroup group) {
      Enumeration<AbstractButton> e = group.getElements();
      while (e.hasMoreElements()) {
         AbstractButton b =  e.nextElement();
        if (b.isSelected()) return (JRadioButton) b;
      }
      return null;
    }
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is (IMHO) a horrible design omission in Swing - AFAIK there's no simple method to get the selected JRadioButton in a ButtonGroup. Most people just loop thru the buttons using their inherited isSelected() method to find the one (if any) that's selected OR they have a Listener for the buttons that keeps track of the current selection.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Once a thread is started it executes "until the app exits" OR until the run method terminates normally. Unless the run method contains an infinite loop sooner or later it will either execute a "return" statement, or it will drop out of its last statement (or throw an Exception).
The "true" in quuba's code just ensures the loop keeps on running until something else stops it.
The idea of the sleep is to control the speed at which the repaints happen - ie if we flatten the loop it goes
repaint
sleep 100 mSec
repaint
sleep 100 mSec
etc
Without the sleep the JVM will execute repaints as fast as the CPU can go, and this will make it animate far too fast, and it will be difficult or impossible for any other threads to execute.
You don't need to override Thread's start() - just call it.

Having answered all those questions, I now have to tell you that, depending on what else is going to be in your app, this is probably the wrong way to do this (sorry). You can stay with this a bit longer, and you should be able to get a basic animation running, and you will learn good stuff about how Java threads work.
If/when you run into serious problems you can switch your code to use the recommended approach (Swing Timer) to run your animation. This uses and integrates properly with the …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

AFAIK it's no problem if you handle KEY_PRESSED and KEY_RELEASED KeyEvents.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

We've all don ethat at some time or another! Mark as solved?

Akill10 commented: Saved me from a mental breakdown +1
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I see you are using ImageIcons in JLabels to show the images. Here's how I would think of this:
I'd create a subclass of JLabel that has an Image and a zoom ratio as instance variables. I'd use getScaledInstance to scale the Image according to the zoom ratio in a setZoom(float ratio) method and use that to update the ImageIcon.
I could have any number of these and use an ArrayList to track them all if necessary.
getScaledInstance has some bad press, although I've found it to be OK, but heres a link to ways round that
http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

An Exception is basically a data structure used to transmit information about an error from the source of the error back up to a calling program. An Exception handler is a piece of code that catches and deals with an Exception (eg prints an error message). The Exception class may have methods that the handler can use, eg to print the stack trace, but the specific handler has to decide whether and when to use those methods.
The code you just posted seems way over the top. All you need is

public class CheckInException extends Exception {
    public CheckInException (String message) {
        super(message);
    }
}

In your checkIn method you can then just say:

if (some error has been detected) 
  throw new CheckInException("Some suitable error description");

Then any code that calls your method can simply catch(CheckInException e) {...
to handle the Exception as it choses.

ps
The code that throws the exception knows that a problem has happened, but doesn't know what is the right thing to do about it - eg it doesn't know whether it's been called from a console app (so the message should go to System.out), or a Swing GUI (in which case it needs an error dialog box), or a web server (send the message as an HTML page). This is also true of the Exception class itself.
The calling program (try/catch exception handler) doesn't know the details of how the problem was detected, but it does know …

ticktock commented: Muchos helpful :) +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In OO design classes represent things, and methods represent actions. (OK, this is the very simplified version!). So a class "sell" or "buy" is probably a bad idea, although Deans "buyScreen" and "sellScreen" are OK because they represent bits of GUI.
However, this is all a bit cart-before-the-horse.
Look at the problem description for the nouns that refer to the key elements - eg "product" "customer" - these are possible classes. Look at the key attributes they have - eg a product has a price, a customer has an amount of cash - these will be possible instance variables for these classes. Finally look at the verbs in the problem description - eg buy, sell - these will be candidates for methods (eg class Customer has a "sell" method that takes a Product as a parameter and updates the Customer's cash.
Finally, design a user interface that uses these classes and methods to achieve the desired results.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Well don't assign the result of Arrays.sort to anything.

A "senior moment"? Arrays.sort sorts the array you pass to it and has return type null. Hence my concern that it changes the actual source data.

As for nth highest, median etc, I fully agree with you.

masijade commented: I guess I deserved that. ;-) +10
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are Exam and its implementation the same thing??

No, but in this case the method is defined as returning an Exam. Because Exam is an interface you cannot instantiate one directly. If ExamImpl implements Exam then this is a class that you can instantiate, and any instance of it will also be an Exam, so it is valid to return an instance of ExamImpl where the return type is defined as Exam.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I guess the "parallel arrays" 1 & 2 contain (1) each value that occurs in the source data and (2) the number of times that values appears. Then you can search (2) for its max and get the corresponding value from (1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, of course you can catch it, but you don't have to. Even worse, unless you study the complete source code of the public method you are calling, you have no way to know that this exception may be thrown and are therefore very unlikely to write a try/catch for it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How about the "contains" method in the String class - or is that too obvious?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You refer to the list using the class name, so that will only work if the variable is static. If it's not static you need to refer to it through an instance of the class, as in

myPackage.firstClass otherClassInstance = ... get a reference to an instance of the other class
int index = otherClassInstance .myList.getSelectedIndex();

Once this is fixed you can see if there are any other problems ;-)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If I understand you right, then what you are doing is OK. Once someone has checked out that's it, they can't change the list of items later except by a possible "update existing order" function, and then only if the order hasn't been dispatched (at least, that's how e-shopping sites and supermarkets work for me).
I'd expect the whole item list GUI to disappear after a check out and be replaced by a "thank you for your order" screen.

ps: parallel post with Gerbiller, who has a valid suggestion - perhaps you should update the total in real time (ie in response to item selection/deselection), but the point I made about check out stands.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No. You may be failing to distinguish between a class and an instance of that class.
You need Panel.class ( not java) at the client end so that the JRE understands what a "Panel" is. You can then use ObjectStreams to send/receive individual instances of Panel.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use the SimpleDateFormat class - create an instance using a pattern that describes your date's format, then use the parse method to convert your String to a java Date object. Once you have a Date object you can use another SimpleDateFormat to convert that to pretty much any format you want.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can leave the text field as it is and fix the commas after you read the text into your program, ie
String textWithoutCommas = loanText.getText().replace(",", "");

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is the method that is called first, and when it ends the program ends as well.

... unless the program has more than 1 thread (eg every Swing GUI program)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. Create a new class that contains a label and a textfield and write the necessary constructor(s) and other methods.
2. If you just want it for positioning etc, put them both in a JPanel

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

5 seconds or 5 minutes?
Are you doing those long-running tasks on the Swing event thread? If so, move them to a SwingWorker thread so they execute in the background while the GUI continues to display

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

passnum = 26;}

int table[][] = new int [26][26];

table[passnum][passnum[i++]]++

Now can you see it? (the max valid index for an array[26] is 25)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I really want to endorse the "println" approach.
Some of the hardest problems to solve are because:
1. You have an incorrect understanding of how some API works, or
2. You are missing something that you didn't know you needed
In both cases no amount of introspection or code re-reading is going to help, but lots of printlns (or using a full debugger to trace execution & variables) will quickly reveal the source of the trouble.

The other key strategy is to debug in small increments. It's amazing how often you see beginners coding a complete app before trying to run anything, then have no idea why "it doesn't work".
It's usually possible to package a small "main" method in every public class that just runs a quick hard-coded test of that class's basic functionality. I always use these to check out classes before trying to integrate them.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Wait a minute..

public Object getElementAt(int i) { return "Task " + i; }

you can call setElementAt from here to next Tuesday, but getElementAt will always return the same value!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

AffineTransform.
You create an instance of AffineTransform that maps the rotation you want and apply that to a Graphics2D instance with setTransform(...). Paint your icon to the Graphics2D and hey-presto, you have a rotated icon.
Here's a quick intro:
http://www.javareference.com/jrexamples/viewexample.jsp?id=88

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, now I understand.
oOoKodakoOo please meet convertRowIndexToModel
convertRowIndexToModel pleased meet oOoKodakoOo

No, seriously

public int convertRowIndexToModel(int viewRowIndex)

Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.

Parameters:
viewRowIndex - the index of the row in the view
Returns:
the index of the corresponding row in the model

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Smallest = Math.min(Smallest,a);

If Smallest is -99999 then for any value of a >= 99999 this will return -99999. Not quite what you need.
Like I said before: Best to initialise it to the largest valid int value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ProductionWorkerA lines 31,32

blknmld69 commented: Thanks for your guidance! +1
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java assumes that a class is not Serializsable (ie not able to be written to an Object stream) unless you explicitly say it is. To do that you declare the class as
implements Serializable
(check out the API for the Serializable interface)
If all your class data is just regular Strings and ints and so on, that's all you need to do.

o0oKodako0o commented: Help me java code +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

JList() Constructs a JList with an empty, read-only, model.

The API says it all.

Ezzaral commented: Good eye. +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can add a ListSelectionListener to the first list. It will be called whenever the user changes his selection in the list. When that is called you can query which line is selected and use that to put the appropriate data in the second list.