JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Perfect!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, you can put components directly into a JFrame (well, actually there are some panels that are part of every JFrame, and one of those is where your components are placed, but that's hidden from you so you don't need to worry about it unless you have some special requirement. You can just think of it as adding components directly to the JFrame).

You use JPanels to organise and simplify your layout, eg if you have a row of buttons across the bottom its easier to put a panel at the bottom then put the buttons in the panel. If panels don't help the layout then don't use them.

ps You shouldn't extend JFrame unless you have some particular reason to do so. You can just have a class that one or more JFrames as instance variables and that class can populate them, respond to them, make them visible or invisible etc

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It would help if you gave some better info than "it wont let me".
If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be. If you don't know how to code something explain exactly what it is.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent!
A while ago I did a test/demo program using the simple architecture above. It has hundreds of balls bouncing around and bouncing off each other, with full spherical bounce calculations and gravity, plus animated gifs moving across the screen at the same time and assorted bricks and balloons crashing/floating around. Runs smoothly at 30 fps on a five year old ordinary PC.
Back in the days of Java 1.4 that was the first version where animated games became possible, but only if you spent a lot of time doing your own buffering etc.. Unfortunately there are still many tutorials on the web that date back to those days. Java 1.5 gave us double buffering and a whole load of internal optimisations, and life has been a lot simpler ever since.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like you are fighting the Java graphics system rather than working with it.
Before getting into buffer strategies, just try the recommended (flicker free) mechanism...
Override paintComponent for a JPanel and do all your drawing there or called from there
Update the game state in a non-Swing thread
Call repaint() at the required intervals
Let Swing's double buffering handle the flicker

The link I gave you earlier has some other hints to optimise performance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Swing components are double buffered by default, unlike the old AWT components that make you do it. If you place a canvas with some more complex buffering strategy inside a double buffered Swing parent it's far from clear what your custom buffering will achieve anyway.

If you are updating the painting of a JPanel you do not need to call revalidate, a simple call to repaint will do, so that's no different from canvas.
Have a look at Oracle's write up on AWT vs Swing. http://www.oracle.com/technetwork/java/painting-140037.html

So unless you are doing something very special in a difficult hardware environment it's safe to say that using obsolete AWT components instead of their superior Swing replacements is always a mistake, and mixing them is a bigger mistake.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Useful in what ways?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why? Canvas is an old AWT component that you would use in an old AWT Frame. Forget it, and use a JPanel.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that's possible, but it's a really bad idea. For a start you are replacing one simple short safe piece of code with two lines of detailed fragile code. Duh!
The whole idea is that each object is in the best position to know about all its own fields etc, so it's uniquely best placed to provide a printable version of itself. It's a universal Java idea that you can just print any object without having to know about its details.
Suppose one day someone enhances the Patient class to include (eg) gender. That same person is responsible for updating toString to match. Any code that prints a Patient will continue to work, and will display all the latest data.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, toString is called when you print or println.
If you override the 'garbage' Object toString then you have to return a String. How you construct that String is entirely up to you - string concatenation, format or whatever.
When you print a Patient object, print calls its toString. Now if that toString includes a Date its up to that toString method to ensure that the Date is converted to a suitable String.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Every class in Java has a toString method. It's defined in the Object class so everyone inherits it if they don't override it. It returns the name of the class followed by the instance's hash (that's the garbage you referred to). You are right, print always calls the toString method of anything you print. But when you use format you have to ensure that the values you provide match the format spec.
Using a reference type, which includes String remember!, is perfectly OK and normal for.a getter

Public Date getDateOfBirth() ...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Make the superclass default constructor private and provide a public constructor that takes a parameter ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's not exactly what I would think of as MVC either. But it does look like good architecture, regardless of what you call it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

replaceAll does not modify the current string, it returns a new string, which you have ignored. Your regex seems too complicated. After the replacement you just need to split on spaces.
Here's a simple example

    String s = "a b-c, d;  e";
    // replace any of , ; - with spaces...
    s = s.replaceAll ( "[,;-]" , " " );
    // split on one or more white space characters...
    System.out.println(Arrays.toString(s.split("\\s+")));
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How about replacing all those difficult characters with blanks befor splitting the sentence?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

the problem is with the variable you call "total".
Your loop sets total to 15/15 then 15/14, then 5/13 etc - which in integer arithmetic keeps giving you 1's
I don't undersatnd what that variable is for anyway. In pseudo-code all you need is

for possible = 2 to (number-1) // 1 and number don't count as  factors
   if  (number modulo possible) == 0 then possible is a factor
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I wish you success, but I don't think there is anything else I can do to help you.
Maybe someone else can be more useful to you?
Good luck
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes yes yes. You said that many times already. Tell us something new!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That fragment of code opens a socket connection, sends some text, and disconnects. What did you expect?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's an old Apple thing - modal means the computer is telling the user what they can or can't do, whereas, as far as is possible, the user should be telling the computer what they want, whenever they want.
That, if course, is a fantasy about some ideal thing that we don't know how to do yet, so in reality modals are often necessary and frequently useful.
J
:)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Good point. In Java all gui activity is serialised on a single thread, but I don't know aboutC#.
If it is a problem then the simplest solution is probably to force the data class updates to happen serially by locking its update methods on a single lock object. More complex problems such as two windows performing incremental updates or concurrent input to the same items are probably a symptom of a UI design issue because no matter what you do the user is going to confused or disappointed. Much as I try to avoid modal windows, there's a lot to be said for making some input forms modal with respect to any other windows that can update the same items.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

And there's more!
Your data class can raise events whenever it's data changes and your windows can have handlers/listeners for those events so that when one window changes the data all the others can automatically update themselves. Amaze your friends.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That is ok for a simple case. If you have multiple windows that may be called in various sequences, and complex data to be shared, then the standard approach is to create a new class that holds all the data and make that class available to all windows.

ddanbe commented: True indeed. +15
Santanu.Das commented: Right solution +6
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

VisualVM will let you see what objects the VM has stored in its (heap) memory.
https://visualvm.java.net/gettingstarted.html

But it doesn't give you any access to actual memory locations. It just lists objects according to their class or size or whatever.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that code creates a new String object with the value "hello" and it also creates a new variable called hello that contains a reference to the String. The String will be created in the JVM's heap(s) somewhere. The variable may be in the heap (if it is a static or instance variable) or on the stack if it is local.

The language definition refers to references, not to addresses. Yes, the simplest sort of reference is just the address of the object being referred to. But that's not necessarily so and there are good reasons why you might do it differently (eg use an offset into a table of object addresses, which then allows you easily to move objects to optimise memory usage). The design of the Java language is very careful never to say anything about actual memory addresses.

Bottom line: in some implementations you may be able to interpret the default hash as an object address, but this is not required or guaranteed. In general there is no way to get an object's actual memory address.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's Oracles brief operating instructions for jconsole
https://docs.oracle.com/javase/tutorial/jmx/overview/javavm.html

... but what do you really want/need to know? I can't imagine that a memory address is going to be useful to you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What level of phone - just a voice'n'texts basic model, or an internet-connected smartphone?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Again, Yes. You said that already. That's what I was answering.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's a complete example in the introductory description in the standard API documentation

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's nothing in that code that will behave differently between upper and lower case letters, other than Collections.sort will sort upper case letters before lower case (ASCII/Unicode order).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You split each line into words (line 19) but then you do nothing with that, you just add the complete line to your ArrayList.
But anyway, the only way I can see for you to get gibberish output like that is if your default charset doesn't match the actual file's contents.

