Could anyone help me with this? I don't want the answer just an idea on how to get started.
Scenario:
1. Jack is grandfather of Jim.
2. Jackie is grandmother of Jim.
3. John is father of Jim.
4. Jane is mother of Jim.
Inheritance relation:
A. Jim inherits color from John, which he inherited from Jack.
B. Jim inherited his blonde hair from his mother.
C. Jim inherited his eye colors from his grandmother.
How can you put these statements in Java? You can consider each person as interface or class.
Programming?
There are a number of ways to model these relationships. Since this appears to be a homework problem, I won't supply any Java code. However, the simplest design might be to define a couple classes (e.g. Person, Trait) with one attribute per inherited trait (e.g. hair, skin_color, eye_color), two Person references for the parents, and a String for the person's name. The "Trait" class could have the inherited name as a String (optional) and a Person reference for who it is inherited from.
If it needs to be a complete program, you need to define the classes and initialize object instances with the scenario data. However, there's no indication from your givens whether any class methods are needed but if this were a practical assignment, some obvious ones might have to be developed.
There are ways to do something similar with collections but it would be more complicated; this would only be called for if certain flexiblity in the design were needed (e.g. dynamic traits).
Reply:public class Jack{
..
}
public class John extends Jack{
..
}
public class Jim extends John {
..
}
Reply:Draw it out on paper. First draw Jim. The draw his parents above him. Draw an arrow from Jim to each of his parents. The arrow always points from the child to the parent.
From the inheritance relation information, we learn that Jack is parent of John. So draw that relationship.
We cannot tell if Jackie is parent of Jane or John. Pick one.
Once you have the class diagram, write in the attributes. Color from from Jack to John to Jim. So you would put the color attribute in Jack. Do the same for blond hair and eye colors, putting them in the right place of course.
Once you have a class diagram, you can then write the corresponding Java code. It's just a matter of translation from that point forward.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment