THINK FIRST·CODE LATER

← All labs

What methods can and cannot change

Problem

Write a class Wallet (owner, private balance, deposit, withdraw, toString → Lina: 120) and a public class TransferDemo with three static methods:

  • boolean transfer(Wallet from, Wallet to, int amount) — refuses amounts ≤ 0 or above from's balance;
  • void swapWallets(Wallet a, Wallet b) — swaps its two parameters;
  • void doubleIt(int value) — doubles its parameter.

main reads two wallets (name balance name balance) and then transfer amounts until the end of input, printing transfer 50: done -> Lina: 50, Omar: 70 (or refused). Finally call swapWallets(first, second) and doubleIt(total) and print

after swapWallets: first is still Lina
after doubleIt: total is still 120

Explain in one sentence why transfer works but the other two do not.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l11-4-data-1.txt

Lina 100 Omar 20 50 80 -5 30
What your program must print for l11-4-data-1.txt
transfer 50: done -> Lina: 50, Omar: 70
transfer 80: refused -> Lina: 50, Omar: 70
transfer -5: refused -> Lina: 50, Omar: 70
transfer 30: done -> Lina: 20, Omar: 100
after swapWallets: first is still Lina
after doubleIt: total is still 120

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l11-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l11-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.