Greg Hatt

Greenhorn
+ Follow
since Feb 26, 2014
Merit badge: grant badges
For More
Nova Scotia, Canada
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Greg Hatt

My rationale for the two dimensional array is so that the class could be used to print out a game board which would show all previous attempts and evaluations. If you look at an image of the game on the Wikipedia article you can see what I mean.

The idea being that an actual mastermind game stores all previous guesses and evaluations.
10 years ago
Hey all,

So I just started learning Java not too long ago and have been struggling with creating useful objects. A lot of the material in the book I'm reading covers technical details of the Java language itself without much emphasis on designing classes. I was hoping that someone would be willing to take a look at a class I made and point out some of the things I've done wrong so I can work on improving them.

The object I tried to create was a Mastermind game. The game consists of one player creating a unique four element code of six colours and the other playing attempting to guess it. After each guess the code maker indicates whether the code breaker has guesses a correct colour or a correct colour and position. I know that the code is likely ugly but the only way it's going to get prettier is if I practice and recognize my errors. Feel free to criticize my naming conventions, class design, method creation and what-have-you.

10 years ago
True. I just sort of assumed that the lottery numbers themselves would have to be unique and the user's guess would have to be unique as well but after looking at the code I see it's not a business rule of this particular application.

Sorry for misleading OP. Keeping with above theme, or not using arrays or loops and if all numbers can be the same then you'll end up writing conditional statements for every possible combination of matches.
10 years ago
My mention of a GUI chess program was probably a bit misleading; it just came to me as an example of a program that would implement OOP concepts in constructing the different elements of the game (board, piece(s), etc). I was more or less just looking for general examples of what people consider to be model OOP designs so I could see some of the decisions people made in designing their objects.

Also, Winston, is the above link the one you were thinking of?

- Greg
10 years ago
I'm not sure if this is for a class or if you're learning on your own but one thing you might want to look into are collections/arrays. If this is for a class then you'll be covering arrays and loops in the near future (I assume) and are expected to answer this problem without the use of either loops or arrays?

You could just check each value against each other value and keep a track of how many matches you accumulate.

User Numbers = A B C
Lottery Numbers = D E F

The '||' is the "Or" operator. So the below statements say: "Increment the count of matches If A equals D OR A equals E OR A equals F".

If (A == D || A == E || A == F) { matchCount++};
If (B == D || B == E || B == F) { matchCount++};
if (C == D || C == E || C == F) { matchCount++};

Then you'll know how many matches you have. I think that makes sense.

10 years ago
That's actually really excellent advice, thanks. By "The Tutorials" I assume you mean the Java tutorials found at http://docs.oracle.com/javase/tutorial/ ?

I'm certainly not in the "been programming for twenty-five years" category by any means. I'm a student who just finished his first year of studies and has a bit off time during the summer to study. The books I mentioned above are fantastic at demonstrating different concepts but fail to present many fully formed programs. I'll have a look at the tutorials and see if the satisfy my need.

What I'm NOT looking for is simple one method programs whose sole purpose it is to demonstrate a single concept; my books have plenty of those. Like I said, I'll have a look through those tutorials.

Thanks again,

- Greg
10 years ago
Hey there,

I'm fairly new to Java myself but I will certainly give my suggestion. As I understand if you want to check if a value entered is numeric and you want to calculate the change. The way to do this would be with two separate methods. The scanner class does have a method to see if the input is numeric but I find it simpler to work with the String value. So, your method to check if user input is numeric might look something like this:

Or you could use regular expressions to see if it matches a numeric format, like this:

As was mentioned above though, it'd be easier to give advice if the code was posted.

10 years ago
Learning Java has been coming along quite nicely, I've mostly been working through Big Java: Early Objects and Core Java: Volume One. What I've found both these texts lack is enough examples of complete programs demonstrating good OOP design.

To get to my question: I was wondering if anyone had any suggestions for a site/place to find code examples of good object oriented design for a beginning programmer. An example might be a chess program with a GUI. To put it another way, as a beginning programmer I'm looking to read more code but I'm struggling to find a good resource for example programs. Any suggestions would be greatly appreciated.

- Greg
10 years ago
So it turns out that these options ARE mutually exclusive, in which case the diagram looks like:

Additionally Ulf Dittmer, that's exactly what I was looking for.
10 years ago
Sorry about that. As soon as I did I realized that no matter how poorly written or confused my question was it still might benefit someone since you were kind enough to provide an answer.
10 years ago
I'm fairly new to UML and have been trying to diagram a scenario for class but I'm stuck on one part. In the scenario three separate, non-exclusive activities can occur. After a client is registered they are able to book a new trip, modify an existing trip or cancel a trip. They can do all or one of that activities until they're all done managing their trips. I have something of a draft but I think it's wrong. I'll post the scenario and the diagram I've drafted.

Scenario

Once registered, the client has three options:
  • Book passage - this includes registering a passenger, selecting the destination, drop off and pick up times and paying for the passage. (Note that only on-line payments are accepted when using the web interface)
  • Modify existing passage
  • Cancel and refund existing passage (Note: any existing passage has already been paid for).


  • My Diagram

    After talking with some fellow students we've come up with a rough rule of thumb. You should always design your programs in such a way that you could replace the GUI without fundamentally changing your classes. Your GUI classes should not be performing logical operations.
    10 years ago
    I've removed my post simply because I wouldn't want code as sloppy as that appearing next to my name if someone did a Google search on me. Essentially the question was about how to separate GUI logic from Class logic.
    10 years ago