473,748 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with inheritence, confused.

12 New Member
ok, so we're to right this program(in bluej) that consists of a few classes that will ultimately simulate a simple billfold(consis ting 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
6 3859
r035198x
13,262 MVP
ok, so we're to right this program(in bluej) that consists of a few classes that will ultimately simulate a simple billfold(consis ting 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
3,652 Recognized Expert Specialist
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
bjwillykajilly
12 New Member
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(sin ce 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
r035198x
13,262 MVP
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(sin ce 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 printExpiredCar ds() method.
Nov 15 '06 #5
bjwillykajilly
12 New Member
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
r035198x
13,262 MVP
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

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

Similar topics

1
1256
by: pb | last post by:
Im getting this compile error, undefined reference to BitString<10>::scramble() collect2: ld returned 1 exit status For some reason the compiler cant find the symbol for the member function i declare. Everythign compiles correctly up to the point when i make a call to the "scramble" routine. Calling the member functions of the base class work just fine, but when i try to call the member functions i declare in the extened class, the...
7
1599
by: vj | last post by:
Hello Group, I am C++/OOP newbie and was working on a project when i came accross this puzzleing problem with inheritence. C++ Code ================== class Parent {
1
1433
by: j.mandala | last post by:
I have two versions of a database front end and want to be able to use docmd.copy (or some other method) to move a bunch of queries. I was able to use the '.tag' property to of forms and reports to move them using code. For example, first I put the word "Special" in the ..tag property of my reports (or forms), then I use the following procedure: Sub CopyReps()
9
1209
by: Jack Addington | last post by:
I have a base form and a base logic class. Each has to know of the other. I'm then inheriting to create descendant form and descendant logic which extend both objects and again have to know of each other. I'm getting mixed up in the syntax/technique on how to refer to the references generically in the code. I want to be able to always call LogicClass.SomeMethod() and each FormClass knows what to do with it. In some cases I have 3-4...
1
1014
by: Learnicus | last post by:
Hello, Nothing seems to make any sense anymore. I created a new website which creates a _Default webform. I did nothing to this webform and then created another (Default2) and made it inherit from _Default. If i open Default2 i get errors in the error list and cannot build it. If i close Default 2 however all the errors disappear and i can build the project and run it.
0
282
by: saravanan_article | last post by:
Hi I am newbie to C#, i am using C# 2005 and DataGridView in my Application. The problem is described here I am using DataGrid and I placed some Headers like Column1,Column2,Column3.... What i want is i that i want to Create secondary column under the Column1(Col name) of dataGridView. Please share ur ideas.. Its very urgent. Is there any properties i need to set or any methods i need to override? Reply me as soon as possible
3
1486
by: Charlie Bear | last post by:
i've got myself into a bit of an oo mess. it's probably me misunderstanding how oo works. I've got a base class called "Feature" which some classes inherit. in the database i've stored the data in a table along with a type id to tell me what type of feature it is. ie a poll feature has an id of two and has the properties of feature plus a poll id which links to a poll table. the poll feature has seperate methods that the base feature...
8
9165
by: rdabane | last post by:
I'm trying to perform following type of operation from inside a python script. 1. Open an application shell (basically a tcl ) 2. Run some commands on that shell and get outputs from each command 3. Close the shell I could do it using communicate if I concatenate all my commands ( separated by newline ) and read all the output in the end. So basically I could do following sequence: 1. command1 \n command2 \n command 3 \n
9
3711
by: tsushio | last post by:
hello,i'm a student and i'm in my first year. i had an assignment using borland c++ and this is what i made #include <iostream.h> #include <conio.h> int main () { int number,odd,even,small,big;
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.