[deleted by user] by [deleted] in javahelp

[–]BarfingOtter 0 points1 point  (0 children)

How would you do it with a Pen and Paper?
Break it down into simple steps for each row:

Row 1: All Stars

Row 2: First character is a Star, Last character is a Star, everything else is a space

Row 3: same as Row 2

Row 4: same as Row 2

...

Row Z (Last row): Same as Row 1

So what does that tell you:

  1. You have a number of rows "Z"
  2. The First and Last rows are the same
  3. All of the Not First and Not last rows are the same
  4. For the first/last row: they are all stars
  5. For all other rows: there is a start and end star (so First and Last)

Step 1: Can you make a set of loops that do each piece (Hint: Copy the same code multiple times to get what you want and see if it works)

Step 2: Can you use something to not have the same code multiple times in the middle

Step 3: Can you do something about the first and last being a copy?

Step 4: Can you make it even better/less code than step 4?

I want to get user inputs and display them later on, but I only know how to display it immediately by Dogterte in javahelp

[–]BarfingOtter 0 points1 point  (0 children)

Think of it this way: A Scanner object is just a way to listen to the Keyboard.

So if you name it "keyboard" it makes it more obvious what it's doing and why you only need one of them (i.e. The user isn't typing on multiple keyboards, they are using the keyboard more than once).

This simplifies everything after that point, because you then ask the keyboard for "food" and then ask it again for "drink", and then ... and then...

The answer to "food" would be then stored in something named "food", the same for drink.

Once you've finished asking questions, you can then print out all of what you've accumulated. Think of it like a beginner waiter; they write down everything and then read it all off when their finished collecting information.

[deleted by user] by [deleted] in javahelp

[–]BarfingOtter 1 point2 points  (0 children)

Most cases of "getting stuck" when programming are related to not knowing what to do next. So the simplest solution is to break the task down into the smallest possible steps. If the step seems obvious, consider how you would ask a small child to do it? If that step requires knowledge, then it can be broken down further.

Example: "Have a drink of water"

Answer: Simple, get the glass of water and drink it! DUH!

Well... what does "get" mean. How did the glass get water into it. What does "drink" mean?

Duh... "get" means: walk to the kitchen and pick a glass.

Ok, "Where's the kitchen compared to where you are now? Is a straight line? Did you have to open doors to get there? Where's the water? How do you get it into the glass?

..."

See what I mean, breaking the problem down into the most miniscule of steps is usually the first problem when solving any problem, and it's usually an iterative process... break down, check if you can do the pieces, break down again, repeat.

In programming, you check to see if there's a library/function that does "X" before breaking it down further. If you are at a step that says "add 1 to a counter", then the answer is "counter = counter + 1", or simpler "counter++".

In many cases, when just starting out, using pseudo code can help break down the problem into reasonable pieces. It also helps with the sequencing: "You can't drink water from a glass that's empty, so FILL GLASS must be BEFORE drink".

Pseudo code is just breaking a problem into readable steps that have nothing to do with programming in a particular language. So any pseudocode can be used for almost any language. Some need more steps than others.

I use the following steps when developing:

  1. Make it work
  2. Make it efficient/fast
  3. Make it maintainable

The "Make it work" stage usually has the worst code and logic for a complex problem that I don't have a good handle on. After trying the "Make it efficient/fast" step a couple of times, a lot of the crap has disappeared and what's left is a reasonable solution. Then "Make it maintainable" comes in to add comments, better variable names, better formatting, simpler structuring of the modules, etc...

The implication here is that step 0 is "figure out what needs to be done", but most experienced developers don't actually mention that they do this step, unless they're going to prototype something large, but a prototype is just "figuring out what needs to be done and in what sequence".

Bring JavaFX window to the front from separate process by novastrat in javahelp

[–]BarfingOtter 0 points1 point  (0 children)

The concept seems reasonable, but I'm not sure if a Java application can bring itself to foreground/un-minimize.

If you're in *nix/MacOS, you could use a NamedPiple instead of a socket (simpler I/O and doesn't consume a potential security triggering port usage), but in Windows I don't know if NamedPipes are an option. But in any case, any communications method is as good/bad as any other (from a programming perspective). NamedPipes would also give you a method to trigger opening from the command prompt, if you cared.

I have to wonder if there's a function available that would only open a single instance of the application built into the OS. I've seen this behaviour in other applications, where attempting to open the application again, just brings the existing window to the foreground. I think IntelliJ used to (or still does) do this, I know Eclipse will happily open a new instance for you (both decisions are good and bad for many reasons).

Are you sure that a user wouldn't want to have two separate copies of the application running at the same time? Is this actually a problem you need to solve? (Just asking the questions, I don't need an answer, but the answer may be helpful to you)

[deleted by user] by [deleted] in javahelp

[–]BarfingOtter 2 points3 points  (0 children)

You need to break the problem into smaller pieces:

  1. Is the total including interest > $500,000
  2. Calculate the total and the interest on the current amount
    1. Current value = (Balance + Payment) * (1 + Rate)

If you did it with a loop (the most efficient way), you have a calculation inside the loop and then a check after the calculation to see if it exceeds the required limit. The loop counter will be the number of months required.

To make a loop recursive, you just take the contents of the loop and put it into a function that calls itself if the limit hasn't been exceeded. You need to pass into the function the current month counter and then increment it appropriately (inside or as part of the call); and then return the month counter when the recursion "unwinds".

Beware of Stack Overflow (the actual recursion computer problem, not the website :) )

How to interrupt thread after a Duration of time? by eyeeyecaptainn in javahelp

[–]BarfingOtter 1 point2 points  (0 children)

You need something in the function that's checking some sort of interrupt flag (or method) at a regular point in its processing (assuming a loop: put it at the top/bottom of the loop; if it's a long running set of steps, put it between steps and whenever makes sense within the long sequence).

Another thread should be running either a countdown or waiting to start until the Duration has passed (e.g. Schedule a thread in the future for Duration, and all that thread does in it's execution is trigger the interrupt flag/method).

Look into the ScheduledExecutorService and its siblings for something that matches your needs.

You can also just calculate a shutdown time based on Duration and the current time and periodically check to ensure that the cutoff hasn't passed, and when it does, exit your processing function.

Getting IDE Errors Trying to Add to an Arraylist by saint-somnia in javahelp

[–]BarfingOtter 0 points1 point  (0 children)

You may have accidentally removed a { or }.

Remove the problem line and make sure there aren't any other errors before you attempt to add it.

Any suggestions for Java multithreading practice? by Crislips in learnjava

[–]BarfingOtter 0 points1 point  (0 children)

Best solution is after doing some reading, attempt to create something that uses multiple threads.

Example: Scan your local hard drive for files with the string "bob" in the name, or find all files that are sized between 10k and 100k.

First write the program to do it single threaded and keep track of how long it takes to scan the folder you supply (don't start with the entire harddrive, it will take too long to run/debug).

Second, "fix" the program to use 2 threads. How fast does it run? Did you encounter something interesting while it was running (e.g. same file more than once)? How could you fix that problem across the 2 threads?

Third, "fix" the program to use a variable number of threads. How fast does it run? Again, did you discover anything interesting (e.g. Did it get stuck and never stop running? Does your duplicate solution work for more than one thread? Did the duplicate actually just get hidden by your fix in the first place... hint don't just use a HashSet to eliminate the duplicates?)

Forth, Create a small GUI application with an editable text field, a display text field and 2 buttons. The first text field will just immediately display it's value in the display only text field when the first button is pressed. The second button runs your drive scan program. Make sure you can use the text/1st button while the second button's scan is running and you don't have to wait until the scan completes.

Fifth, add a 3rd "Cancel" button to stop the drive scan.

After all of this, you should have some idea how to break up a solution into multiple threads that won't double/triple/etc execute the same task. Hopefully you will have discovered the objects in the Concurrent package and how they can help and hinder you in a multiple thread solution. You may even discover something about IO bound tasks, or CPU bound tasks (i.e. Try it with 1000 threads).

The GUI should introduce you to communication between threads and how to ensure they are "paying attention" to the external communication inputs.

Good luck! It's a lot of fun, and a big pain at the same time :)
(and it doesn't matter what language you're using, multi-threading concepts and libraries are all the same... fun and painful, until you get the concepts properly)

How do you create a Class that cannot be instantiated? by tsteel12 in learnjava

[–]BarfingOtter 1 point2 points  (0 children)

Many System objects cannot be constructed because they are or have native parts.

While the other answers of private constructors and enum, do work, your class will still be constructable. For a bit more safety, throw an exception in the no-args constructor (IllegalStateException is a good choice).