By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

need help with inheritence, confused.

P: 12
ok, so we're to right this program(in bluej) that consists of a few classes that will ultimately simulate a simple billfold(consisting of credit cards) a main card class, a drivers license, an id card, and a calling card. Now, i'm not coming to you guys to finish my program, i'm coming to you guys so i can understand it(as it is my major, as of 2 days ago)

Expand|Select|Wrap|Line Numbers
  1. class Card
  2. {   public String name;
  3.     public Card()
  4.     {   name = "";}
  5.     public Card(String n)
  6.     {   name = n;}
  7.     public boolean isExpired()
  8.     {   return false;}
  9.     public void print()
  10.     {   System.out.println("Card holder: " + name);}
  11.     public String toString()
  12.     {   String temp = "Card[name=" + name + "]";
  13.         return temp;}
  14.     public boolean equals(Card x)
  15.     {   if(this.name == x.name)
  16.         {   return true;}
  17.         else if(this.name != x.name)
  18.         {   return false;}
  19.         else
  20.         {   System.out.println("Error, Card.equals(method)");
  21.             return false;}}
  22.     public Card Clone()
  23.     {   Card x = new Card(this.name);
  24.         return x;}
  25. }
Expand|Select|Wrap|Line Numbers
  1. class Billfold extends Card
  2. {   Card card1;
  3.     Card card2;    
  4.     public void addCard(Card x)
  5.     {   if(card1 == null)
  6.         {   card1 = new Card(x.name);}
  7.         else if(card1 != null)
  8.         {   if(card2 == null)
  9.             {   card2 = new Card(x.name);}}}
  10.     public void printCards()
  11.     {   if(card1 != null)
  12.         {   if(card2 != null)
  13.             {   card1.print();
  14.                 card2.print();}
  15.             else
  16.             {   card1.print();}}
  17.         else
  18.         {   return;}}
  19.     public void printExpiredCards()
  20.     {   if(card1 != null && card2 != null)
  21.         {   if(card1.isExpired() == true && card2.isExpired() == false)
  22.             {   System.out.println("Card 1 has expired, but Card 2 has not.");}
  23.             else if(card1.isExpired() == false && card2.isExpired() == false)
  24.             {   System.out.println("Neither Card has expired.");}
  25.             else if(card1.isExpired() == false && card2.isExpired() == true)
  26.             {   System.out.println("Card 1 has not expired, but Card 2 has.");}
  27.             else if(card1.isExpired() == true && card2.isExpired() == true)
  28.             {   System.out.println("Both Cards have expired.");}
  29.             else
  30.             {   System.out.println("Error");}}
  31.         else if(card1 != null && card2 == null)
  32.         {   if(card1.isExpired() == true)
  33.             {   System.out.println("Card 1 has expired.");}
  34.             else if(card1.isExpired() == false)
  35.             {   System.out.println("Card 1 has not expired.");}
  36.             else
  37.             {   System.out.println("Error");}}
  38.         else if(card1 == null)
  39.         {   System.out.println("No cards available.");}
  40.         else
  41.         {   System.out.println("Error");}}
  42. }
Expand|Select|Wrap|Line Numbers
  1. class driversLicense extends Card
  2. {   private int expYear;
  3.     driversLicense(String name, int exp)
  4.     {   super(name);
  5.         expYear = exp;}
  6.     public boolean isExpired()
  7.     {   if(expYear >= 2006)
  8.         {   return false;}
  9.         else
  10.         return true;}
  11. }
Expand|Select|Wrap|Line Numbers
  1. class CallingCard extends Card
  2. {   private int cardNumber;
  3.     private int PIN;
  4.     CallingCard(String name, int cn, int pin)
  5.     {   super(name);
  6.         cardNumber = cn;
  7.         PIN = pin;}
  8. }
Expand|Select|Wrap|Line Numbers
  1. class IDCard extends Card
  2. {   private int idNumber;
  3.     IDCard(String name, int id)
  4.     {   super(name);
  5.         idNumber = id;}
  6. }
Expand|Select|Wrap|Line Numbers
  1. class BillfoldTester
  2. {   public static void main(String[] args)
  3.     {   Billfold b1 = new Billfold();
  4.         Card dl = new driversLicense("Bryan", 2005);
  5.         Card idc = new IDCard("Dwayne", 6404);
  6.         Card cc = new CallingCard("Meghan", 12, 21);
  7.         b1.addCard(cc);
  8.         b1.addCard(idc);
  9.         b1.addCard(dl);
  10.         b1.printExpiredCards();
  11.         b1.printCards();
  12.         Card x = dl.Clone();
  13.         x.print();
  14.         System.out.println("Is dl = x? " +dl.equals(x));
  15.         System.out.println(dl.toString());}
  16. }
sorry for all the code, i thought i might as well post it all, the problem is the isExpired class(as far as i know) I want to overwrite the method from card with the one in driversLicense, but to no avail have i succeeded in getting any result other than false... am i going about this all wrong? this whole inheritance is quite confusing and my teacher keeps postponing his office hours. any and all help i get is greatly appreciated, i just beg you to keep in mind, I have had 1 beginning computer course(bits bytes and all that crap) and am(as for this semester) in 2 intro classes(object oriented java) and intro 2 computing c++(object oriented also.) so my knowledge isn't what you could call vast by any means. thank you for your help
Nov 15 '06 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
ok, so we're to right this program(in bluej) that consists of a few classes that will ultimately simulate a simple billfold(consisting of credit cards) a main card class, a drivers license, an id card, and a calling card. Now, i'm not coming to you guys to finish my program, i'm coming to you guys so i can understand it(as it is my major, as of 2 days ago)

Expand|Select|Wrap|Line Numbers
  1. class Card
  2. { public String name;
  3. public Card()
  4. { name = "";}
  5. public Card(String n)
  6. { name = n;}
  7. public boolean isExpired()
  8. { return false;}
  9. public void print()
  10. { System.out.println("Card holder: " + name);}
  11. public String toString()
  12. { String temp = "Card[name=" + name + "]";
  13. return temp;}
  14. public boolean equals(Card x)
  15. { if(this.name == x.name)
  16. { return true;}
  17. else if(this.name != x.name)
  18. { return false;}
  19. else
  20. { System.out.println("Error, Card.equals(method)");
  21. return false;}}
  22. public Card Clone()
  23. { Card x = new Card(this.name);
  24. return x;}
  25. }
Expand|Select|Wrap|Line Numbers
  1. class Billfold extends Card
  2. { Card card1;
  3. Card card2; 
  4. public void addCard(Card x)
  5. { if(card1 == null)
  6. { card1 = new Card(x.name);}
  7. else if(card1 != null)
  8. { if(card2 == null)
  9. { card2 = new Card(x.name);}}}
  10. public void printCards()
  11. { if(card1 != null)
  12. { if(card2 != null)
  13. { card1.print();
  14. card2.print();}
  15. else
  16. { card1.print();}}
  17. else
  18. { return;}}
  19. public void printExpiredCards()
  20. { if(card1 != null && card2 != null)
  21. { if(card1.isExpired() == true && card2.isExpired() == false)
  22. { System.out.println("Card 1 has expired, but Card 2 has not.");}
  23. else if(card1.isExpired() == false && card2.isExpired() == false)
  24. { System.out.println("Neither Card has expired.");}
  25. else if(card1.isExpired() == false && card2.isExpired() == true)
  26. { System.out.println("Card 1 has not expired, but Card 2 has.");}
  27. else if(card1.isExpired() == true && card2.isExpired() == true)
  28. { System.out.println("Both Cards have expired.");}
  29. else
  30. { System.out.println("Error");}}
  31. else if(card1 != null && card2 == null)
  32. { if(card1.isExpired() == true)
  33. { System.out.println("Card 1 has expired.");}
  34. else if(card1.isExpired() == false)
  35. { System.out.println("Card 1 has not expired.");}
  36. else
  37. { System.out.println("Error");}}
  38. else if(card1 == null)
  39. { System.out.println("No cards available.");}
  40. else
  41. { System.out.println("Error");}}
  42. }
Expand|Select|Wrap|Line Numbers
  1. class driversLicense extends Card
  2. { private int expYear;
  3. driversLicense(String name, int exp)
  4. { super(name);
  5. expYear = exp;}
  6. public boolean isExpired()
  7. { if(expYear >= 2006)
  8. { return false;}
  9. else
  10. return true;}
  11. }
Expand|Select|Wrap|Line Numbers
  1. class CallingCard extends Card
  2. { private int cardNumber;
  3. private int PIN;
  4. CallingCard(String name, int cn, int pin)
  5. { super(name);
  6. cardNumber = cn;
  7. PIN = pin;}
  8. }
Expand|Select|Wrap|Line Numbers
  1. class IDCard extends Card
  2. { private int idNumber;
  3. IDCard(String name, int id)
  4. { super(name);
  5. idNumber = id;}
  6. }
Expand|Select|Wrap|Line Numbers
  1. class BillfoldTester
  2. { public static void main(String[] args)
  3. { Billfold b1 = new Billfold();
  4. Card dl = new driversLicense("Bryan", 2005);
  5. Card idc = new IDCard("Dwayne", 6404);
  6. Card cc = new CallingCard("Meghan", 12, 21);
  7. b1.addCard(cc);
  8. b1.addCard(idc);
  9. b1.addCard(dl);
  10. b1.printExpiredCards();
  11. b1.printCards();
  12. Card x = dl.Clone();
  13. x.print();
  14. System.out.println("Is dl = x? " +dl.equals(x));
  15. System.out.println(dl.toString());}
  16. }
sorry for all the code, i thought i might as well post it all, the problem is the isExpired class(as far as i know) I want to overwrite the method from card with the one in driversLicense, but to no avail have i succeeded in getting any result other than false... am i going about this all wrong? this whole inheritance is quite confusing and my teacher keeps postponing his office hours. any and all help i get is greatly appreciated, i just beg you to keep in mind, I have had 1 beginning computer course(bits bytes and all that crap) and am(as for this semester) in 2 intro classes(object oriented java) and intro 2 computing c++(object oriented also.) so my knowledge isn't what you could call vast by any means. thank you for your help
1) Consider making the Card class abstract.
2) The Billfold class should not extend Card. It is not a card but a collection of cards and so it should have an ArrayList of cards. Currently you can only store 2 cards in the Billfold class but in the main method you are adding 3 cards
Nov 15 '06 #2

Ganon11
Expert 2.5K+
P: 3,652
In addition to making the Card class abstract, you may want to make the isExpired function in card abstract, as the expiration date on any card may vary. You could also have an integer value for each card representing its expiration date that is passed as a parameter in the constructor. of course, if you did this, there would be no need to overload the isExpired function, and you could simply write it as

Expand|Select|Wrap|Line Numbers
  1. public boolean isExpired() {
  2.     return (expYear >= 2006); // or other expiration condition
  3. }
Then each card subclass would have this function, and it should work accordingly.
Nov 15 '06 #3

P: 12
the only problem is the lab that we are working on is over inheritence and super classes, not abstract, or i don't think it would be near as much of a problem, secondly, the billfold is as requested supposed to hold a maximum of 2 cards, if it reaches 2 it simply doesn't add anymore(3rd card on billfoldtest is to make sure it works) and the isExpired is supposed to be declared in the card class as it is and then only in driverslicense is it supposed to be overwritten(since as you may have seen, the driverslicense is the only one with an exp year, as again, requested by the teacher). what i really need to know is, am i creating the cards correctly in the billfold class, ie, should they be initialized as new Card(String name) 's or should i find some way to find out what card object the addcard is being passed then create that specific card object, and if so, how, if possible? thanks again for any and all help.
Nov 15 '06 #4

10K+
P: 13,264
the only problem is the lab that we are working on is over inheritence and super classes, not abstract, or i don't think it would be near as much of a problem, secondly, the billfold is as requested supposed to hold a maximum of 2 cards, if it reaches 2 it simply doesn't add anymore(3rd card on billfoldtest is to make sure it works) and the isExpired is supposed to be declared in the card class as it is and then only in driverslicense is it supposed to be overwritten(since as you may have seen, the driverslicense is the only one with an exp year, as again, requested by the teacher). what i really need to know is, am i creating the cards correctly in the billfold class, ie, should they be initialized as new Card(String name) 's or should i find some way to find out what card object the addcard is being passed then create that specific card object, and if so, how, if possible? thanks again for any and all help.
No need to check card type in addCard method unless you use that information in that method(In this case you are not). If you do decide to do it, the method would be

if(card instanceof driversLicense ) etc

IMO, you are creating your cards correctly in the main method and the override is correct for isExpired. What you still need to do is remove the Billfold extends Card thing. As soon as you learn the abstract keyword you should come and add it to Card ofcourse.

addCard should simply be looking like

Expand|Select|Wrap|Line Numbers
  1.  public void addCard(Card x)
  2.  if(card1 == null)
  3.     card1 = x;
  4.  else if(card2 == null)
  5.      card2 = x;
  6. }
  7.  
  8.  
You can also improve your printExpiredCards() method.
Nov 15 '06 #5

P: 12
Ok, i've fixed the addcard thing(idk y i didn't think of that, its so obvious) and took off the extends card on billfold class, tested it again, and i still get false when i run it for the isExpired, i even changed the expyear to 10 and it still didn't work. any other ideas? thanks for the help r035198x, and every1 else of course.
Nov 15 '06 #6

10K+
P: 13,264
Ok, i've fixed the addcard thing(idk y i didn't think of that, its so obvious) and took off the extends card on billfold class, tested it again, and i still get false when i run it for the isExpired, i even changed the expyear to 10 and it still didn't work. any other ideas? thanks for the help r035198x, and every1 else of course.
Remeber the driversLicense is not being added to the billfold class since it's added as the third card. So how are you testing the isExpired method?
Nov 16 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.