JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just a guess:
Its line 12 that's the problem. rows is the number of rows, so valid values for the second parameter should be 0 to (rows-1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I would not put the code lines 4-8 in paintComponent. They don't belong there, and don't forget that paintComponent may be called by Swing at any time.
I would just have paintComponent do the painting, nothing else.

In the listener for the undo button I would simply delete the latest entry from the shapes list and call repaint()

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Who's UI?

Don't worry - it's a bit of an ongoing in-joke here at the moment.
I've sent Dani a PM on your behalf to let her know you want to get in touch.
JC

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

g is undefined at line 6 - maybe you intended graphics2d (see line 3) ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Normally for an undo you would get rid of the last thing you added - ie remove the last Triangle, Rectangle, or Circle from its corresponding list, then redraw.
Your code deletes all the Rectangles in the list - regardless of how many there are, and regardless of whether the last thing you added was a Triangle, Rectangle, or Circle.

Hint:
You can make this a whole lot simpler by recognising that triangle, rectangle, and circle are all kinds of shapes. You could create a DrawShape class, with an abstract draw(Graphics2D g) method. Let your three Draw... classes extend it, and each implement its own version of draw(Graphics2D g).
Now you only need one List<DrawShape> that you can add all three shapes to. You can draw them all with a single loop - Java will call the correct draw method for each type of shape.
And (here's the punch line) the last thing you created will always be the last DrawShape in the list, so to undo you just delete that one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to create your triangle on mousePressed, and keep updating it as the mouse is moved.

ps: down voter: did I get something wrong?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's because you don't add the new triangle to triangles until the mouse is released.

Upside down may be because y coordinates start at 0 at the top and increase downwards.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looking at your code again it seems you are setting the font on line 17.
SANS_SERIF is a porportional font where blanks and letters have different widths, so columns will not align.
Try MONOSPACED instead.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe it'sthe font in the xps document.Are you sure you are using a mono-spaced font (like courier for example)?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't know what your latest code looks like, but here's an example that works...

        System.out.printf("%-30s %10s %10s %10s\n", 
                              "Item", "Quantity", "Price", "Amount");

        String format = "%-30s %10.2f %10.2f %10d\n";
        System.out.printf(format ,"a",1.1,2.0 ,400);
        System.out.printf(format,"bb",100.,20.99,4);

Output is

Item                             Quantity      Price     Amount
a                                    1.10       2.00        400
bb                                 100.00      20.99          4
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Try posting it as code - that will preserve spaces

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't see the layout you are getting unless you format your post to retain blanks etc, but in general...
if you are formating a string followed by 3 numbers you would left-justify the string, and format the numbers with a numeric format (right justified), eg "%-30s %10d %10d %10d\n"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I have given a space of %30s between every field

That's not how it works. The width spec is the minimum width to use for the field, not the space between fields.

Your format is "%s %30s %30s %30s" That means the first item is printed a String with no padding, and the rest are printed as Strings padded to 30 chars wide.
If you want the first field printed at a fixed width then you need somethig like "%30s %30s %30s %30s"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't have an answer - I'm just checking that it's possible to reply now... JC

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

26: double apr = 0;
30: HNUnit4Ch13Investor.setInterestRate(apr); // sets rate to zero
33: apr = input.nextDouble(); // this is the real rate, but too late. You never use it

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is why Swing has Layout Managers. You start with the components (text fields, drop-down lists, whatever) which Java sizes to fit the text in them - nb this depends on the font you are using and the screen resolution. You use a layout manager to add these to the JFrame according to where you want them to go (top, bottom, etc). The Layout Manager then sets the size of the JFrame to fit your components according to the layout you wanted. This is done at run time so when you run on a different machine the frame is still sized appropriately. There is a decent choice of Layouit Managers, some simple, some less simple giving you detailed control over how things are spaced out, how they share the space when the frame is re-sized etc.
See https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Going back to Kernighan & Ritchie...

An array name is a pointer expression ... a reference to an array is converted by the compiler to a pointer to the beginning of the array ... a reference to a[i] can also be written as *(a+i)

Obviously this implies that the first element of an array MUST be [0]

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your mistake was to use a message dialog. That's intended to show a message and then close. If you want the user to be able to chose between OK and cancel/close then you need to use another type of dialog.

You could use a confirm dialog with OK and Cancel buttons . That returns an int showing whenther it was OK, Cancel, or close that was clicked.

Even better, ditch your whole thing and just use an input dialog to prompt for a store and accept user input. If the user presses OK it returns the input. If the user presses Cancel or closes the dialog it returns a null. Just test that return value to decide whether to save or do nothing. This is the obvious "right" solution.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your code says:

create some fields
open a (modal) message dialog
...  (modal dialog waits for user to click OK or close etc)
set store name

so that's why it sets the store name when you close the dialog

If you only want it to update on the save button, then you don't want the code at lines 6-11 above, you just want that code in the save button's event handler (if I understood you correctly).
OR Just use the standard dialog that has an input field !

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Try displaying the arraylist - it looks like maybe you have words and meanings originally adjacent (?), but after sorting there's no relationship between the word to its meaning

I see no code to display a meaning anyway

you have an empty catch, so there may be a serious error that you are ignoring

lines 28 etc you keep changing the text ina loop so only the last pass will have any effect (other than to slow things down)

... I:m sure there's more.
It's better to write your code in small sections and test each before moving on. That way you only have one or two bugs at a time, and you know where to look for them.

ps Why not use a map<word, meaning>?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Wow. That's really weird. What happens for 17-5-2017, or when you change the end date?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm looking at line 14 where the last argument is arcEnd (also line 17) which should be 2 but is actually increasing by 4 on each pass of the loop.
It looks like you think the last two parameters are the starting and ending angle for the arc, but they are not. To quote the api doc "The resulting arc begins at startAngle and extends for arcAngle degrees".
Your last parameter should be 2, and you do not need endArc at all.

Start4me commented: That makes sense. I must've overlooked the fact that the second parameter is the degrees to which it needs to draw for. Thank you so much. Now it work +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The last parameter for fillArc is the size of the arc in degrees, not the angle at which it finishes. So it should always be 2 or 4 (sorry, don't have the time to work out which)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Rproffitt...
What's going on here? Who are you quoting?

