Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: ElementCoder on December 10, 2012, 03:08:56 pm
-
So I am in need of a little help here. There is the following problem:
Look at the following code. Assume there are five types (classes or interfaces) (U, G, B, Z and X) and one variable with each type.
U u;
G g;
B b;
Z z;
X x;
The following assignments are allowed (they compile):
u = z;
x = b;
g = u;
x = u;
The following assignments are NOT allowed:
u = b;
x = g;
z = u;
g = x;
The question:
What can you say, with the information you've gotten, about the types and their relations (how do they relate to each other?)
I'm pretty confused here so any help would be appreciated :)
-
class U is a subclass of class Z
class X is a subclass of class B
class G is a subclass of class U
class X is a subclass of class U
class U is not a subclass of B
class X is not a subclass of G
class Z is not a subclass of U
class G is not a subclass of X
-
You have your logic backwards pimathbrain. And Element, I've run into a problem, with the z, x, and u, it seems to me to be impossible for those three conditions to be true (or false as it were). I'll see if I can't figure something out though =P
-
You have your logic backwards pimathbrain. And Element, I've run into a problem, with the z, x, and u, it seems to me to be impossible for those three conditions to be true (or false as it were). I'll see if I can't figure something out though =P
I wasn't sure of my logic, anyways
-
I think there are three possible solutions to this problem using only the given classes/interfaces. They have the same basic hierarchy: X is a parent of B, X and G are parents of U, and U is a parent of Z. The three solutions come from the fact that either one or both of G and X can be an interface. Here are the three solutions (I think):
- class G
- interface X
- class B implements X
- class U extends G implements X
- class Z extends U
- interface G
- class X
- class B extends X
- class U extends X implements G
- class Z extends U
- interface G
- interface X
- class B implements X
- class U implements G, X
- class Z extends U
EDIT: Fixed, maybe? Why does this hurt my brain so much...
-
Thanks for your help guys. With your help I've figured out what it should be:
class U implements X, G
interface G
interface X
class B implements X
class Z extends U
[edit] aww runer you ninja'd me
Thanks :D ;D(http://www.omnimaga.org/Themes/default/images/gpbp_arrow_up.gif)
(http://4.bp.blogspot.com/_D_Z-D2tzi14/S8TRIo4br3I/AAAAAAAACv4/Zh7_GcMlRKo/s400/ALOT.png)