Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes struct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "struct" Watch "struct" New topic


William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I have a block/cube, it has six sides, each side can have one of four colors. In C++ I could have a struct to hold this info. And each color would be an enum.
I java (or C++) I can have a class which represents the block and have private data members for each side of the block (front, back, left, right, top, bottom). But how do I use "RED" as the value for one of those sides? In C++ I would have an enum that declared "RED" than I could use that value in different classes. How do I do this in java, thanks.

Please ignore post, I have no idea what I am talking about.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Christopher.
I have a handicap here in that I don't know C++. But maybe in addition to your Cube class you could define a Side class? Then you could go a step further and even define your own Color class. Then a Side would "have-a" Color, and a Cube would be composed of six Sides:

If you're not married to the idea of naming the sides of your Cube class, you might want to define your instance variables there as a collection of Sides: maybe an array, or a java.util.ArrayList or a java.util.Vector. Then it will be easier and more robust to iterate over your cube's sides, looking for a particular color. Furthermore, ask yourself if it makes sense to decree, somewhat arbitrarily, one of the six sides "top"? After all, if you're in the northern hemisphere, someone in Australia might think what you're calling the "top" is really the "bottom," and Java is supposed to be "write once, run anywhere". For what you're doing, Christopher, this nomenclature may very well be sufficient, but I want to be sure you think about the ramifications of your naming conventions and design before you get too far into this project.
Just out of idle curiosity, are you converting a Rubik's cube app from C++ to Java?
Good luck, Christopher, and let us know how it goes.
[This message has been edited by Art Metzer (edited May 15, 2001).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
You could have an array of Colors
class Block{
Color[] fourColors = {,,, Color.yellow};
Color topColor = fourColors[0];

"JavaRanch, where the deer and the Certified play" - David O'Meara
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
If the only info you need to know about each side is the color it is then you could just define some constants:
int RED = 1, BLUE = 2 and so on. Then just assign each side the constant. ie
side1 = RED;
now side one has the value of 1. Later then just test each side to see what color they are:
if (side1 == BLUE){
// do somehting
If I complety missed what you were trying to do let me know, else I hoe that helps you out - even just a little.


William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Thanks for the suggestions, they are all helpful.
I wrote code to solve the puzzle (I don't know a name for it) you have four six sided blocks. The sides of the blocks have one of four colors. You stack the blocks and try to get all of the colors on each side of the stack. (Hope that makes sense.)
So it is important to know which side of the block is the "top". Because when you rotate the block to the left, for example, the top and bottom colors don't change but all of the others do.
I have "int Red = 0;" at the moment, which is fine. I was just wondering how to let multiple classes know about "Red" without declaring "Red" in each of those classes.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
You should be able to make the colors static variables of the class, then they would be available to all the other clases.
public class Cube{
public static int RED = 0;
Then all of your other classes could just refer to them as:
Cube.RED, or Cube.BLUE, etc.
that should do the trick for you.

I agree. Here's the link:
subject: struct
It's not a secret anymore!