In: Computer Science
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below.
The boxes will expand once you paste your code into them, so don’t worry about how it looks J
All data members are private and all methods are public in each class/interface in the exercise below.
Creare a class named CreditCard, which has three private data members:
Provide:
There are two effector methods:
/** CreditCard.java */
Code a subclass named ExpressCard that inherits from the superclass CreditCard, which has two private data members:
Provide:
There are two effector methods:
/** ExpressCard.java */
Code a subclass named PremiumCard that inherits from the superclass CreditCard, which has two private data members:
Provide:
There are two effector methods:
/** PremiumCard.java */
Code a class named JohnDoeTest that has a main method. Replace JohnDoe with your name.
In the main method, create two objects:
When creating these two objects, you need to use the constructor that has all parameters in each class, and you need to hardcode reasonable values for the parameters needed by the constructor in each class. (There is no need to ask user to input data - so don’t include the Scanner class!).
Then in the main method, use each object created above to invoke the necessary methods, so that you can calculate each object’s transaction fee and mileage award, then output the transaction fee and mileage award.
Keep 2-digit precision in the output for transaction fee, and keep 1-digit precision in output for mileage award.
/** JohnDoeTest.java, and replace JohnDoe with your name*/
Please up vote ,comment if any query . Thanks for Question .Be safe .
Note : check attached image for output .
Program : *************************CreditCard.java***********************************
public class CreditCard {
private double domesticSpending; //private
variable
private double foreignSpending;
private double basicFeeRate;
public CreditCard() //default constructor
{
this.domesticSpending =
0.0;
this.foreignSpending =
0.0;
this.basicFeeRate =
0.0;
}
//parameter constructor
public CreditCard(double domesticSpending,
double foreignSpending, double basicFeeRate) {
this.domesticSpending =
domesticSpending;
this.foreignSpending =
foreignSpending;
this.basicFeeRate =
basicFeeRate;
}
//getter and setter
public double getDomesticSpending() {
return
domesticSpending;
}
public void setDomesticSpending(double
domesticSpending) {
this.domesticSpending =
domesticSpending;
}
public double getForeignSpending() {
return
foreignSpending;
}
public void setForeignSpending(double
foreignSpending) {
this.foreignSpending =
foreignSpending;
}
public double getBasicFeeRate() {
return
basicFeeRate;
}
public void setBasicFeeRate(double
basicFeeRate) {
this.basicFeeRate =
basicFeeRate;
}
//basic fee calculation
//foreignSpending= $50
//domestingSpending=$100
//basicFeeRate=2%
//fee= (f+d)*2/100
public double calcBasicFee()
{
double
basicFee=(this.foreignSpending+this.domesticSpending)*this.basicFeeRate/100.0;
return basicFee;
}
//mileage
//1 mile award for every $20 spend
public double calcBasicMileage()
{
return
(this.foreignSpending+this.domesticSpending)/20.0;
}
}
Program : ******************ExpressCard.java**********************
public class ExpressCard extends CreditCard{//inherit Credit Card
Class
private double foreignFeeRate;
private double domesticBonusMileageRate;
public ExpressCard() { //default
constructor
this.foreignFeeRate =
0.0;
this.domesticBonusMileageRate = 0.0;
}
//parameter constructor
//assign to local class variable
//and credit class data
public ExpressCard(double foreignFeeRate, double
domesticBonusMileageRate, double domesticSpending, double
foreignSpending, double basicFeeRate) {
super(domesticSpending,
foreignSpending, basicFeeRate);
this.foreignFeeRate =
foreignFeeRate;
this.domesticBonusMileageRate = domesticBonusMileageRate;
}
//getter and setter method
public double getForeignFeeRate() {
return
foreignFeeRate;
}
public void setForeignFeeRate(double
foreignFeeRate) {
this.foreignFeeRate =
foreignFeeRate;
}
public double getDomesticBonusMileageRate()
{
return
domesticBonusMileageRate;
}
public void
setDomesticBonusMileageRate(double domesticBonusMileageRate)
{
this.domesticBonusMileageRate = domesticBonusMileageRate;
}
//% of foreignspending with foreign spending
rate
// add with calculate basic fee method of super
class
public double calcFee()
{
return
(super.getForeignSpending()*this.foreignFeeRate/100.0)+super.calcBasicFee();
}
//same add basic mileage of super class
//with domesticspending % with domestic mileage
rate
public double calcMileage()
{
return
(super.getDomesticSpending()*this.domesticBonusMileageRate/100.0)+super.calcBasicMileage();
}
}
Program : ****************************PremiumCard.java********************************
public class PremiumCard extends CreditCard{
private double annualMemberFee;
private double foreignBonusMileageRate;
//default constructor
public PremiumCard() {
this.annualMemberFee=0;
this.foreignBonusMileageRate=0;
}
//parameter constructor with credit card memeber
assign
public PremiumCard(double annualMemberFee,
double foreignBonusMileageRate, double domesticSpending, double
foreignSpending, double basicFeeRate) {
super(domesticSpending,
foreignSpending, basicFeeRate);
this.annualMemberFee =
annualMemberFee;
this.foreignBonusMileageRate = foreignBonusMileageRate;
}
//getter and setter method
public double getAnnualMemberFee() {
return
annualMemberFee;
}
public void setAnnualMemberFee(double
annualMemberFee) {
this.annualMemberFee =
annualMemberFee;
}
public double getForeignBonusMileageRate()
{
return
foreignBonusMileageRate;
}
public void setForeignBonusMileageRate(double
foreignBonusMileageRate) {
this.foreignBonusMileageRate = foreignBonusMileageRate;
}
//sum of basic fee + annual member fee
public double calcFee()
{
return
super.calcBasicFee()+this.annualMemberFee;
}
//mileage
// basic mileage of super class + % of foreign
spending with foreign spending rate
public double calcMileage()
{
return
super.calcBasicMileage()+(super.getForeignSpending()*this.foreignBonusMileageRate/100.0);
}
}
Program : Main class **************************JohnDoeTest.java*****************************
public class JohnDoeTest {
public static void main(String[] args) {
//create hard code value
for better understannding
double
domesticSpending=100.0;
double foreignSpending
=50.0;
double basicFeeRate =
2.0;
double
foreignFeeRate=3.0;
double
domesticBonusMileageRate=4;
double
annualMemberFee=65.0;
double
foreignBonusMileageRate=20;
//set value to Express
Card class
ExpressCard
expressObj=new
ExpressCard(foreignFeeRate,domesticBonusMileageRate,
domesticSpending,foreignSpending,basicFeeRate);
//set value of premium
card class
PremiumCard
premiumObj=new
PremiumCard(annualMemberFee,foreignBonusMileageRate,domesticSpending,foreignSpending,basicFeeRate);
//print fee for express
card and mileage
System.out.printf("Express Card Fee :
$%.2f\n",expressObj.calcFee());
System.out.printf("Express Card Mileage Award :%.1f
miles.\n",expressObj.calcMileage());
//fee for premium card
and mileage
System.out.printf("Premium Card Fee :
$%.2f\n",premiumObj.calcFee());
System.out.printf("Premium Card Mileage Award : %.1f
miles.\n",premiumObj.calcMileage());
}
}
Output :
Please up vote ,comment if any query . Be safe .