Zachary Griggs

+ Follow
since Apr 29, 2016
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Zachary Griggs

In Java, String comparison must be done with the .equals method rather than the == operator, because the == operator compares addresses only, not contents

Also, it doesn't look like you are accepting an input value for "amPm" anywhere as you are in this C++ code:

In Java, the main method has no return value ("void"), unlike C++ where you return an int.
3 years ago
Hi, welcome to the Ranch.

Game programming is quite complex and I'm by no means an expert, though I have programmed games in my free time. Here's a high-level overview:

What I'd recommend is using JavaFX as a base, with a Canvas to display the game screen. Create a simple GameObject which can render itself on a Canvas (retrieve the GraphicsContext2D and drawImage), and a List of GameObjects which belong to the game. You can use a JavaFX based timer which asks each GameObject to render itself and then advances the game by a frame (which can mean, for example, moving an object if the player is holding an arrow key). You can add event handlers to the game canvas to detect when the user does something like press the right/left key, and then propagate that message to all game objects. If you do something like this, you'll be able to create a simple Java-based game and you can make it more complex from there - for example JSON definition files for maps, switching between sprites to make animations, etc. For importing sprites, find some freeware ones online and add them to your Resources folder. Retrieve the resource as a Stream, and then pass that to the drawImage method on GraphicsContext2D.

Though really the most important thing is to just try it out yourself. Break apart what you want to do into simple tasks, and set some realistic goals. For example when I started making a game, my tasks were like this:
1) Make a JavaFX project
2) Add a game canvas to the project
3) Import a freeware sprite into the game
4) Render a sprite on the game window
5) Detect key presses on the canvas
6) Make the arrow keys move the player character
7) Add gravity to the player character
8) Add tiles to the game
.. and just build from there.
3 years ago
Hi, welcome to the Ranch!

This forum actually doesn't use ``` for code tags, rather you can use the [code] tag. I'd recommend editing your post and wrapping it in the tag to make it a bit easier to read.

Conditions is to have the battleships spaced one square apart horizontally and vertically on my 5x5 grid

This sounds like a requirement around the display to the user, not around the actual underlying array. Could you post the code where you print the display to the user?

Edit- after re-reading I may have misunderstood. It's saying that there can't be two ships next to each other, correct? If so, I think you may want to change the random ship generation. That is, before you add a ship to that square, ensure the squares around it are empty (i.e. +- 1 on the x and y)
3 years ago

I really need to take the images through URLs and not by storing it in the application, otherwise it will make an application with too much storage volume which will be exhausted on the computer.  

If this is some kind of requirement from a school assignment, that's fine, you have to follow it. But if it's not, I strongly recommend you reconsider. Images are very, very small compared to the amount of storage modern computers have. You can even store videos and music in Java resources (and have to, for applications like video games) without a problem. In fact, downloading from online is really inefficient because not only does that image get stored in memory anyways, it'll have to get downloaded from the Internet, which means establishing a connection, streaming the data over that connection, etc. I don't see a strong reason to do this for this use case.

Now for debugging - try to narrow down the problem
- Do other images show and just this one does not show?
- Does it throw an exception or simply display nothing?
- Try analyzing the ImageView in the Debugger. Does the internal Image show up there, is it an empty image, does it have some invalid state? Is the size all zero or something like that?
3 years ago

So the first thing I'd recommend is formatting your code. Any modern IDE will allow you to do that with one keypress. Specifically, it's pretty tough to read those if statements when they're formatted like that.

Second - please describe the issue you're having in detail when asking a question, since it'll help other people help you. Based on the code, I'm guessing the problem is that it says all the temperature readings are zero no matter what you enter.

For this line:

What data type is c?
What data type is "F"?
(edit) Paul beat me to it here. Yeah, these are not the same data type. I actually missed that it used "=" instead of "==", so it just won't even compile as it is now.

I strongly advise splitting up these lines:

Using && to connect multiple different statements may work (though I don't know if it does) but it's definitely not good style. It makes those lines much harder to read - and saving lines of code is never an advantage if it hurts readability. There's actually an issue with the line above, and if you split it up into multiple lines rather than using && between them, you'll likely be able to see what it is.
3 years ago
I would recommend looping through the children on the VBox, or storing the children you want to fade in a List and then looping through that List. As far as I know, JavaFX does not provide an immediate method to fade all items in a Pane, without iterating through the elements you want to fade.
3 years ago
I think this kind of thing will depend on the specific company that has this job. Perhaps one is more of a hands-on coding leader, while one is more of a management position - but the only way to know for sure is to ask then company what the responsibilities/expectations of the job are.
3 years ago
How are you compiling this, and how are you referencing the main class?

If you're using a package manager like Maven, you can have Maven specify the main class like this:

If you're using built-in IntelliJ functions to specify the main class, you'll have to update those whenever you change the pathname of the main class

If using raw javac commands - ensure that you're typing the fully qualified classname. To do this, type "packageName.mainClass" for example.
3 years ago
There are probably plenty of helper methods in Apache Commons to do this for you, but it's actually a good exercise to write this kind of thing from scratch once.

This is what I would do, step by step:
- Create an empty StringBuilder
- Retrieve the KeySet from the Map
- Loop through the KeySet (for each key)
- Retrieve the value for that key
- Append the value onto the StringBuilder
- After the loop, return the StringBuilder.toString()
3 years ago
Hey, welcome to the Ranch!

Is there a reason you're referencing an image on an external site (for example does the site change that image and you want the application to change too)? If not, I'd recommend downloading the image and packaging it in your application Resources folder, if you have permission to use the image in your application. That'll avoid any sort of networking issues or stability issues with the website.

Other than that: what problem are you having specifically with it?
3 years ago

I think you'll need more TranslateTransitions, where the second transition begins where the first one ends. I don't think those support multiple cycles that begin at different start locations.
3 years ago
Hey, welcome to the Ranch!

The issue causing the exception is that your Group contains "newBox" because of this line:

And you are also trying to add the group to newBox here:

In JavaFX, this sort of circular dependency is not allowed.

However even if you get this fixed, it doesn't look like the code in the m1 action really does anything. It creates a new VBox and Group, but doesn't end up doing anything with them, so you'll never see those items anywhere. I'd recommend not creating new elements, and rather have the elements exist immediately and hide them. Then in the m1 code, you can un-hide them.
3 years ago
So, in "a = ++n", the ++n operator means the ++ takes place first. In other words, it's essentially doing the following: "n++; a = n"
In "b = n++", the ++ happens last. So you can think of it as: "b = n; n++"

I think you got the two operators mixed up.

In practice, these command should probably be split into two when you write code. Saving one line of code isn't worth the confusion caused by things like this!

If you run this program:

vs, this program:

They are functionally equivalent, but the second one is much more clear than the other.
3 years ago
I believe the assumption is that you'd print some more specific error message for what went wrong if you catch a specific exception, for example for NumberFormatException you would say "That's not a number" or something similar. I usually do catch Exception in the bottom-level of the block and print some generic message for them, but catch specific exceptions and give more specific messages for them if I know they could be thrown.

In real life code, I find that people usually just catch Exception.
3 years ago
Which parts do you need help with? What have you tried so far?
3 years ago