Please do not give solution in image format thanku I'mwriting code for an assignment that I'll post below. I was hoping for some help with creating my driver for the new lockable Account Class. Assignment Question: Design a Java interface called Lockable that includes the following methods: setKey, lock, unlock, and locked. The setKey, lock, and unlock methods take an integer parameter that represent the key. The setKey method establishes the key. The lock and unlock methods lock and unlock the object, but only if the key passed in is correct. The locked method returns a boolean that indicates whether or not the object is locked. A Lockable object represents an object whose regular methods (Note: you need to modify these methods) will perform the tasks only if the object is unlocked. If the object is locked, then the methods will not perform the requested task (e.g., if the task is to "deposit" to an Account object, no deposit will be done. The method will simply return the current balance of the object); if the object is unlocked, the requested task can be performed. Now, redesign the Coin class (Listing 5.4, p. 186) and Account class (Listing 5.7, p. 192) from Chapter 5 in your course textbook so that they are Lockable. Create a driver class to demonstrate the behaviours of Lockable objects using Account and Coin classes (Note: you may have two separate demonstrations using 2 different driver classes, one for Account and another for Coin) Lockable Interface: public interface Lockable { public void setKey(int newkey); public void lock(int key); public void unlock(int key); public boolean locked(); } Account Class: import java.text.NumberFormat; public class Account implements Lockable { private final double RATE = 0.035; private String name; private long acctNumber; private double balance; private int key; private boolean isLocked; public Account(String owner, long account, double initial) { name = owner; acctNumber = account; balance = initial; isLocked = false; } public double deposit (double amount) { if (isLocked) { return balance; } if (amount > 0) { balance += amount; } return balance; } public double withdraw (double amount, double fee) { if (isLocked) { return balance; } if (amount + fee > 0 && amount + fee <= balance) { balance -= (amount + fee); } return balance; } public double addInterest() { if (isLocked) { return balance; } balance += (balance * RATE); return balance; } public double getBalance() { return balance; } public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber + "\t" + name + "\t" + fmt.format(balance)); } public void setKey (int key) { this.key = key; } public void lock (int key) { if (this.key == key) { isLocked = true; } } public void unlock (int key) { if (this.key == key) { isLocked = false; } } public boolean locked() { return isLocked; } }
Please do not give solution in image format thanku
I'mwriting code for an assignment that I'll post below. I was hoping for some help with creating my driver for the new lockable Account Class.
Assignment Question:
Design a Java interface called Lockable that includes the following methods: setKey, lock, unlock, and locked. The setKey, lock, and unlock methods take an integer parameter that represent the key. The setKey method establishes the key. The lock and unlock methods lock and unlock the object, but only if the key passed in is correct. The locked method returns a boolean that indicates whether or not the object is locked. A Lockable object represents an object whose regular methods (Note: you need to modify these methods) will perform the tasks only if the object is unlocked. If the object is locked, then the methods will not perform the requested task (e.g., if the task is to "deposit" to an Account object, no deposit will be done. The method will simply return the current balance of the object); if the object is unlocked, the requested task can be performed. Now, redesign the Coin class (Listing 5.4, p. 186) and Account class (Listing 5.7, p. 192) from Chapter 5 in your course textbook so that they are Lockable. Create a driver class to demonstrate the behaviours of Lockable objects using Account and Coin classes (Note: you may have two separate demonstrations using 2 different driver classes, one for Account and another for Coin)
Lockable Interface:
public interface Lockable
{
public void setKey(int newkey);
public void lock(int key);
public void unlock(int key);
public boolean locked();
}
Account Class:
import java.text.NumberFormat;
public class Account implements Lockable
{
private final double RATE = 0.035;
private String name;
private long acctNumber;
private double balance;
private int key;
private boolean isLocked;
public Account(String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
isLocked = false;
}
public double deposit (double amount)
{
if (isLocked)
{
return balance;
}
if (amount > 0)
{
balance += amount;
}
return balance;
}
public double withdraw (double amount, double fee)
{
if (isLocked)
{
return balance;
}
if (amount + fee > 0 && amount + fee <= balance)
{
balance -= (amount + fee);
}
return balance;
}
public double addInterest()
{
if (isLocked)
{
return balance;
}
balance += (balance * RATE);
return balance;
}
public double getBalance()
{
return balance;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
public void setKey (int key)
{
this.key = key;
}
public void lock (int key)
{
if (this.key == key)
{
isLocked = true;
}
}
public void unlock (int key)
{
if (this.key == key)
{
isLocked = false;
}
}
public boolean locked()
{
return isLocked;
}
}
Step by step
Solved in 4 steps with 5 images