rproffitt commented: I worry their spelling may be causing other troubles. http://marcoiannicelli.com/portfolio-posts/algorith/ (the web) +12
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 4 textara_ is that how it is in the actual source code, or just a copy error?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm not a php user, but just reading what you posted...
these constants have a name and value (which depends on the context).
The name appears to be case-insensitive (ie LINE Line line are all valid), but the values follow case-sensitive rules as above.
Maybe a php buff can confirm or correct that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Does the ID have a known fixed length? If so you can have a text changed event listener that ignores text less than the necessary length, and starts the search as soon as the text length is right.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I agree, it's a variant of agile. I've used agile-type methodologies in projects since the mid 1990's, and I'm a huge fan.
The truth of software development is that nobody really knows what the product is supposed to be until they see it and use it. That's why waterfall always fails.
Keep prototyping and getting feedback on the prototypes until the feed back is good enough. Then tidy it up and you have version 1.0 ... at which point you haven't finished, you just started.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The semi-colon on line 1 is not the correct sytax for an if/else, so when the compiler gets to line 4 it does not see it as a valid else

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Seems to be an implementation of "determinant expansion by minors", so Google that, eg
http://mathworld.wolfram.com/DeterminantExpansionbyMinors.html or
http://paulbourke.net/miscellaneous/determinant/

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That code is a good demo of how not to format code, but in various ways it's NOT a solution the requirement that was posted.
Was it intended as a sort of example of how to approach it (in which case that's better)?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

As usual Oracle have the answers on their web site. For example: https://docs.oracle.com/javadb/10.8.3.0/devguide/cdevdeploy32171.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The thing is that car goes to the initial coordinates i tell him to go but then it does not move at all

OK, please see my previous post. I think you misunderstand Stopwatch.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm not a VB user, but just asking...
is it OK to do animation in a cpu-bound loop like that, or is the use of a Timer mandatory rather than just recommended?

even if it is OK, won't that code just go

start stopwatch
set location
stopwatch is < 3000 (unless set location is world's slowest method)
skip if block
return

it kinda looks like OP wants Stopwatch to behave like a Timer

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

if (n = 1) return 1;

No, not a bug, because yes, that's pseudo-code. Depending on you language you may have to code the test for equality differently (but not, for example in SmallTalk or Pascal). The whole discussion of = vs == in C derivatives is, of course, correct, but not really what this is about.

This recursive algorithm is interesting because you really want to return both the index n of the longest sequence and its length. In (most?) C-like languages you can't easily do that, so you have to return n, and then you need a duplicated call to sequence_Length (line 13) While looking at this myself I was tempted to define a class or struct with both n and its corresponding sequence length just so I could return that but it did seem overkill. If I were to become worried about the efficiency issue of duplicated calls to sequence_Length I would probably prefer to use a Map to cache known n,sequenceLength pairs and thus eliminate re-calculations.
Swift allows you to return an arbitrary tuple from a method, which seems to me to be the cleanest way to code this. (You may be getting a suspicion that Swift as is my favourite language, successfully incorporating the lessons of Java, C* etc , and you would be right.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can call it a guard or you can call it a base case, but there's no reason to have a guard AND a base case. If you find yourself needing both, you probably have a design flaw.

I respectfully disagree.
A guard protects the following code from incorrect or invalid values
A recursion base case is the case where recursion stops
They are two different things with different uses, and shouldn't be confused. eg

int next(int n) {
    if (n<1) raise error "algorith is undefined for n<1"  // guard
    if (n = 1) return 1;  // base case
    ...

I don't know about C++, but Java uses assert for guards (can be turned on or off at runtime for improved error checking vs performance), and Swift has explicit guard statements/

Personally I'm huge fan of guards to document and enforce a method's pre-conditions

AssertNull commented: Good food for thought. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Cast your mind back a day or so when you were making great progress by starting with a short pseudo-code text description of the process and some pencil and paper simulation to check it out. You seem to have forgotten about that and are hacking code that frankly makes no sense.
Please do not write a single line more code until you have a working paper simulation.

AssertNull commented: Paper and pencil rarely fails. +7
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When I posted the "two line" version I was just trying to show the logic in pseudo-code, it just happened that's it's pretty close to actual code, depending on your language.
AssertNull is right (as usual) in that an experienced programmer would probably code it that way as well, but a learner would do far better to break it down and make the intermediate results visible for debugging. He's also right about variable names.

ps I keep referring to AssertNull as "he" in the absense of any biographical details. If that's not appropriate then I apologise to him/her/hir/whatever.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ps Doing a hand simulation is exactly the right thing to do. Recursion always twists your brain into prezels, and going straight to code is asking for a mess. We are always saying it in these forums - if there's the slightest doubt about what the code should be doing, do it on paper first - but most beginners are too impatient so end up wasting more time.
I would just say - don't just simulate with some random numbers. Start with some real data where you know the correct answer before you start simulating, so you know when you have got it right.
Anyway - well done.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't see where that so n=2 came from!
Let's use 9, because I have the sequence for that..

n= 9
not <=1
return max (9, seqMax(next(9)))  // next(9) is 28, so this is max(9, seqMax(28))
    1st recursion: 
    n = 28
    not <= 1
    return max (28, seqMax(next(28)))  // this is max(9, seqMax(14))
        2nd recursion:
        n = 14
        return max (14, seqMax(next(14)))  // this is max(9, seqMax(7))
               lots more recursion until eventually we get seqMax(2)
                     n = 2
                     return max (2, seqMax(next(2)))  // this is max(9, seqMax(1))
                           last recursion
                           n=1
                           return 1
                     can now return max(2,1) ie 2
               lots more returns getting 40 in the process
        can now return max(14.40) ie 40
   can now return max(28, 40) 
can now return max(9, 40)

The base case is n=1, returns 1 without recursing, which then enables the whole recursive stack to unwind

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, we can help you do it. What have you got so far?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry sorry sorry. Looking back at that post I wonder what I was thinking! Please ignore it. I'm sorry if it distracted you. I knew what it should look like, and forced what I saw into that shape, even though it was nothing like. Put it down to a "senior moment"!

AsserNull didn't suffer any such brain damage. Please re-read his post. The description of the algorithm in his last para is correct, and it's what you need.

to get the maximum value of ANY sequence, you can take the maximum of the FIRST element and the maximum of the remaining elements... which is the maximum of n and sequence_Max(next(n)). Your base case.. is when n is 1, so check for that at the top. Return 1 in that case.

Re-phrasing that in pseudo-code:

if (n<=1) return 1
return max (n, sequence_Max(next(n))

Your code is a lot more complicated than that, and I have no idea why.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have the recursive call to next on line 4, which is before you test for n<=1 and terminate the recursion, so it always recurses while initialising the variables, even if n<=1.

<EDIT> Above comment is wrong - I left it here so subsequent posts still make sense.. Mea Culpa. JC </EDIT>

In any recursion you must deal with the terminating condition before you make any recursive calls

(and the n!=1 test is redundant, because you have already reurned from the method on line 7 if n==1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

subSet method is way off - contains code that is not just unneccesary but actually prevents it working - it's really simple given what you already have done, but you have made it too complicated.

s2 is a subset of s1 if

for each element in s2  
    the element is a member of s1 

or, fleshing that out a bit..

    for each element in s2  // use an enhanced for loop syntax
       if (! the element is a member of s1)  // you have a method to do this
         return false
    return true
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, Java is pretty strict on making sure you have initialised variables - and the logic analysis it does to confirm that is pretty sophisticated, but in the end it goes for safety. "No surprises at run time" is a fundamental desire behind Java's design.

Honestly, having avoided C++ like the Devil's spawn, I was still astonished to discover that something as innocent as f(f1(i), f2(j)) is code that can legally produce different results at run time depending on the compiler's whim. The Java Language Spec, incidentally, does describe what the one correct solution is for the Java equivalent, and it's not difficult. I guess that compiler writers write C++ compilers to suit themselves, so making their life a bit easier is well worth the price of having ordinary programs produce unpredictable results

Anyway, please feel free to ignore this as the rantings of a bigot who thinks C++ should have been strangled at birth.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Look at line 45. You have an extra ; that completely changes the meaning of that line of code, and therefore the meaning of what follows.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If there are no bugs in your code, it will work everywhere.

True for many modern languages, but not C++. C++ suffers from "undefined behaviour" - code situations that the language definition deliberately says are legal but whose implementation can be left for the compiler to decide what to do. The best you can hope for is that your compiler will flag any such nightmares.
Eg

i = ++i + 2;       // undefined behavior until C++11
i = i++ + 2;       // undefined behavior until C++17
f(i = -2, i = -2); // undefined behavior until C++17
f(++i, ++i);       // undefined behavior until C++17, unspecified after C++17
i = ++i + i++;     // undefined behavior
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, in the past I have seen style guides and standards that deprecate re-using parameter variables,even if the language allows it. I don't know the reason, but maybe its to avoid confusing less-skilled programmers who may think that

void fn(int a) {
   a = 3;
}

int a = 1;
fn(a);
print a

may display the value 3.

It's also common to have style guides about the max length of a function definition (also if block, else block, loop body etc) with the value set about 1 screen-full. That's a valid readability/understandability target, but it's totally unrealistic as a mandatory standard. Sometimes its clearer to keep code together in one block, even if it is a bit long.
In practice, if you adopt good design, with single-purpose functions, and optimum cohesion & coupling then the resulting code blocks will be the right size anyway.

Having said that, I see where your prof is coming from. Here on DaniWeb we often see beginner's programs that are one huge function with ifs and their matching elses separated literaly by hundreds of lines. The are totally unreadable. Better to stamp that out from day 1 than try to fix bad habits later. I get obsessive about variable and function names for the same reason :)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Although it's true that you can omit the guard, I would argue for leaving it in. When you're reading a recursive method the first question is (how) does it Terminate? I would be very happy to see that right up front. For me readability trumps efficiency 95% of the time.

Re changing parameters, C++ passes parameters by value unless you specify passing by reference, so the value inside your function is your own private copy of the original, and you can do whatever you like with it.