Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!

Ricky Bee

+ Follow
since Apr 04, 2019
Ricky likes ...
IntelliJ IDE Java Windows
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 Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ricky Bee

Brian Cole wrote:

Junilu Lacar wrote:Campbell is trying to point out an example where a call using a object reference instead of a class reference actually does more than just make the code confusing

...  all the Lion and Elephant instances are doing is trying to cause more confusion. The confusion is intentional, of course, but it would have been clearer to just write Animal.staticMethod(...) and be done with it.

Actually, I value that kind of intentional induced confusion - it is one of the great things about the Ranch. No direct answers provided, but answers that will make you think about the problem (and context) while "gently" pointing out a way.

There was certainly a lesson to be learned in Campbell's answer (and I thank him for it), but let me rephrase my previous question (and steer it a little from the inheritance of a static method problem which originated it in the first place): is such a structure a common-place in Java programing? I mean, is it normal to instantiate an object from a superclass and then pass it an object from a subclass?  That was what left me thinking...

By the way, thanks Junilu, for another awesome link.  

1 year ago
Hello, Ritchie.

Campbell Ritchie wrote:

Why would you want to code something like this?  

1 year ago

Knute Snortum wrote:Java classes can be used for rows in an SQL table and the fields in a class for columns.  An entire table might be List<MyClass>.

Ok, I see. Each instance of the class will hold a set of values (as per field) that, together become a record, and the list is the "logical structured unit" to hold them objects together.
1 year ago

Carey Brown wrote:


This is a disaster waiting to happen. A 'Patient' class should be created to hold this info. Have one field in the Patient class for each of these attributes.

As side questions, and thinking a little bit ahead:

Data like this normally gets stored in databases (normalized into tables and fields), and I can certainly sense a "strong structural resemblance", so:

- Is it normal practice in Java to mirror tables into classes and fields into data variables?
- Is it a good pratice so that some back-end database system can then be utilized?

1 year ago

Piet Souris wrote:Looks nice! Is it true that the ball slows down (to us, spectators), when it approaches the event horizon?   ;)

The ball does slow down sometimes, but I suspect it is due both to processor occupancy and poor coding skills...    

Thank you for your interest in the matter.     I will look into your code, study it to figure stuff out, but a good portion of it it still "out of my reach".
1 year ago

Knute Snortum wrote:An Applet, huh?  Those aren't used outside of the classroom, so redoing the code in Swing or JavaFX would make sense.  I'd vote for JavaFX, but there is some differing opinions on that.

Yes, indeed. I was fully aware of the Applet "limitation" before I started the lectures. My main goal regarding this course is to learn how to think in a OO-logic coding environment, and the fact that I got to code a Breakout-like little game was a value-added bonus (Arkanoid II was the first Spectrum game I ever owned, so this project has also a kind of emotional value attached).

But yes - I intend to recode this in Swing or JavaFX somewhere along the path. It still does make sense to learn both these technologies, right? (instead of putting my money in only one of them - which would by JavaFX, in case of choice).

1 year ago
Hey, Knute.

Thanks for the tip. That was exactly the sort of thing I was asking about. I'm still in the fisrt steps of a long Java-learning process and I know nothing at all about JavaFX and Swing. In a not much distant future (I hope), maybe I will come back to this topic to read again your post.

This Breakout-type app was coded with resources available in both the awt and acm libraries, the latter of which is said to be used mainly in teaching contexts. It even runs inside an Applet...

"The thing" presently looks like this:  

1 year ago
Back to your situation, a few hints:

What is happening on line two?

In the code you provided, a similar thing is happening in one case, and not happening in the other.

Also, and not least important, the names of the variables, methods and arguments are deliberately the same, so they might be confusing. Variable SCOPE matters in this situation...
1 year ago

Moe Jackson wrote:Can someone help  me figure this code out, this topic on OCA is very confusing to me.

Can you tell me why if i was to change line 5 to number = number(number); I would see the incrementation to 2.
And if I was to change line 6 to letter(letters) I would only see abc. Thanks.

Hello, Moe.

Two important things you should mind:

1 - What is keeping you from indenting your code? Like so:

There is a tag in the menu bar titled "Code" that allows you to do so. Then, copy code from your IDE or text editor,  paste it inside the tags and voilá.

2 - Have you tried the changes you propose? What was the result?
1 year ago
Hello, Piet and Angus.

Thank your for your time and insights.

I will certainly give some thought to your suggestions. I will probably go for a multiple class approach, thinking about different kinds of bricks, layouts, levels, balls, paddles, etc. I'll try and see how it goes - if anything, I'll learn something in the process.  

