JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

We may have had our disagreements in the past, but this is one thing I wouldn’t wish on my worst enemy. With all my heart I wish you the strength to endure what must be endured.
And, who knows, modern medicine is improving exponentially. There’s always hope.
James

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So if I use Excel to create an interesting spreadsheet Microsoft should be the copyright owner?

Dani commented: No, but I think Microsoft should own the magic behind its macros. The end user is just plugging input into them and MS decides what the output is. +34
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What are the 2 main types of software?

Software with known bugs, and software with bugs we don’t know about.

Reverend Jim commented: Spot on. +15
rproffitt commented: Nailed it. +17
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

My mistake (typing while watching to), you need to declare String line before the while loop.

epsilonb commented: Well either way I need to fix things up make it work as should and will refactor after that :P +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just FYI the “standard” way to code this (saving duplicated reads and tests) is simply

while ((String line = br.readLine()) != null) {
   // process line
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Did you set the JFrame as the owner of the dialogs when creating them?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Some countries including France where I live use a . between thousands and a , before the decimals. Java localisation will automatically default to display numbers in the correct format for the users locale.
You can override this by setting an explicit locale in your program if you want to use the same formatting regardless of local norms.

https://docs.oracle.com/javase/6/docs/api/java/util/Locale.html#setDefault(java.util.Locale)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I asked how we could help. ‘Just complete it’ isn’t help. Instead of getting some points for completing the basic stage you face ZERO points (at best) for cheating. Think again.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

We do try to be helpful here, but it’s hard to see what we could do in this case. What help would you like?

fouad_4 commented: Just completing the project. I prepared the first stage and made all the classes as well as all the relationships between these classes as well as the +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Be realistic. Your tutor expects this to take weeks of work for a team of 2. There’s no possibility of doing anything by ‘tomorrow’. Spend your time thinking about plan B.

fouad_4 commented: I regret it, but I was traveling and I don't know anything about the project. I am surprised that the delivery is this week with discussion as well. I +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don’t know if this helps but…
If your compiler has __STDC_NO_VLA__ = 1 then it doesn’t support variable length arrays (C++ 11)

rproffitt commented: Most likely the issue. I've noticed that beyond the code, I have to know the IDE and compiler. +16
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How can I get this source code?

It's a learning assignment. You get the source code by writing it yourself - that's how you learn.

If you are trying to write it, and you're stuck, explain what you have done so far and what's blocking you. Someone will help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The code you posted looks ok. Maybe the problem is in the GamePanel Class?
Maybe it’s this scenario…
A JPanel class adds some children and that triggers a repaint on the Swing thread. But the child’s constructor is still running on the main thread so it’s possibly not complete when the repaint runs. This is most probable when the child constructor does any I/O.

.UPDATE.

ignore the above. You didn’t call setup for index 30!

Ps. The empty catch block when trying to read files is a really good way to make any problems totally undebuggable.

Justin_44 commented: Thank you so much +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Dissertation helper

Does that mean you help people cheat? Shame on you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I guess you could use Visual Basic for Applications (VBA) in Excel - my Java code is simple enough that you should be able to translate into vba without too much difficulty.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you're still interested...
Here's a little runnable demo I hacked up in Java to show how the recursive solution works:

    int[] values = {1, 2, 9, 3};
    int target = 8;

    tryValue(0, 0);

    // recursive algorithm to try all possible combinations of values and
    // multipliers that add up to the target.

    // LIFO history of steps leading to the current one...
    Deque<String> solution = new LinkedList<>();

    void tryValue(int valueIndex, int runningTotal) {

        if (valueIndex >= values.length) {
            return; // tried every value
        }

        int value = values[valueIndex]; // just for convenience
        int multiplier = 0;
        int newTotal;

        // keep trying multiples of this value until it exceeds taget...
        while ((newTotal = runningTotal + value * multiplier) <= target) {
            solution.push(multiplier + "x" + value);
            if (newTotal == target) {
                System.out.println(solution + " = " + target);
            } else {
                // try adding (multiples of) the next value....
                tryValue(valueIndex + 1, newTotal);
            }
            solution.pop();
            multiplier++;
        }

    }
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I doubt that you will find an off-the-shelf solution, nor will you find an easy one.
You need to traverse a tree with nodes for each value x1, x2, x3 etc.
Looks like a recursive algorithm.
How much do you really want a solution?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Ok. You have started, so that’s good. How far did you get and what’s stopping you from progressing further?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You write a Java program as specified in the assignment. Have you even tried to start writing?

cyrennex commented: yes +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

And another thing…
If the if block exits, then the “else” following it is redundant.

Dani commented: True +34
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

First: you are still using Class.forName. It’s obsolete. See my previous post on the subject.
Second: looks like the Derby jars are not in the class path on the real machine.

Saboor880 commented: I removed the Class.forName method and it gave me Error: No suitable driver. +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why those old old versions? I'm not surprised that they're not running on Windows 10.

The current Netbeans is version 15, not 9
The current JDK is version 19, not 9. (Although you may prefer version 17 which is the current LTS version).
All those are Windows 10 compatible.

jwenting commented: that said, many if not most companies and agencies you'll be dealing with as a professional are still on Java 8 or at most 11 +16
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You’ll find what you need in Java’s Random class (Java.util.Random)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Me too. Over 20 devices connected to the wifi, many with no keyboard and tortuous setup procedures (not just printer and tv, but dishwasher, oven, Tesla charger etc). No way am I ever going to change the SSID or password. I know that’s crazy, but it’s not my fault.

rproffitt commented: We can do this the hard way or the smart way! +16
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here’s a really neat algorithm:
Write a small method to sort the letters of a word into alphabetical order (let’s call it alpha).
Read the dictionary file creating a map with alpha(word) as the key and a list of corresponding words as the value.
Now you can look up all the matches for some anagram by getting the value for key alpha(anagram).

With the official Scrabble word list of 230k words building the map takes up to 1 second and occupies 10-20 Mbytes, but subsequent lookups are instantaneous.

I can post the code if anyone is interested.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think the point is that when trying out ideas in Java you sometimes need something to deliver done html to a browser. You don’t need to set up and configure a full-feature server, just do something very very basic with no question of using it in a live production environment.
That’s why it eventually got included in the jdk.
It is, by the way, a really good app to write to learn about server sockets etc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That’s a great contribution. Thanks.
I think most of us have written something like that for our private libraries, and as of Java 18 it seems people have noticed.
“Text blocks” allow you to code literal multi-line formatted text such as your html in a straightforward way.
The jdk now comes with a simple web server as standard.

jnbgames.dev commented: I agree with you! I will try text blocks, they will be much helpful in this case! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You almost never see a redundant this. in standard Java. A programmer seeing it would immediately assume that there was something unusual going on that the writer wanted to draw his attention to.
Unnecessary use of this. is confusing for beginners. It’s redundant in the same toxic way that
<some Boolean expression>==true
is redundant. (Another common beginner mistake that follows from a fundamental misunderstanding of the if statement.)

I suspect OP has spurious this. because he is unclear on this simple rule:
in Java you refer to instance variables by their simple unqualified name (unless it’s necessary to do otherwise).
It’s not a shortcut, it’s the language. Adding unnecessary qualifiers is confusing and bad practice.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

this.length vs length
Dani: you only need this when you want to refer to the instance variable and its name has been hidden by a parameter of the same name. The two-args constructor is the classic example.
Every other use of this in your code is redundant, and stylistically very odd.
OP’s 0 args constructor is correct as written.

The computeArea doesn’t need the area variable. All it needs is
return length*width;

It’s worth explaining why the area must be computed in the getter and not earlier: just think what happens when you change the length or width.

Ps; current Java thinking would be to make this class immutable (thread safety etc) and create a new instance rather than mutating. It’s also missing toString, hashCode, equals. Declaring it as a record rather than an ordinary class gives you all those for free.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You haven’t asked any questions, but I guess you want to know what’s wrong with that code…
Using upper case where lower case is needed, and vice-versa
Missing open and close parentheses
Missing semicolons
Missing quote marks on strings
(Etc)
Programming languages are totally rigid about stuff like that. There’s no flexibility.

Ps. Next time start your own thread, don’t hijack an old one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If PHP works like Java then all the initialisation of the parent is completed before the initialisations of the child, so the parent's constructor will be executed before the Childs initialisation, and so you will see "foo". If you print the variable after construction is complete then the Bar instance should show "bar".

In any case that looks like a shadowing situation, so Foo's VAR is hidden in Bar

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why promote the use of DriverManager.getConnection when, to quote Oracle's documentation, "a DataSource object is the preferred means of getting a connection." as of Java 1.4?

dimitrilc commented: ... +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have created a little class to hold the item names and prices. That's what any Java programmer would instinctively do. The problem is that the specification you have been given tells you to create two parallel arrays. Presumably you teacher is assuming that you haven't learned about classes yet?
In any case, no matter how stupid the spec is, you have to write what you are asked to write if you want to get a good grade.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Build the process with ProcessBuilder. Start the process. Star at 30 minute Timer. Get the available results as they are written to the process' output stream (may need to code the script to display results as they are created). When the Timer fires terminateForcibly() the process.

Programmer18 commented: Thank you. Can I have one example for this approach to understand correctly in order to decide whether I can use this approach. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please let us know the email address for your teacher so we can submit the code directly to him/her and save you all that tedious copy/pasting. I'm sure you'll get an A+ for finding such a cunning way to handle the assignment.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

14.31 has number 1 on the its integer side and a 4 on its decimal side, hence its a variant of 1.4.

???

Bright_5 commented: That was a typo. I meant to say 14.31 has a 4 on its integer part and a 1 on its decimal side. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You didn't say anything about the error. Is it line 38 - invalid syntax?

N_765 commented: Can't get the comment to work sorry +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't understand the need for both line 7 and line 8 but anyway...
In that code the random number generation is not significant to the execution speed; it's utterly trivial.
Whatever is slowing down your code, it's not random()

FYI my Mac mini will generate just under 50 million random numbers per second using a single thread, an average of 21 nanoSeconds per call.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unfortunately, the FPS was even lower, at about 5.

That’s inevitable. The overheads of maintaining the pool greatly exceed the cost of creating each random number. That approach is not going to work for you.

archers shoot more often…

For each type of soldier define a mean time to fire (some number of seconds). Then in your main update do something like (pseudo-code)

for each soldier in the game
   if random.random() < 1/(soldier.timeToFire*frameRate)
      soldier.fireWeapon

That’s just one call to random per soldier, so fast you won’t be able to measure it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sounds like you had failed to provide a meaningful toString() method for the MArry class, so it's just inheriting the default toString() from Object, which just returns the object's hash, which is typically it's memory address.
Overide public String toString() in your MArry class to return a String that represents the contents of that class.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Time for a reality check.
Generating a pseudo random number is one multiplication, one addition, one modulo.
It’s a fast as a function can be.
You can’t make it faster by adding the overhead of storing and retrieving values. The use of sleep to synchronise around an empty pool will utterly destroy the performance when large quantities of values are needed quickly; that needs proper synchronisation which will bring its own overheads. Yes, you can potentially gain 2x performance by using two cores concurrently, but I’m pretty sure the overheads will be greater than 2x.

Pritaeas’ original suggestion was to pre-compute a pool of numbers. The o.p. Wants 75,000,000 numbers every 16 mSec. So a pre-computed pool of (say) a billion numbers (4 GB minimum) will last less than 1/4 second. After that all you have is overhead slowing things down.

The real question is why does o.p. Think he needs 75,000,000 random numbers? That’s almost certainly based on some kind of misconception, and that’s what we should be addressing.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

With 5000 soldiers that should be 5000 random numbers. The 1/15000 chance requires just one random number.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The last time you posted a data file lines 2 and 3 were the same. If the user/pw matched those lines you would get two Booking forms.
Lines 55-65 should really be replaced by a method call, eg
isLoginSuccess = isValidUSerAndPassword(user, pass);
.... and refactor the user/pw validation code out of the UI, because deep application logic like that does not belong in the user interface code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You call the constructor for the new frame. That creates the frame but it does not display it. You also need to call setVisible(true) on the new frame. See line 446 for example.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Obviously something going wrong with the split because it's returning an array of length 1.
You are splitting on a single space but the data seems to contain multiple spaces between the values. That should give you extra empty strings in the array, not a 1 element array. Maybe there a tab character between the fields in the data file? (In which case split on tab, not space).
Have a look at what's happening by adding a print of the split result
System.out.println(Arrays.toString(line.split(" ")));

JC

ps: your English is good enough - no problem. :)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ps: To anyone else thinking of asking a question like this, please note how Wafflez made it easy to get a good response:
Wafflez posted all the relevant code, properly formatted
Wafflez posted the relevant data file
Wafflez posted the complete error message
Wafflez used variable names that made the code easy to read and understand

read it and learn.
J

Wafflez commented: Thank you sorry for my bad english +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java arrays are zero-based, so the two array elements created by line.split are [0] and [1], not [1] and [2]

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Despite your variable and class names seemingly chosen to make this as hard as possible...

It looks like the server may have closed the connection before the client gets to read a whole line. You don't say whether this happens on the first word or a subsequent one. Some more debug info is needed here. eg trace every string being sent or received at both ends, trace when the loops are exited etc

mihailbog245 commented: I found the solution . First of all I didn't look carefully in text file and I put words in text file in a wrong way. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to share the exact details of the socket exception

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I've used Plex for years with servers on Mac, M1 Mac, Windows, QNAP and Synology; clients on Mac, IOS, iPadOS (including remote access over internet), Apple TV, Chromecast, LG Television. Metadata, subtitles etc all solid. And it's free.
What more are you looking for?

lewashby commented: I've had issued with plex that daniwebs comments don't give me enough characters for. And I don't like Plex having write access to my media. +6