need help with inheritence, confused.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bjwillykajilly
    New Member
    • Nov 2006
    • 12

    #1

    need help with inheritence, confused.

    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)

    Code:
    class Card
    {   public String name;
        public Card()
        {   name = "";}
        public Card(String n)
        {   name = n;}
        public boolean isExpired()
        {   return false;}
        public void print()
        {   System.out.println("Card holder: " + name);}
        public String toString()
        {   String temp = "Card[name=" + name + "]";
            return temp;}
        public boolean equals(Card x)
        {   if(this.name == x.name)
            {   return true;}
            else if(this.name != x.name)
            {   return false;}
            else
            {   System.out.println("Error, Card.equals(method)");
                return false;}}
        public Card Clone()
        {   Card x = new Card(this.name);
            return x;}
    }
    Code:
    class Billfold extends Card
    {   Card card1;
        Card card2;    
        public void addCard(Card x)
        {   if(card1 == null)
            {   card1 = new Card(x.name);}
            else if(card1 != null)
            {   if(card2 == null)
                {   card2 = new Card(x.name);}}}
        public void printCards()
        {   if(card1 != null)
            {   if(card2 != null)
                {   card1.print();
                    card2.print();}
                else
                {   card1.print();}}
            else
            {   return;}}
        public void printExpiredCards()
        {   if(card1 != null && card2 != null)
            {   if(card1.isExpired() == true && card2.isExpired() == false)
                {   System.out.println("Card 1 has expired, but Card 2 has not.");}
                else if(card1.isExpired() == false && card2.isExpired() == false)
                {   System.out.println("Neither Card has expired.");}
                else if(card1.isExpired() == false && card2.isExpired() == true)
                {   System.out.println("Card 1 has not expired, but Card 2 has.");}
                else if(card1.isExpired() == true && card2.isExpired() == true)
                {   System.out.println("Both Cards have expired.");}
                else
                {   System.out.println("Error");}}
            else if(card1 != null && card2 == null)
            {   if(card1.isExpired() == true)
                {   System.out.println("Card 1 has expired.");}
                else if(card1.isExpired() == false)
                {   System.out.println("Card 1 has not expired.");}
                else
                {   System.out.println("Error");}}
            else if(card1 == null)
            {   System.out.println("No cards available.");}
            else
            {   System.out.println("Error");}}
    }
    Code:
    class driversLicense extends Card
    {   private int expYear;
        driversLicense(String name, int exp)
        {   super(name);
            expYear = exp;}
        public boolean isExpired()
        {   if(expYear >= 2006)
            {   return false;}
            else
            return true;}
    }
    Code:
    class CallingCard extends Card
    {   private int cardNumber;
        private int PIN;
        CallingCard(String name, int cn, int pin)
        {   super(name);
            cardNumber = cn;
            PIN = pin;}
    }
    Code:
    class IDCard extends Card
    {   private int idNumber;
        IDCard(String name, int id)
        {   super(name);
            idNumber = id;}
    }
    Code:
    class BillfoldTester
    {   public static void main(String[] args)
        {   Billfold b1 = new Billfold();
            Card dl = new driversLicense("Bryan", 2005);
            Card idc = new IDCard("Dwayne", 6404);
            Card cc = new CallingCard("Meghan", 12, 21);
            b1.addCard(cc);
            b1.addCard(idc);
            b1.addCard(dl);
            b1.printExpiredCards();
            b1.printCards();
            Card x = dl.Clone();
            x.print();
            System.out.println("Is dl = x? " +dl.equals(x));
            System.out.println(dl.toString());}
    }
    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by bjwillykajilly
    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)

    Code:
    class Card
    { public String name;
    public Card()
    { name = "";}
    public Card(String n)
    { name = n;}
    public boolean isExpired()
    { return false;}
    public void print()
    { System.out.println("Card holder: " + name);}
    public String toString()
    { String temp = "Card[name=" + name + "]";
    return temp;}
    public boolean equals(Card x)
    { if(this.name == x.name)
    { return true;}
    else if(this.name != x.name)
    { return false;}
    else
    { System.out.println("Error, Card.equals(method)");
    return false;}}
    public Card Clone()
    { Card x = new Card(this.name);
    return x;}
    }
    Code:
    class Billfold extends Card
    { Card card1;
    Card card2; 
    public void addCard(Card x)
    { if(card1 == null)
    { card1 = new Card(x.name);}
    else if(card1 != null)
    { if(card2 == null)
    { card2 = new Card(x.name);}}}
    public void printCards()
    { if(card1 != null)
    { if(card2 != null)
    { card1.print();
    card2.print();}
    else
    { card1.print();}}
    else
    { return;}}
    public void printExpiredCards()
    { if(card1 != null && card2 != null)
    { if(card1.isExpired() == true && card2.isExpired() == false)
    { System.out.println("Card 1 has expired, but Card 2 has not.");}
    else if(card1.isExpired() == false && card2.isExpired() == false)
    { System.out.println("Neither Card has expired.");}
    else if(card1.isExpired() == false && card2.isExpired() == true)
    { System.out.println("Card 1 has not expired, but Card 2 has.");}
    else if(card1.isExpired() == true && card2.isExpired() == true)
    { System.out.println("Both Cards have expired.");}
    else
    { System.out.println("Error");}}
    else if(card1 != null && card2 == null)
    { if(card1.isExpired() == true)
    { System.out.println("Card 1 has expired.");}
    else if(card1.isExpired() == false)
    { System.out.println("Card 1 has not expired.");}
    else
    { System.out.println("Error");}}
    else if(card1 == null)
    { System.out.println("No cards available.");}
    else
    { System.out.println("Error");}}
    }
    Code:
    class driversLicense extends Card
    { private int expYear;
    driversLicense(String name, int exp)
    { super(name);
    expYear = exp;}
    public boolean isExpired()
    { if(expYear >= 2006)
    { return false;}
    else
    return true;}
    }
    Code:
    class CallingCard extends Card
    { private int cardNumber;
    private int PIN;
    CallingCard(String name, int cn, int pin)
    { super(name);
    cardNumber = cn;
    PIN = pin;}
    }
    Code:
    class IDCard extends Card
    { private int idNumber;
    IDCard(String name, int id)
    { super(name);
    idNumber = id;}
    }
    Code:
    class BillfoldTester
    { public static void main(String[] args)
    { Billfold b1 = new Billfold();
    Card dl = new driversLicense("Bryan", 2005);
    Card idc = new IDCard("Dwayne", 6404);
    Card cc = new CallingCard("Meghan", 12, 21);
    b1.addCard(cc);
    b1.addCard(idc);
    b1.addCard(dl);
    b1.printExpiredCards();
    b1.printCards();
    Card x = dl.Clone();
    x.print();
    System.out.println("Is dl = x? " +dl.equals(x));
    System.out.println(dl.toString());}
    }
    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

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      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

      Code:
      public boolean isExpired() {
          return (expYear >= 2006); // or other expiration condition
      }
      Then each card subclass would have this function, and it should work accordingly.

      Comment

      • bjwillykajilly
        New Member
        • Nov 2006
        • 12

        #4
        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.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by bjwillykajilly
          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

          Code:
           public void addCard(Card x)
          { 
           if(card1 == null)
          	card1 = x;
           else if(card2 == null)
          	 card2 = x;
          }
          You can also improve your printExpiredCar ds() method.

          Comment

          • bjwillykajilly
            New Member
            • Nov 2006
            • 12

            #6
            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.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by bjwillykajilly
              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?

              Comment

              Working...