Kevin Simonson

Ranch Hand
+ Follow
since Oct 22, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Kevin Simonson

Java class (Math) has a method called (atan()) that takes one (double) as input and outputs the arctangent of that input. That's fine for calculating the angle (in radians) of the line from the origin to the point (3, 7), or to the point (-5, 2); I just call (atan( 7 / 3)) and (atan( 2 / -5)) respectively. But if the points in question are (1, 0) and (-1, 0) then (atan()) would give the same answer for both, and if the points in question are (0, 1) and (0, -1), I'd get a divide by zero exception if I used (atan()) on them. So the people who wrote the (Math) package added another similar method called (atan2()) that takes two doubles. To calculate the angle to (1, 0) I call (atan2( 0, 1)) and get zero, and to calculate the angle to (-1, 0) I call (atan2( 0, -1)) and get pi, exactly like I want. And (atan2( 1, 0)) and (atan2( -1, 0) return half of pi and negative half of pi respectively.

Now I had previously written a method (arcTan()) that took as an argument a (BigDecimal) and returned a (BigDecimal). How can I write a similar method that takes two (BigDecimal)s and returns a (BigDecimal) that works analogously to (atan2())?
2 months ago

Carey Brown wrote:I'd be curious what the following tweaks would do in your environment.


Carey, that made it so my "Emp.Txt" (File) object gave an (exists()) of (true)! That's a much better fix than my kluge, and I've already put it into my tool. Thanks!
2 months ago
Note that the following kluge fixes the problem for me:

I still think the previous behavior makes no sense to have happened.
2 months ago
File Method (listFiles()) Returns a List of (File) Objects that Don't Exist

I built myself a little tool that I've been using for some time, but it had some behavior that adversely affected the way I used it. I isolated that behavior to the following piece of code:

When I compile this program in my "C:\Users\Kevin\Java\Exp" directory and then move down to my "Sub" directory below that, where the only file there is empty file "Emp.Txt" and at the same time have another empty file at "D:\Exp\Sub\Emp.Txt" on my jump drive, I get the following behavior:

I should say I created an alias "j8" that is just "\Langs\Java\8u40-windows-x64\bin\java". I should also say that if I type in "D:" I discover that my current directory on my jump drive is "D:\Exp\Sub".

Why is the (listFiles()) method of my directory's (File) object returning (File) objects that don't exist? Note that the canonical path of the single empty file is the same for each of the two times I execute (DsBug) on my jump drive, and yet the empty file is listed as existing when I type in "D:\Exp\Sub" as my argument to (DsBug), but not existing when I type in simply "D:". It seems to me that if the Java interpreter is able to discover that a directory has a file, it should also be able to realize that that file exists. Can anyone tell me why I'm getting this behavior?
2 months ago
Class (BigDecimal) doesn't have a square root function, so I wrote Java program (Sr) to calculate the square root of a (BigDecimal) using Newton's Method.

If I run this with operand 4 I get:

Variable (potRoot) starts out as (1), and then (qtnt) and (sum) each get calculated correctly. But when I take the average of (1) and (4) to put in (newRoot), that should be (2.5). Why is this program showing that (newRoot) is (3)? Is it rounding (2.5) up to an integer before displaying it? Is there some way to keep it from rounding it to an integer value, so that it just displays (2.5)? If my variables were (double)s, I'm pretty sure (newRoot) would end up being (2.5), so when I see it as always an integer value that's really confusing me.
8 months ago

Ron McLeod wrote:Other than renaming some variables, I ran your code as-is, and it seemed to work as your wanted:

Windows 10 and Java 17.0.7



Ron, I just changed the name of your class from (MyDir) to (McLeod), giving me:

But I get the same results:

My command "j8" is just an alias for "\Langs\Java\8u40-windows-x64\bin\java".
11 months ago
I'm working on a computer with operating system Windows 10 Enterprise. On my D: drive in directory "Bug" I have two files, "Oops.Txt" and "Qf.Txt", of lengths 6 and 49 respectively. When I type in "D:", it takes me directly to that "\Bug" directory. (In other words, when I was last on my D: drive I was in that directory.) When I type "dir D:" I get a listing of those two files, and when I type in "dir D:\Bug" I get an identical listing of those two files.

Now I've written file "MyDir.java" which consists of:

When I run it I get:

Why am I getting zero lengths for those two files when I execute "j8 MyDir D:"? It's getting the two files that are in my "Bug" directory. Why is it getting the two sizes wrong? Is there a way to fix my code so that I can execute "j8 MyDir D:" and get the correct results?
11 months ago

Carey Brown wrote:


Thanks, Carey! This worked just fine.
2 years ago
I've got two ".pdf" files that are precisely the same size. I suspect they may be identical, but I'm not exactly sure. If you have two files of the same format, and you're not exactly sure what the format is, so the files could have any possible binary values in an arbitrarily large number of bytes, is there a way to open each of the files and compare all the bytes one by one, to find out if the files are identical or not?
2 years ago

Rob Camick wrote:

When you do the rotate the string gets drawn down 250 and to the left 250, so effectively it is painted off the panel.


Thanks! I got my code working the way I like it. I really appreciate the help.
2 years ago

Campbell Ritchie wrote:The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.


Actually, let me make it easier. I tried:

And then I executed "java Ritchie", and got a window with a white strip on top with an icon and "Ritchie" on the left, and then underneath it I got an apparently 500 pixel by 500 pixel square that was completely gray; there's no trace of a green "Campbell is brilliant" anywhere near the center of the square where it should have appeared. Once again, what am I doing wrong?
2 years ago

Campbell Ritchie wrote:The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.


Campbell, using what you said I wrote:

Then when I ran it with

I got a large (JFrame) window with "DsgRot Cube_15_10.0" in the upper left corner, but nothing actually appeared in the window; it's all completely white. Any idea what I'm doing wrong?
2 years ago

Carey Brown wrote:Kevin, can you provide a screen snapshot as to how you expect it to look. From your description I'm guessing (?) you want:
A
B
C

and not just ABC rotated 90 degrees.


I don't know how to provide a screen snapshot; I was hoping on using the Java interpreter to display it, and I can't figure out how to do that; I don't know any other way to display it. But in response to your guess, I actually do not want
A
B
C
instead I do want ABC rotated 90 degrees.
2 years ago
If I extend (JPanel) and in my (paintComponent()) method I call (drawString()) on a (Graphics) object, I can pass in a (String) object as my first parameter and cause that (String) object to be printed horizontally in my (JPanel) window. Is there a way to print that (String) object vertically instead of horizontally? I need to switch each of the characters in the (String) object so that they occur one on top of the other instead of horizontally next to each other, and I also need to rotate each of the characters ninety degrees. Does anybody know how to do that?
2 years ago
If I do a Google search on "java escape sequences", I get a number of pages like "https://www.golinuxcloud.com/escape-sequence-in-java" that seem to indicate that there are only eight escape sequences, namely "\t", "\b", "\n", "\r", "\f", "\'", "\"", and "\\". But when I wrote the following code:

it became obvious that I can also have "\uxxxx", where (xxxx) are four hexadecimal digits, and I can also have "\0", "\01", "\012", and "\7"; so the list on that website appears to be incomplete. Can anyone tell me where I can find the complete rules on which escape sequences I can use?
2 years ago