ps Your code would be a lot easier to read without all those pointless comments. You should assume that the reader knows Java syntax, so all you need to explain is overall what you are trying to achieve in each code block. There is not a single comment in all that code that tells the reader anything they can't see in the code iteself, except where they are actually wrong, eg
if (!file.exists()) { //creates a loop ... (no, it doesn't create a loop)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The result could be as long as the longer input aray, or 1 digit longer than that (if you end up witha carry), so maybe the easiest way is to set up 3 arrays of length (longest input +1), read the inputs into two of them and calc the result into the third. With all the arrays the same length you will have no problems with indexes or loops.

ps: a boolean expression evaluates to true or false, so some of your code is highly redundant, eg consider
if(isValid(temp) == true)
isValid(temp) returns true or false, so the == true does exactly nothing. All you need is
if (isValid(temp))

Similarly, consider

        if(val == 0 || val == 1)
            return true;
        else
            return false;

now val == 0 || val == 1 evaluates to true or false, so all you need is

return  val == 0 || val == 1;
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"ascending" is defined by the compareTo method.
a,b is ascending iff a.compareTo(b) >0

If you reverse the test inside the compareTo so that a.compareTo(b) <0 then you have just definied a,b as a descending sequence.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you are supoosed to be swapping elements in the list then I would expect to see two calls to set(...)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes. You said that already. That's what I was answering.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The 4 numbers are your xPressed, yPressed, xReleased, yReleased. They are all you need to draw the same line(s) in another window.

You have a server program running or yur PC. Someone starts a client program an their machine. It connects to your server via a Socket. Then you can send info back & forth. See some tutorials, eg: https://docs.oracle.com/javase/tutorial/networking/sockets/

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you want to set the ith element of a List to some value, the correct method call is like this:
myList.set(i, someValue);

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

24: compareTo returns an int -1, 0, or +1, but not a boolean
31/32: you can't have method calls like that on the LHS of an assignment. You probably want to use set to change the contents of the list

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes.
All the stuff I suggested before is about drawing on another window applies perfectly in that case.
First, get it working with two windows in the same program, using the method I suggested.
When that works you will see that just 4 numbers need to be passed from the "mouse" window to the "slave" window each time the mouse is dragged. You can then pass those numbers via a Socket connection between different programs or computers. There are lots of tutorials on how to use Sockets, but please get it working with two windows in the smae program first.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Earlier you said "it did catch the mouseevents ... it doesn't draw anything on the other window" - which is the problem I was addressing. But now it seems you want to draw on the same window as the one where you catch the mouse events?

In that case it would help if you added the new2 object to a JFrame so it is visible on-screen.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry necrovore, that would add an asterisk to every line. Between lines 19 and 20 would be better.

The problem is that on the last line the loop lines 6-9 is not executed, so the variable a does not get set, so it keeps its previous value of 1, which is why you only get 9 asterisks. The correct fix is to make sure a is zero for the last line. I'll leave you to think about that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

repaint() tells Swing that you want to update your window. Swing adds that request to it's queue of work and when it gets to the top of the queue it calls your paint method where you can re-draw the window's contents.

I'm worried that this conversation is going a bit random. In particular I'm still a bit puzzled why you want to drag the mouse in one wondow but draw in the other window. Is that right?
Can you please explain again exactly what you want to happen in each window.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Start by reading the instructions...
"store the values in Card object with the attributes symbol and number."

... but also, when you need to add a new key/value pair you use the unhelpfully-named array for the value. You keep adding numbers to that, so (1) it keeps getting bigger and (2) all the keys point to exactly that same ArrayList.

... and while I'm on the subject of variables names, you have three variables called card in just a 41 line source file, and none of them represents a Card. One is a CollectCard object (what happened to class names being nouns?), one is a Map, and one is a Map.Entry. Now, what are the chances of getting confused - I know I was.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

its something along the lines of

private int startX, startY, etc
public void updateDrawing(int newStartX, int newStartY ... etc) {
   startX = newStartX;
   etc
   repaint();
}

then you can call that method from the second class to update the first class's values and make it re-draw the line

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

WHich part did you not understand?
I'm not going to give you the code, but I will try to explain what code you need to write.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK! You didn't explain that.
So
the listeners have to be added to the window where you are clicking/dragging the mouse. What you ned to do is then to pass the info back to the first window.
Here's a good way to do this that can be applied in many other examples as well

create a public method in the first class, like

public void updateDrawing(parameters for pressed and draggged coordinates) {
   take a copy of the parameter values
   call repaint
}

in the first class override paint and draw the line according to the values

in the second class's mouse listeners, whenever the values change, call the first window's updateDrawing method. To do that you will need areference to the first window. The best way is to pass that as a parameter to the seocnd window's constructor.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I have not tried your code, but it looks OK. The way you add the mouse listeners in new2 looks perfectly normal.

Why do you think you are doing something wrong? Do you have error messgaes? Doesn't it work as you expected?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

LInes 46,47
The only place you can draw to the Graphics is inside the paint method (or paintComponent, which would be better). Swing is double-buffered and anything you draw in other methods is most likely never going to de used.

f.getContentPane().add(ok);
you haven't needed to do this for about 10 years. f.add(ok); is enough.
Similarly, you use an anonymous inner class for your action listener. In modern Javas a lambda is much simpler to code and to read, and less prone to syntax errors.
Are you sure you are using an up-to-date source of info, such as Oracle's own tutorials?

Anyway - what exactly was your question?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your understanding is wrong in that the key/value pair is not replaced, just the value. The API doc says
"If the map previously contained a mapping for the key, the old value is replaced."
Your equals method defines two Accounts as being the same if the name fields are equal, so although you have two different Accounts the second is seen as the same key. Thus the key is left unchanged (ie the first Account) and the value is updated.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Same problem as in your previous post: "sum" is not a Java type, nor a class.
Line 8 also has invalid syntax for calling a method