Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called Purse. Purse should have the following: three int instance

ID: 3722426 • Letter: W

Question

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

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

an accessor method String toString() that returns a String representation of the purse (such as "pounds 3, shillings 10, pence 5")

Create : 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 pence sophie 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

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

// Purse.java

public class Purse {

                private int pounds;

                private int shillings;

                private int pence;

                //initializes all fields to 0

                public Purse() {

                                pounds = 0;

                                shillings = 0;

                                pence = 0;

                }

                /**

                * method to add pounds

                */

                public void addPounds(int poundsToAdd) {

                                pounds += poundsToAdd;

                }

                /**

                * method to add shillings

                */

                public void addShillings(int shillingsToAdd) {

                                /**

                                * Adding shilling

                                */

                                shillings += shillingsToAdd;

                                /**

                                * Checking if shilling is greater than or equal to 20, if yes, adding 1

                                * pound , removing 20 shillings until shillings comes under 20

                                */

                                while (shillings >= 20) {

                                                pounds++;

                                                shillings = shillings - 20;

                                }

                }

                /**

                * method to add pence

                */

                public void addPence(int penceToAdd) {

                                /**

                                * Adding pence

                                */

                                pence += penceToAdd;

                                /**

                                * Checking if pence is greater than or equal to 12, if yes, adding 1

                                * shilling , removing 12 pence until pence comes under 12

                                */

                                while (pence >= 12) {

                                                /**

                                                * Adding one shilling, this will update the pounds also, if

                                                * necessary

                                                */

                                                addShillings(1);

                                                pence = pence - 12;

                                }

                }

                /* getter methods */

                public int getShillings() {

                                return shillings;

                }

                public int getPounds() {

                                return pounds;

                }

                public int getPence() {

                                return pence;

                }

                public void spend(int poundsToRemove, int shillingsToRemove,

                                                int penceToRemove) {

                                /**

                                * Converting whole money to be spent into pence (pounds*20*12 = pounds

                                * in pence) (shillings*12 = shillings in pence)

                                */

                                int totalPencesToRemove = (poundsToRemove * 20 * 12)

                                                                + (shillingsToRemove * 12) + penceToRemove;

                                int i = 0;

                                /**

                                * loops until required number of pennies are removed

                                */

                                while (i < totalPencesToRemove) {

                                                pence -= 1; /* removing one penny */

                                                /**

                                                * checking if pence went less than 0

                                                */

                                                if (pence < 0) {

                                                                /**

                                                                * Removing one shilling

                                                                */

                                                                shillings -= 1;

                                                                pence = 11;

                                                }

                                                /**

                                                * checking if shilling went less than 0

                                                */

                                                if (shillings < 0) {

                                                                /**

                                                                * Removing one pound

                                                                */

                                                                pounds -= 1;

                                                                shillings = 19;

                                                }

                                                i++;

                                }

                }

                /**

                * Returns a string containing money in pounds, shillings and pence

                */

                public String toString() {

                                return "pounds " + pounds + ", shillings " + shillings + ", pence "

                                                                + pence;

                }

}

// PurseTester.java

import java.util.Scanner;

public class PurseTester {

                public static void main(String[] args) {

                                Scanner scanner = new Scanner(System.in);

                                /**

                                * Defining two Purse objects for sophie and sally

                                */

                                Purse sophie = new Purse();

                                Purse sally = new Purse();

                                int pounds, shillings, pence;

                                /**

                                * looping for two times

                                */

                                for (int i = 0; i < 2; i++) {

                                                /**

                                                * Getting the money to be added

                                                */

                                                System.out.println("For sophie:");

                                                System.out.println("Enter the pounds to be added");

                                                pounds = Integer.parseInt(scanner.nextLine());

                                                sophie.addPounds(pounds);

                                                System.out.println("Enter the shillings to be added");

                                                shillings = Integer.parseInt(scanner.nextLine());

                                                sophie.addShillings(shillings);

                                                System.out.println("Enter the pence to be added");

                                                pence = Integer.parseInt(scanner.nextLine());

                                                sophie.addPence(pence);

                                                System.out.println("For sally:");

                                                System.out.println("Enter the pounds to be added");

                                                pounds = Integer.parseInt(scanner.nextLine());

                                                sally.addPounds(pounds);

                                                System.out.println("Enter the shillings to be added");

                                                shillings = Integer.parseInt(scanner.nextLine());

                                                sally.addShillings(shillings);

                                                System.out.println("Enter the pence to be added");

                                                pence = Integer.parseInt(scanner.nextLine());

                                                sally.addPence(pence);

                                                /**

                                                * Display the current money in purses

                                                */

                                                System.out.println(" Money in sophie's purse: " + sophie);

                                                System.out.println("Money in sally's purse: " + sally);

                                                /**

                                                * Getting money to be spent

                                                */

                                                System.out.println("For sophie:");

                                                System.out.println("Enter the pounds to be spent");

                                                pounds = Integer.parseInt(scanner.nextLine());

                                                System.out.println("Enter the shillings to be spent");

                                                shillings = Integer.parseInt(scanner.nextLine());

                                                System.out.println("Enter the pence to be spent");

                                                pence = Integer.parseInt(scanner.nextLine());

                                                /**

                                                * spending money

                                                */

                                                sophie.spend(pounds, shillings, pence);

                                                System.out.println("For sally:");

                                                System.out.println("Enter the pounds to be spent");

                                                pounds = Integer.parseInt(scanner.nextLine());

                                                System.out.println("Enter the shillings to be spent");

                                                shillings = Integer.parseInt(scanner.nextLine());

                                                System.out.println("Enter the pence to be spent");

                                                pence = Integer.parseInt(scanner.nextLine());

                                                /**

                                                * spending money

                                                */

                                                sally.spend(pounds, shillings, pence);

                                                /**

                                                * Display the current money in purses

                                                */

                                                System.out.println(" Money in sophie's purse: " + sophie);

                                                System.out.println("Money in sally's purse: " + sally);

                                }

                }

}

/*OUTPUT*/

For sophie:

Enter the pounds to be added

10

Enter the shillings to be added

23

Enter the pence to be added

13

For sally:

Enter the pounds to be added

5

Enter the shillings to be added

0

Enter the pence to be added

14

Money in sophie's purse: pounds 11, shillings 4, pence 1

Money in sally's purse: pounds 5, shillings 1, pence 2

For sophie:

Enter the pounds to be spent

5

Enter the shillings to be spent

3

Enter the pence to be spent

13

For sally:

Enter the pounds to be spent

0

Enter the shillings to be spent

0

Enter the pence to be spent

24

Money in sophie's purse: pounds 6, shillings 0, pence 0

Money in sally's purse: pounds 4, shillings 19, pence 2

For sophie:

Enter the pounds to be added

7

Enter the shillings to be added

0

Enter the pence to be added

1

For sally:

Enter the pounds to be added

10

Enter the shillings to be added

10

Enter the pence to be added

10

Money in sophie's purse: pounds 13, shillings 0, pence 1

Money in sally's purse: pounds 15, shillings 10, pence 0

For sophie:

Enter the pounds to be spent

10

Enter the shillings to be spent

0

Enter the pence to be spent

1

For sally:

Enter the pounds to be spent

10

Enter the shillings to be spent

11

Enter the pence to be spent

0

Money in sophie's purse: pounds 3, shillings 0, pence 0

Money in sally's purse: pounds 4, shillings 19, pence 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote