Question: A Basic Object In this section we ll build a small class together ( starting from the above template ) , useful for managing a

A Basic Object
In this section well build a small class together (starting from the above template), useful for managing a gift card balance. This class will keep track of the total balance and will allow for a starting total, deductions, and a balance report. To manage such details, well need a class-level (or instance) variable to store the balance, as well as a few methods to implement the actions mentioned above. Download GiftCard.java and look at the main method.
Explain to your partner what is happening in each line of the main method of GiftCard. How do card1, card2, and card3 differ from each other, after each line of the code?
(Tip: Just write a comment in your code for this.)
Add a class-level variable to track the balance at the top of the file. Should this variable be a char? A double?
(Tip: A class-level variable is like a normal variable, but it is inside of a class and can only be accessed by accessing an instance of a class too. It's just like what you've already been doing since all Java programs are inside one big class, just make sure the variables are above your constructor.)
Add a constructor that will set the class-level instance variable you defined earlier when you create an instance of GiftCard. Check the main method for places where the GiftCard constructor is used, and make sure that constructor takes in the correct number of parameters.
(Tip: remember, a constructor is for initializing vital variables of a class that may not always be the same value. In this case, thebalance variable is the one we want to initialize (because it might be different for each gift card), and so our constructor should have one parameter. Inside of the constructor, you should set thebalancevariable to whatever the parameter value, or argument, is equal to.)
Add a void method setBalance(double newBalance) that resets the balance.
(Tip: All this does is set the balance variable to a new value, whatever newBalance is equal to. This is called asetter ormutatormethod.)
Make this function fail on negative input
(Tip: if-statement inside of the setBalance method, before you actually set balance to be equal to newBalance)
Add a void method deduct(double amount) method that subtracts from the balance
(Tip: This is another mutator method, but instead of completely replacing the old balance, you just do some simple subtraction. Keep in mind both of these methods will be before yourmain method.)
Make this function fail on a negative balance
Add a void method report() that prints out the balance remaining on the gift card.
(Tip: Print the value of balance to the console usingSystem.out.print().)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!