JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's no such thing as "clear enough" code in my opinion. It may be clear to you now, but how about in a year's time, or to someone else? 90% of commercial programming involves updating code that someone else wrote some time ago - that's when clarity is everything!
:)
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think that's a lot better and the code is really clear and easy to understand. Just look at that compared to earlier iterations - it's really getting there. The repetition between the two transfers isn't too great, and I wouldn't worry about it. In real life upgrading and downgrading are very different processes anyway.

There's some confusion around the message dialogs (lines 31/43), but you'll see that when you try to run the application.

The one easy thing I would do is to create meaningful constants for the class values, eg
static final int FIRST_CLASS = 1; // etc
so rather than
assignSeat( 1 ); // what's that???
you would have
assignSeat( FIRST_CLASS ); // OK, I understand that
or, even better, rename the method as well, so it reads
`assignSeatIn( FIRST_CLASS ); // you will understand that even if you don't speak Java!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

setMnemonicAt(int tabIndex, int mnemonic)

http://www.kodejava.org/examples/733.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent! Glad we could help.
Perhaps you can mark this thread "solved" now. If you run into problems with your next assignment you can start a new thread.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's right

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can print anything anywhere on your panel...
g.drawString("some text!",x,y);
... so you can use that to print the coords or just the position in the list at the appropriate x,y for each point.
YOu may wany to use setFont to seta smaller font size - see the API doc for Graphics.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

as the Point2D class holds the point co ordinates as private , i couldnt do the for loop like you said

That's standard OO stuff - make the data private and provide o=public accessor methods. Just use the public accessor methods x() and y() in the outline code I suggested

One reason you see nothing is that you create two panels - one on line 58 which you add to the frame, and one on line 27 which you populate with data. Forget the one on line 27 etc, and just pass the real array of points to the panel's constructor, and use the accessor methods to access the x and y values.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What you ask is not difficult, and learning about it will be very valuable anyway, so here goes:
You need a window with a simple JPanel in it. You override the JPanel's paintComponent method to draw your points. The first two steps of the Oracle tutorial will get you that far (ignore step 3), and you can just copy/paste their code...
http://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html

In your paintComponent method, to draw the points you can just start with a little circle for each point in the ArrayList, eg

for (Point2D point : myListOfPoints) {
   g.fillOval(point.x, point.y, 3, 3); // 3 pixel diameter filled circle at x, y
   // assumes the points fit in the pixel sixe of the window, eg 0 < x,y < 200
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Two observations:
16 mSec is 1/60 sec Depending on your PC 1/60 sec may be the resolution of the timer, so maybe you can't mesaure less than that.
That time includes opening, reading, parsing, closing a file. That probably takes 99% of the time.

My guess is that you won't be able to measure the time for just the sort itself until you have an awful lot of data. Try timing a sort on thousands of random points (excluding creating the points).

ps I just "got" the polar order comparator and the "one point" stuff - it orders pairs of points by their angle measured from a specific reference point. That's why it's an inner instance class of Point2D; the enclosing instance is the reference point. Obvious, really.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't forget to start with the API documentation - that's the definitive source
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Almost. The classRequested variable is just a copy of the user's input for which class, already converted to an int 1 or 2. You then pass that to the assignSeat method, but you still have it available afterwards in case the assignment was unsuccessful and you need to know which class they wanted.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Comparators are simpler than you probably think. When the Collections class is sorting data it needs to know what order to put them in. Some data types (eg integers) have "natural ordering", but objects in general do not. A Comparator just takes two objects and tells you which comes first in the sort order.
All the sort algorithms reduce to comparing pairs of objects then moving them around based on which object goes first. The sort algorith selects a pair of objects the asks the Comparator which comes first. The it picks anothe pair etc etc.
The stuff about "one point" doesn't quite make sense. A Comparator<Point2D> just compares any two Point2D instances that get passed to it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Interesting question! To find all the colinear points presumably you can't just take their angles from one arbitrary point - you'll have to repeat that process using each point in turn as the "particular point"?

If this is really just about colinear, the maybe there's a simpler answer using the formula for the area of triangle - take the triangle defined by 3 points for its vertexes, and if and only if the area is zero then the points must be colinear. If you're using the Princeton algorithms Point2D class, as opposed to the standard JDK Point2D, (wich I assume you are becuase of the ref to POLAR_ORDER) then you can use its area2 method directly.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK. Don't worry. Keep posting your problems and we will try to help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That can work. The only thing you should think about is why do you want to return a value identifying the class? The caller must pass a parameter "type" saying which class, so they must already know which class it was. They don't need you to tell them what they just told you. That's what my last post was illustrating. Yes, your approach will work, but at the cost of unnessesary complexity, obscurity, and opportunity for errors.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In that case, it's time to stop and read the instructions properly:

Instruction: NOTFOUND is set to -1;
Your code: NOTFOUND = grades.get(-1);

Instruction: findGrade - private method given a parameter representing the grade's date
Your code: findGrade(Grade aGrade)

Instruction: returns the ArrayList index of a grade
Your code: Grade g .... return g;

So that's three examples of not following the instructions just from that tiny excerpt.

For your own sake I would start again with a clean sheet of paper and re-read the instructions until you understand every single word in them. Until then your code is doomed to go off in random directions.
Please don't think I'm being negative or critical; I'm just giving the best advice I can, even if it's uncomfortable.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So far you posted getGrade (no parameter list) and getGrade() (empty parameter list), yet the method signature requires a parameter of type Score, so neither of those is right. And what's the difference between a Score and a Grade? Line 6 implies that a Score can be equal to a Grade, but without the class definitions who knows?

Grade NOTFOUND = grades.get(-1); will never assign a value to NOTFOUND, and none of the rest of findGrade will ever be executed because it will always throw an exception first (see the JavaDoc for ArrayList's get(int) method). Similarly getGrade will also stop executing and throw the exception as soon as it calls findGrade. (And why call findGrade anyway when you just ignore the value it returns). From which I guess you haven't been able to execute any of this code yet.

It seems to me that you are trying to code this in pieces that are too big. You will have fewer problems if you write one method at a time, write some trivial discardable code to test that method, and get the method working before starting on the next method. If you try to write too much in one go then you get buried in errors when you finally try to compuile and run, and its really hard to see where to start fixing things.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I understand why you don't want to post all your code, but there is some more info neded. In particular, what is the return type for the getGrade() method? The name implies that it returns a Grade object (in which case you need to get compareTo working), but the context suggests that it's unlikely to be a Grade object. If, for example, it just returns an int or a char then you can compare with a simple <

(s.getGrade.compareTo(lowestGrade.getGrade)
Unless getGrade is also a variable this syntax is wrong. If, as the name suggests, it's a method then you need a (...) for the parameter list, even if it's empty ... eg s.getGrade()

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's what the classRequested variable is for!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Java compiler is your friend. Don't be afraid to try and compile your code. There's no point trying to get it right before compiling. Just chuck it into the compiler and see what errors it throws up. That costs you nothing.
At first you;ll get a lot of errors, but start with the first, fix them one at a time, re-compiling after each one.

But first...
Go back to your very first Java program (Hello World?). Remenber the method called main? Every program needs one. That's where you can: create new grades list, add some data to it, call your getLowest method, and print the result.

Have a look at that, if/when you get stuck come back here and post the whole of your code - it's hard to help you if we have to guess what's in the bits you didn't post!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's just that you do a bit too much in one statement...

seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));

just split it up to the keep track of the class...

int classRequested = Integer.parseInt( action );
seatAssigned = seatingPlan.assignSeat(classRequested);

now you have all the info you need, and also the code is easier to understand

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't agree with sleep here. The "application" can't sleep, only a thread can sleep. After the start() method everything in this code runs on the Swing EDT, and sleeping that thread will kill the animation (and any other swing activity).
Three solutions:
1. When you set the text, set a counter to 100. In your timer's actionPerformed dcrement the counter. When it gets to zero reset the text.
2. When you set the text get the system time, and 1 sec to it and save it in a variable. In the actionPeformed check the current system time against that variable to reset the text.
3. When you set the text start a new Timer with its own actionPerformed and a delay of 1 sec. In that actionPeformed reset the text and stop the timer.

Option 2 is probably the easiest/safest for you to code, but 3. is how Swing does stuff like popping up hints when you hover over a control for a second.

stultuske commented: agreed +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you execute a return statement your method finishes at that point, and it does not execute any following statements (1)

(1) Unless you have a try/finally structure

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like maybe you start a new timer when you restart, but the old timer is still running, so now your actionPerformed gets called twice every 10 mSec, and every time you restart that gets worse. Make sure that when you restart either you just keep using the old timer, or you stop the old timer before starting a new one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless you are runnning a very old verison of Java you can convert an Integer to a Double trivially using auto boxing/unboxing:

 Integer i = 3;
 Double d = new Double(i);

Your array index exception is at line 42 of Poly.IntegerPolynomial.getCoeff, where you are trying to access element number 3 of an array that doesn't have 4 elements (see the exception message), but since you didn't post that code it's hard to say any more.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I love building things, making things, designing things. And I love teaching. As CEO of a software house I wasted years in meetings with bankers, lawyers, shareholders etc, while the people working for me were doing all the things I wanted to do. On the other hand, the money was nice. In the end I went back to consulting in software design because being happy is more important than being rich.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Joined IBM as a trainee programmer in 1969, been doing it one way or another ever since, apart from a few wasted years in pure management roles. The irony is that by the mid 70's I understood maybe 50% of what there was to know about commercial programming; now it's more like 1% of what there is to know.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A couple of minor comments:

for (int i = 0; i < die1.length; i++)  
{
   die1[i] = r.nextInt(6) + 1;
}
for (int j = 0; j < die2.length; j++)
{
die2[j] = r.nextInt(6) + 1;
}

no need for two loops, why not just

for (int i = 0; i < die1.length; i++) {
   die1[i] = r.nextInt(6) + 1;
   die2[i] = r.nextInt(6) + 1;
}

=====
rollHist has 11 elements, corresponding to the 11 possible rolls, but they are numbered 0-10 corresponding to rolls of 2-12. In my opinion the code would be clearer if rollHist was 13 elements with the count for a roll of 1 in [1] and the count for a roll of 12 is [12]. Yes, that means [0] and [1] are never used, but so what?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
if (rollTotal == 10) {
   rollHist[8] += 1;
} (etc)

There's a pattern- the array index is always two less than the rollTotal, so all those ifs can eb replaced with a single line of code something like rollHist[rollTotal-2] += 1;

stultuske commented: missed your post, but yup, that's the main point +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Making stuff static is not the answer. When you say AbacusModel.addCounter(thisCol); AbacusModel is a class, not an instance, so you cannot use that to call an instance method.
However, in your constructor for AbacusPanel you do (correctly) create an instance of AbacusModel
myAbacus = new AbacusModel(numCols,numRows);
now you can use that instance to call the instance emethods of the AbacusModel class, eg
myAbacus.addCounter(thisCol);

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The signature of that method is
public ArrayList<Customer> search(Customer cust) {
so you must pass a single instance of Customer as the parameter, and it will return you a list of Customers

(although having looked at the code of that method it makes little sense to me)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To call an instance method of the CustomerModel class you need an instance of that class.
Your CustomerController creates such an instance on line 25 (cMod), so you can use that to call the method(s), as in
cMod.search(etc...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I vote for the second version. The extra lines and indentation in version 1 create a lot of clutter and will push the following code off the screen - just imagine it inside a loop inside a method definition...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are those methods defined in the AbacusModel class? In that case you created an instance of that class on line 23 as part of your constructor, so you can use myAbacus to call those methods. Without seeing the method definitions it's impossible to comment on what parameter(s) to pass.
I have to disagree with joshfizzle - just defining everything as static reduces Java to the level of beginner's Basic. In this case you create an instance with values for the number of rows and columns - presumably instance variables, which would not be accessible if you made the methods static.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why are you overriding those methods? What's wrong with just inheriting them? Your overides look just like the originals anyway.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't say anything about the code because your class extends other classes that I don't have the source code for. If you think the problem is somewhere in that try block, just insert a whole load of System.out.println statements to print the key variables at various points in the block, so you can see what's being executed and what values are being used.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What's your definition of "runs fine"? In my book a program that does not produce the desired output is anything but "fine". Have you added the code to print any Exceptions?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your catch block is passed an Exception to tell you exactly what went wrong. You should always start by printing all that info:

catch (Exception e) {
   e.printStackTrace();
   ...
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you really want to access random elements in constant time and not O(n) then I guess you have to use an array rather than a linked list. Of course the time to remove an element will then go up, ands some adds will be expensive when yopu have to re-size the array.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If your method declares a return type (boolean, int etc) then every possible execution of the method MUST return a suitable value. You have if/else tests that, if they are true, do not return a value. You must add return statements so that every possible path through yopur methods returns a suitable value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't feel confortable using enum, for some reasons I don't like them, so if I can do without I feel happier.

I do hope you will reconsider that. Enums were added to the language in Java 1.5 because Java needed them, and so do you. They are not difficult to use, but they do eliminate many kinds of difficult bugs, ie they make it much harder to write buggy code. Your code will be so much better if you use them, and you will be a much better coder.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

a small class to encapsulate the functionality

Basically, start a class called (eg) MyQueue, and write method headers for all the public methods your queue will need (public cnstructor, add... , delete... etc). Then chose an implementation and fill in the method bodies (these will be 99% just calling the corresponding method for Linked List, somewhat harder for an array implementation). Now anyone can use your class by calling the public methods, and you can change the impelmentation anytime without affecting them.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless you are processing a vast number of dequeues against a huge queue then "faster" is not going to be relevant. Do you reeally care if they take 1 microSecond vs 2 microSeconds?
Easy to code, easy to read and understand, easy to debug, easy to modify or enhance - those are the issues that matter (listed in increasing order of importance).
Arrays seem simple, but you have to deal with low-level issues like growing the array when its initial capacity is exceeded, of shuffling down all gthe following elements when you delete one in the middle.
IMHO linked lists are better to work with in every respect, except for the speed of accesing random elements by their index number (but see first sentence above).
As for " linkedlists being old, unfashioned n stuff... and arrays are the way to go" - that sounds like total rubbish to me - anyone else want to offer an opinion?

Final observation: in real life, if this was a critical part of some performance sensitive big application, I would write a small class to encapsulate this functionality, and keep the implementation (array / linked list / tree set etc) private. That way, if it became necessary, I could try different implementations without having to change or break anything else.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, but no. I'm no going to write your homework for you. Your teacher obviously thinks that you should know enough about arrays, or are ready to learn enough, so that's what you should do. Here a good place to start:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
It's up to you to try your best. There are plenty of people here who will help you if you do.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... only if you have time travel!
You start with an array that's big enough, fill in the data and keep track of how many data were entered ( = how many of the array elements have been used)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Logically you don't know the highest number until all the numbers have been input, so to show all the differences you will have to store all the numbers (eg in an array) as they are input. Then, when you know the largest, you can loop through the array and disaply all the differences.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What exactly is your question? - the code you posted displays the results.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

As for 5th question
This program is to reverse the array elements

Are you quite sure it doesn't just rotate the elements right by 1 position?

[ edit: original post now updated as per IIM's comment]

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I now have absolutely no idea what's going on in this thread!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If that's all the code then you declare customerService on line 5, but nowhere do you give it a value, so it's still null (uninitialised).