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) - 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;}
-
}
- 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");}}
-
}
- 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;}
-
}
- class CallingCard extends Card
-
{ private int cardNumber;
-
private int PIN;
-
CallingCard(String name, int cn, int pin)
-
{ super(name);
-
cardNumber = cn;
-
PIN = pin;}
-
}
- class IDCard extends Card
-
{ private int idNumber;
-
IDCard(String name, int id)
-
{ super(name);
-
idNumber = id;}
-
}
- 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
6 3859
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) - 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;}
-
}
- 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");}}
-
}
- 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;}
-
}
- class CallingCard extends Card
-
{ private int cardNumber;
-
private int PIN;
-
CallingCard(String name, int cn, int pin)
-
{ super(name);
-
cardNumber = cn;
-
PIN = pin;}
-
}
- class IDCard extends Card
-
{ private int idNumber;
-
IDCard(String name, int id)
-
{ super(name);
-
idNumber = id;}
-
}
- 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
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 - public boolean isExpired() {
-
return (expYear >= 2006); // or other expiration condition
-
}
Then each card subclass would have this function, and it should work accordingly.
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.
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 - public void addCard(Card x)
-
{
-
if(card1 == null)
-
card1 = x;
-
else if(card2 == null)
-
card2 = x;
-
}
-
-
You can also improve your printExpiredCar ds() method.
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.
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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
{
|
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()
|
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...
|
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.
| |
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
|
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...
|
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
|
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;
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |