Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Looking for some guidance on this lab using Purse class This program expands on the Alexander Hamilton We Love You program you wrote earlier to

Looking for some guidance on this lab using Purse class

This program expands on the Alexander Hamilton We Love You program you wrote earlier to handle British Pounds, Shillings and Pence.

A class called Purse. Purse should have the following:

  • three int instance variables: pounds, shillings and pence.
  • a no argument constructor (i.e., Purse())that initializes the instance variables to 0.
  • a mutator method void addPounds(int poundsToAdd)
  • a mutator method void addShillings(int shillingsToAdd) You may assume shillingsToAdd is less than 20. Note that if addShillings increases the number of shillings to 20 or more, the pounds value should be increased until shillings is less than 20.
  • a mutator method void addPence(int penceToAdd) You may assume penceToAdd is less than 12. Note that if addPence increases the number of pence to 12 or more, the shillings value should be increased until pence is less than 12.
  • anaccessor method int getPounds() that returns the number of pounds in the purse
  • an accessor method int getShillings() that returns the number of shillings (<20) in the purse.
  • an accessor method int getPence() that returns the number of pence (<12) in the purse.
  • a mutator method void spend(int poundsToRemove, int shillingsToRemove, int penceToRemove) that removes the specified amount of money from the purse. You may assume the purse has sufficient total funds to cover the withdrawal, though you may have to break a pound into shillings, or a shilling into pence.
  • anaccessormethod String toString() that returns a String representation of the purse (such as "pounds 3, shillings 10, pence 5")

A tester class, PurseTester, is attached for your use. PurseTester has only a single main method, and does the following:

  • creates two Purse objects, sophie and sally
  • has a loop whose body executes two times. For each execution of the loop, it
    • asks the user for the pounds, shillings and pencesophie and sally (each) should add to their purses
    • displays the amount of money in each purse
    • asks the user for the pounds, shillings and pence sophie and sally (each) should spend
    • displays the amount of money in each purse

Grading Elements:

  • Purse has the specified instance variables, and only the specified instance variables
  • Purse has the specified constructor and methods
  • All methods have the correct return type, and signature
  • All methods have the correct behavior
  • PurseTesterworks with Purse as expected

Here is the what I have so far

PurseMain.java

import java.util.Scanner; public class PurseMain { public static void main(String[] args) { Scanner kybd = new Scanner(System.in); Purse sophie = new Purse(); Purse sally = new Purse(); System.out.print("Enter 1 for Sophie, 2 for Sally, or 0 to exit: "); int operation = kybd.nextInt(); while (operation != 0) { Purse currentPurse; // alias greatly simplifies things later if (operation == 1) { currentPurse = sophie; } else if (operation == 2) { currentPurse = sally; } else { currentPurse = null; // will eventually give null ptr on invalid input } System.out.print("Enter 1 to give pence, 2 to give shillings, " + "3 to query her purse: "); int giveOrQuery = kybd.nextInt(); if (giveOrQuery == 1) { System.out.print("Enter the pence to give: "); int amt = kybd.nextInt(); currentPurse.addPence(amt); } else if (giveOrQuery == 2) { System.out.print("Enter the shillings to give: "); int amt = kybd.nextInt(); currentPurse.addShillings(amt); } else if (giveOrQuery == 3) { System.out.println(currentPurse); } System.out.print("Enter 1 for Sophie, 2 for Sally, or 0 to exit: "); operation = kybd.nextInt(); } kybd.close(); } }

I just need assistance on direction for Purse.Java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Legal Environment of Business A Managerial Approach Theory to Practice

Authors: Sean Melvin, Enrique Guerra Pujol

3rd edition

1259686205, 978-1259686207

More Books

Students also viewed these Programming questions