3) Sofar I've used the bounding box method myself. If that is too course however, then use the distance, radius and angle of approach of the ball(s) to determine whether there is a collision between balls or ball/brick. That complicates the formulas, so, as you said, if it is not a big problem, I would leave that for now.  

Yes, formulas will get from simple and straightforward to something resembling rocket-science. Specialy the ones related to the physics of motion (that is, what happens to objects after collisions). I guess I'll put this one on hold for now.  
1 year ago

Md Zuanyeed Kamal wrote: This code has been taken from java 11th edition by harbert schild chapter 13 .


I'm reading from that same book - a couple of chapters behind you.

I see you are using Eclipse to practice the examples provided. That will only get you into trouble (like the very situation you are describing in this topic).

You can of course replicate the examples in any IDE, but the author makes an early point in the book by stating that a simple text editor plus the command line should be utilized.

IDE's have a specific way to structure projects and files inside folders (with packages in the mix) - Ron's response above makes it clear that there are issues in your code related to that - you are not controling where files are being stored.

1 year ago

Junilu Lacar wrote:
Below are some references you might want to take a look at to get a better idea of good design. Knowing about basic design principles and how to apply them to your programs is what will really set you apart from other candidates, especially if it's for an entry-level junior position. In my opinion, it's never too soon to learn about design.

Perspective: Code is design -

Simple Design:
Book about the 4 Rules of Simple Design:

SOLID design:

"Clean Code" by Robert Martin - I wish all programmers would read this book first before they try to write any code in a professional capacity.

Finally, here's what Uncle Bob has to say about certifications:

Nice, nice, nice!     The book Clean Code looks particularly yummy.
Hello all, again.

I've watched 11 lessons of the Stanford CS106A online now and, with the help of the course book, I was able to produce a rudimentary working version of the Breakout game, similar to what is stated in the assignment paper.

Searching through the JavaRanch forums, I find some older posts reffering to this particular course and assignement, but none of them relates to my "existential" pains and aches.

I still have to go through the code in order to clean it up and make it less of a mess readable, but I have a lot of concepts that I would like to implement that go beyond the initial stated assignement.  

Being the purpose of the assignement teaching basic coding design patterns and techniques (rather than teaching how to program Breakout), I would like to ask for your input on the following matters:

The game is coded using a single class with multiple methods inside. Each method is dedicated to a single unitary purpose in the game (1 method to setup the board, 1 method to draw a brick, 1 method to set a line of bricks, 1 method to manage movement, etc).

1 - If I was to add additional levels (with different brick layouts) to the program, what would be a good aproach? 1 method per level? 1 class per level? Probaby, some of these levels would have to be almost built brick-by-brick, without using the "line-of-bricks" method.

2 - The same for bricks. If I was to add different kinds of bricks (in size, in shape - squares, tetris forms etc), how/where should I code them? A method? A class?

3... and 4 - The ball is round. Or is it? What we see is a circle, but the coordinate mechanism that orients the instantiation of an Oval object uses a rectangle placed around the Oval. All of the collision mechanism used to detect contact between ball and bricks is based on the upper, lower, left and right sides of that rectangle and on the illusion provided by movement that the ball is in fact hitting the bricks. However, if, say, I was to put two or more balls bouncing around (or, even better, if I wanted to code a snooker game), and if I wanted to manage the collisions between balls, this implemetations would fall short, because of all the new possible angles of contact not being taken into account (ex: one ball colliding with another at a 45 (first ball) to 225 (second ball) angle degree). This allready happens in part with a single ball and a brick, but it is not an important issue to correct at this stage - I mean, the game works fine as it is.

Given that some of the libraries (acm) used in this course and examples are confined to teaching purposes, and that the object library available might be a little bit old, which libraries/techologies/tools would now be a good choice as to "update" this example? JavaFX? Is there a "round ball" (hehe) available somewhere inside those libraries, with ready methods to getting angles and so?  

Rephrasing: If I was to build such a game from scratch using nowadays Java tools, where would I want to go?

Thanks in advance for any insight/tip.
1 year ago
Hello, Anu.

I'm also in the early stages of learning Java and find it often usefull to print out variables to the screen, so that I can trace what is happening -STEP-By-STEP - when a certain block of code runs. The situations you describe and the questions you are asking fit this use.

So, and starting with the need to format/indent the code pointed out by Campbell, I would suggest you add a couple of "System.out.println()" 's to you examples, outputing in each:

1 - the place in the code where you are;
2 - the value of the variables at that time;

An example you can addapt to your case: