Question

Java

Part 1 In this section well build a small class together, useful for managing a gift card balance. This class will keep trac

template

public class GiftCard
{
    // Instance Variables
    // TODO: balance

    // Constructors

    /**
     * GiftCard(double initialBalance)
     * 
     * Initialize a GiftCard object with a pre-defined amount of money.
     * 
     * @param initialBalance    double  The desired starting balance
     */
    public GiftCard(double initialBalance) {
        setBalance(initialBalance);
    }

    /**
     * GiftCard()
     * 
     * Initialize a GiftCard object with a balance of $0.00.
     */
    public GiftCard() {}

    // Instance Methods
    /**
     * setBalance
     * 
     * This should set the balance of the gift card to newBalance.
     * The balance should be set to zero and complain if the newbalance is less than
     * zero.
     * 
     * @param newBalance    double  The target balance.
     */
    public void setBalance(double newBalance) {
        // TODO: fill in this method
    }

    /**
     * deduct
     * 
     * This should reduce the balance of the gift card by amount. If the amount is 
     * negative, complain and leave the balance unchanged. If the new balance is
     * negative, complain and leave the balance unchanged.
     * 
     * @param newBalance    double  The target balance.
     */
    public void deduct(double amount) {
        // TODO: fill in this method
    }

    /**
     * report
     * 
     * Print the current balance on the card. 
     */
    public void report() {
        // TODO: fill in this method
    }

    // Driver
    public static void main (String[] args) {
        GiftCard card0 = new GiftCard();
        card0.setBalance(12.00);
        card0.report(); // Should print $12.00
        card0.deduct(25); // Should complain
        card0.report(); // Should print $12.00
        card0.deduct(2.50);
        card0.report(); // Should print $9.50

        GiftCard card1 = new GiftCard(40);
        card1.report(); // Should print $40.00
        card0.report(); // Should Print $9.50
        card1.deduct(20);
        card1.deduct(-20); // Should complain
        card1.report(); // Should print $20.00

        GiftCard card2 = new GiftCard(-20.05); // Should complain
        card2.report(); // Should print $0.00
        card2.setBalance(15); 
        card2.report(); // Should print $15.00
        card1.report(); // Should print $20.00
        card0.report(); // SHould print $9.50
    }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class GiftCard

{
double balance;

public void set Balance(double newBal)

{
if (newBal < 0)
{
System.out.println("ERROR : Balance should not be negetive");
}
else
{

balance = newBal;
}
}

public void deduct(double amount)
{
if(amount < 0)
{
System.out.println("ERROR : Amount should not be negetive");
}
else
{

balance = balance - amount;
}

}

public void report()
{
System.out.print(" "+balance+"\n");
}

public void resetToZero()
{
balance = 0;
}

public static void main(String args[])
{
GiftCard card1 = new GiftCard();

card1.setBalance(50);

System.out.print("card1 is :");
card1.report();

GiftCard card2 = new GiftCard();

card2.setBalance(100);
System.out.print("card2 is :");
card2.report();

card2.deduct(50);
System.out.print("After deducting 50, card2 is :");
card2.report();
}

}

Output:

Output JavaApplication3 (run) DD run card1 is 50.0 card2 is: 100.0 After deducting 50, car d2 is 50.0 BUILD SUCCESSFUL (total


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Java template public class GiftCard { // Instance Variables // TODO: balance // Constructors /** *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java pls //////////////////////////////////////// and here is the Giftcard.java copyable //////////////////////////...

    In Java pls //////////////////////////////////////// and here is the Giftcard.java copyable ///////////////////////////////////////////////////////////////////// /* * GiftCard.java * * Lab 10 Part 1 * * Authors: Samantha Smith, you */ public class GiftCard { // Instance Variables // TODO: balance // Constructors /** * GiftCard(double initialBalance) * * Initialize a GiftCard object with a pre-defined amount of money. * * @param initialBalance double The desired starting balance */ public GiftCard(double initialBalance) { setBalance(initialBalance); } /** * GiftCard() * * Initialize a GiftCard object...

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • Please answer using java. For this lab you will practice accessing variables in an array and...

    Please answer using java. For this lab you will practice accessing variables in an array and determining if the values are odd. You will finish the method makeOddArray. You will store only the odd values in another array and return that array. Hint: you will need to keep track of how many items you have currently put in the new array and increment that value as you add. This will keep track of your index for the new array. Below...

  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • use java follow my code: public class q1 {    // Instance Variables    int currentFloor;...

    use java follow my code: public class q1 {    // Instance Variables    int currentFloor;    double maximumWeight;    int topFloor;       // Constructor Declaration of Class    public q1 (int currentFloor, double maximumWeight, int topFloor)    {    this.currentFloor = currentFloor;    this.maximumWeight = maximumWeight;    this.topFloor = topFloor;    }       // Property to get value of currentFloor instance variable    public int getCurrentFloor()    {    return currentFloor;    }       // Property...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes....

    Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding in classes that are instantiated as integer classes, you may just use the default rounding. You will add an additional data member, method, and bank account type that inherits SavingsAc-count ("CD", or certicate of deposit). Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT