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

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

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

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

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

  • Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the...

    Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the implementation of the DataSet class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Please use the code provided and fill in what is needed. BankAccount.java /**    A bank account has a...

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

  • •create a new savings account object (for choice 2). •ask the user to enter an initial...

    •create a new savings account object (for choice 2). •ask the user to enter an initial balance which should be greater than 50. If the entered amount is less than 50, the program will ask the user to enter another amount. This continues until an amount greater than 50 is entered. •Set the balance of the savings account object by calling the setBalance() method. •Ask the user to deposit an amount. Deposit that amount and display the balance of the...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

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