Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Threads assignment Java A bank has asked your company to create a program for them that will address the following: The bank has a customer

Threads assignment Java

A bank has asked your company to create a program for them that will address the following:

The bank has a customer that has set up an account for his 18 year old son, who is about to be away at

college. The bank is going to automatically transfer money from the customers account into the sons

account once per week. As the son has not yet learned how to budget his money, the customer wants

to provide a reasonable amount of spending money, but at the same time limit the amount of money

the son has access to. Thus the bank has tasked your company to create a program that will address the

deposits and withdrawal functionality of the sons account. They want the following behavior:

? For each deposit that is made into the sons account, he can make a onetime withdrawal of the

entire amount that was deposited. He cannot make a partial withdrawal. He has to withdraw

the entire amount of the deposit.

? He has to make his withdrawal after each deposit.

? No additional deposits can be made to his account until he has made the withdrawal.

Your company has no experience with Java threaded applications, so your manager has to be convinced

that the behavior specified above can be made to work with a threaded application. So he has tasked

you with producing a proof of concept prototype that will demonstrate this.

Your instructions for this assignment are to do this:

1. Create a class named Account. It will contain a field that will represent the balance in the

account. It will contain one value. Since this is just a proof of concept program, for simplicity,

this value will be an integer. This field will be named balance.

a) Class Account will have two methods: void deposit(int num), and void withdraw().

b) void deposit(int num) will save the value of num to balance, and will then print the

following deposit: + balance.

c) void withdraw() will print the following: withdraw: + balance.

d) You are to use static Semaphores to prevent the withdraw and deposit methods from

being executed at the same time.

2. Create a class named Deposit that will implement Runnable. This class will execute a loop that

will write 10 values into the balance in Account by calling the deposit method in the Account

class . You may use integers 1 through 10 if you wish. Remember, our proof of concept

program is just to demonstrate that we can do this with threads. You will create a constructor

that has an Account parameter.

3. Create a class named Withdrawal that will implement Runnable. This class will execute a loop

that will call the withdraw method in the Account class. This loop will loop 10 times. You will

create a constructor that has an Account parameter.

4. Create a class named ThreadedAccountDemo. This class will contain a main method. In the

main method you will create an Account object, and call the Deposit and Withdrawal

constructors passing it the reference to the Account object. You will also use the join() method

to prevent the main thread from terminating prior to the Deposit and Withdrawal thre

5. Do not use a time delay to force the deposit and withdraw threads to alternate.

Hint: you might want to try a semaphore

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions