Question

In Java pls

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

////////////////////////////////////////

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

Please find below the code of the GiftCard class with changes highlighted in bold. It is well explained inside the code using comments.

public class GiftCard {

    private double balance;

    public GiftCard(double initialBalance) {
        // sets the balance
        setBalance(initialBalance);

    }

    public GiftCard() {
    }

    public void setBalance(double newBalance) {
        // if newBalance is negative, display error message to user
        if (newBalance < 0) {
            System.out.println("Balance can't be negative.");
        } else {
            // else set the balance
            balance = newBalance;
        }

    }

    public void deduct(double amount) {
        // if amount is greater than baance, display error message to user
        if (amount > balance) {
            System.out.println("Insufficient Balance!");
        } else if (amount < 0) {
            // if amount is negatice, display error message to user
            System.out.println("Can't deduct negative amount!");
        } else {
            // deduct the amount from balance
            balance -= amount;
        }

    }

    public void report() {
        // display the remaining balance on gift card
        System.out.println("$" + balance);

    }
}

Below is the output that the driver gives:

run $12.0 Insufficient Balance! $12.0 $9.5 $40.0 $9.5 Cant deduct negative amount! $20.0 Balance cant be negative $0.0 $15.

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
In Java pls //////////////////////////////////////// and here is the Giftcard.java copyable //////////////////////////...
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
  • Java template public class GiftCard { // Instance Variables // TODO: balance // Constructors /** *...

    Java 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...

  • 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...

  • 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...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to...

    please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to be done! * * TODO#1: Change the name and date accordingly * Created by , 11/25/2019 */ public class CS140_Arrays_Done { //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3) //array for the values private static double[][] values = { { 9, 10, 8 }, { 3.5, 10, 8.5 }, { 10, 8.5, 9 }, { 8.5, 10,...

  • 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...

  • Programmed in Java Pls - The other answer to this question is incorrect.: Your program should...

    Programmed in Java Pls - The other answer to this question is incorrect.: Your program should follow the below requirements: You will only need one class for this project called StudentReport in the package ilstu.edu In your class you will have the following instance variables: grades: a double 2d array that will hold the grades for all the students. Students: a 1d String array that will hold the names of all the students. In your class you will have the...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • 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...

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