Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. We generally recognize inheritance with the ‘is-a’ relationship. In such cases, we would
  2. extend a class to derive a new class that employs the behavior of the parent class and/or
  3. overrides those behaviors that specific to that child class.
  4. This differs from the ‘has-a’ relationship. In this case, a class might use objects of a
  5. different type, e.g., the String class, but does not employ inheritance.
  6. For the following, begin by determining the proper relationship of the class you are
  7. constructing.
  8. 1. Consider using the following Card class.
  9. public class Card {
  10. private String name;
  11. public Card() {
  12. name = "";
  13. }
  14. public Card(String n) {
  15. name = n;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public boolean isExpired() {
  21. return false;
  22. }
  23. public String format() {
  24. return "Card holder: " + name;
  25. }
  26. }
  27. Use this class as a superclass to implement the following hierarchy of related classes
  28. having specific instance data:
  29. Class Data
  30. IDCard ID number String
  31. CreditCard Card number, PIN String
  32. DriverLicense Expiration year int
  33. Write declarations for each of the subclasses based upon the proper ‘is-a’
  34. relationship. For each subclass, supply private instance variables. Leave the bodies of
  35. the constructors and the format methods blank for now.
  36. 2. Implement constructors for each of the three subclasses. Each constructor must call
  37. the superclass constructor to set the name. Here is one example:
  38. public IDCard(String n, String id) {
  39. super(n);
  40. idNumber = id;
  41. }
  42. Be sure to include the data (parameters) specific to each class.
  43. 3. Override the implementation of the format method for each of the three subclasses.
  44. The methods should produce a formatted description of the card details. The subclass
  45. methods should call the superclass format method to get the formatted name of the
  46. cardholder.
  47. 4. Devise another class, Billfold, which contains slots for two cards, card1 and card2, a
  48. method void addCard(Card card) and a method String formatCards(). Note that this
  49. class employs the ‘has-a’ relationship.
  50. The addCard method checks whether card1 is null. If so, it sets card1 to the new
  51. card. If not, it checks card2. If both cards are set already, the method has no effect.
  52. Of course, formatCards invokes the format method on each non-null card and
  53. produces a string with the format
  54. [card1|card2]
  55. What is your Billfold class?
  56. 5. Write a tester program that adds two objects of different subclasses of Card to a
  57. Billfold object. Test the results of the formatCards methods.
  58. What is the code for your BillfoldTester class?
  59. 6. The Card superclass defines a method isExpired, which always returns false. This
  60. method is not appropriate for the driver license. Supply a method header and body for
  61. DriverLicense.isExpired() that checks whether the driver license is already expired
  62. (i.e., the expiration year is less than the current year).
  63. To work with dates, you can use the methods and constants supplied in abstract class
  64. Calendar which are inherited by the concrete class GregorianCalendar. This class
  65. must be imported from the java.util package. You create a Calendar as follows:
  66. GregorianCalendar calendar = new GregorianCalendar();
  67. Then, you can obtain the current year using the constant Calendar.YEAR and method
  68. get in GregorianCalendar. The constant indicates that the method should return the
  69. current year. By comparing the returned value with the expYear field in
  70. DriverLicense, you can determine if the card is expired. The code below will retrieve
  71. the current year:
  72. calendar.get(Calendar.YEAR)
  73. 7. The ID card and the credit card don’t expire. What should you do to reflect this fact in
  74. your implementation?
  75. 8. Add a method getExpiredCardCount, which counts the number of expired cards in
  76. the billfold, to the Billfold class.
  77. 9. Write a BillfoldTester class that populates a billfold with a phone calling card and an
  78. expired driver license. Then call the getExpiredCardCount method. Implement the
  79. following tester class to verify that your method works correctly.
  80. public class BillfoldTester {
  81. public static void main(String[] args) {
  82. DriverLicense d = new DriverLicense("John Doe", "08-097654", 2003);
  83. CreditCard c = new CreditCard("Omega Card", "301233985945", 1030);
  84.  
  85. Billfold b = new Billfold();
  86.  
  87. b.addCard(d);
  88. b.addCard(c);
  89.  
  90. System.out.println(b.formatCards());
  91. System.out.println("Number of expired cards: " + b.getExpiredCardCount());
  92. System.out.println("Expected: 1");
  93. }
  94. }
  95. 10. Implement toString methods for the Card class and its subclasses. The methods
  96. should print:
  97. the name of the class
  98. the values of all instance variables (including inherited instance variables)
  99. Typical formats are:
  100. Card[name=Edsger W. Dijkstra]
  101. CreditCard[name=Bjarne Stroustrup][number=4156646425,pin=2234]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement