Piet Souris

Bartender
+ Follow
since Mar 08, 2009
Forum Moderator
Piet Souris currently moderates these forums:
Netherlands
Cows and Likes
Cows
Total received
151
In last 30 days
0
Total given
98
Likes
Total received
746
Received in last 30 days
0
Total given
83
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

Well said, Junilu.

I do not have a problem with a topic extending a little (or far) from the original question, but what I often see in these cases is that poor old OP gets completely out of sight, intimidated probably. Yes, it IS a sign that we are very very willing to help, but that may be a meagre comfort...

Me too, mentioning a nice GUI with beautful Buttons and Labels... mea culpa too
1 month ago
What use does the GameObject have?  It exists of two GameObjects, Snake and Food, but what use is the posX and posY fields? For Snake these are unnecessary, and a Food has its own x and y.

Then it could be a Collection of GameObjects, but the Board draws a Snake and a Food literally, so it doesn't use a GameObject as Collection.

Why does the Food class have a field: Snake snake? I did not see any code using this field.

So, at a glance I'd say get rid of the GameObject class, make an interface Drawable, that has a method 'draw' and let Snake and Food implement this interface.

Lastly (for now) don't let the 'draw' method load an image. Load these images at startup.

But I like the idea and it certainly would work. As soon as I have created some images, I'll have a go playing the game. The last time I played Snakes, was it in the 80's or 90's?
1 month ago
Do you mean with: s.nextLine() and s.nextInt() perhaps a file of this form:

?
Beware that "s.nextLine();s.nextInt();s.nextLine();s.nextInt(); ..." is dangerous. Campbell already warned for that.

But if your file is in that form, then your calculation is wrong. If we take as average for grade A a score of 95, then the sum goes up by 23 * 95, and the number by 23. You cannot calculate an exact average with these data.

If your file is in the form:

then you code is okay (again, beware Campbells warning), but that file contains a lot of redundant information.
1 month ago
This topic has gone out of hand. It was just an exercise in java, about an elevator that could handle commands like 'U3' and 'D2'. Got little to do with reality. It is therefore completely up to the programmer what to do with it. Throw an exception when a typo is made? Or handle it with grace like Junilu proposed? Up to the programmer, who is wrtiting this code to gain experience and knowledge and is its only user.

This is a nice exercise to practise GUI, though. There you can have all Labels with the floors mentioned, with a big Label showing the current floor, and with a Timer to update that Label when the elevator is on its way.
1 month ago
But I would expect a file with content like:

How else can I calculate an average?
1 month ago

Stephan van Hulst wrote:Line 14 is a shorthand for this.doSomethingUseful().


Slight correction: the 'this' relates to the new Observer. So if you want to use a this reference, you should get:
1 month ago
And here is a method that finds the secondbest

But you see there is quite some code duplication here. They can be combined, though:
Use

for the best runner, and

for the index of the second best runner.

But if they ask for the third best runner, you're still out of luck. So, here is a more general way that is also more OO:

The Pair class that I used here is from JavaFX.
1 month ago
There is no mentioning of there being duplicates or not. Assuming that there are non, is unnecessary, since having duplicates does not make the secondbestRunner any more difficult.
1 month ago
But if you want to use only one iterator, this is also possible:
1 month ago
But the tests are not complete. For instance, if times = {100, 100} then the two methods should give different outcomes.
1 month ago

Campbell Ritchie wrote:But if you add objects to the List with one ListIterator, the other ListIterator will throw a concurrent modification exception.


The elements are added to a different list.
1 month ago
hi Ilya,

why not have TWO ListIterators? See the method List.listIterator. Take one that starts at index 0. and one that starts at index 1. Then you ccan do
1 month ago
hi Ion,

there are some mistakes in your code, and they are a bit complicated to explain. I will have a go:

1) your Game panel overrides the 'paint' method. This is not something you should do. A normal paint does three things: it calls 'paintComponent', where you draw your user graphics (as you do in the GamObject class). Second, it calls 'paintBorder' to draw a Border if it is there, and lastly, it calls 'paintChildren', drawing any component that has been added (here it is an instance of GameObject).

As you see, in yout 'paint' method there is no 'paintChildren', so the GameObject is in principal not drawn. That you see it nevertheless is because the Timer makes it repaint constantly, meaning it is visible anyway.

So, override just the 'paintComponent' in Game, as you do in GameObject.

2) but, as you write, you see no GameObject at all! That is because its size is 0. Although you do: r.setSize(...), that does not work. Use: r.setPreferredSizr(new Dimension(500, 500) instead.

3) but now the checkersboard dissapears! That is because the GameObject panel has a background color, that almost completely hides the checkerboard. So, give the GameObject panel a transparent background, like 'r.setBackground(new Color(0, 0, 0, 0);'

Then it is still not perfect (the checkerboard gets shifted a little. I suspect what causes that, but I have no solution yet), but you see what you had in mind.

There are simpler ways to achieve your goal, though.
1 month ago
Just finished day 3. Thought to be clever by eliminating duplicates with a TreeSet with manhattan distance comparator... took me some time to find out why my answer wasn't correct. And had an off-by-two error in 3B.