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

Develop and implement a class SavingsAccount that represents a savings account.

ID: 3649464 • Letter: D

Question

Develop and implement a class SavingsAccount that represents a savings account. A SavingsAccount object should have three attributes: the name of the account (a String), the owner of the account (a String), and the current balance (a double). The class should provide the following constructors and methods: SavingsAccount(): configures a new savings account with name "name", owner "owner", and balance 0.0. SavingsAccount(String s, String t, double b): configures a new account with name s, owner t, and balance b. void deposit(double b): adds amount b to the current balance of the associated account. void withdraw(double b): subtracts amount b from the current balance of the associated account. void close(): zeroes out the account balance and sets the owner to "". String getAccountName(): returns the name of the associated account. String getOwner(): returns the owner of the associated account. double getBalance(): returns the balance of the associated account. void setName(String s): sets the name of the associated account to s. void setOwner(String s): sets the owner of the associated account to s. String toString(): returns a textual representation of the attributes of the associated account. Include a main method in your SavingsAccount class that tests all of the constructors and methods you have defined.

Explanation / Answer

public class AccountTest
{
public static void main(String[] args)
{
Account account1 = new Account();
System.out.println(account1);
Account acct = new Account("John Adams", "John Adams", 100.0);
acct.deposit(1000.0);
System.out.println("acct balance = " + acct.getBalance());
acct.close();
System.out.println(acct);
Account freds = new Account("freds party account", "Fred Smith", 50.0);
System.out.println("name = " + freds.getAccountName() + " " +
"owner = " + freds.getOwner() + " " +
"balance = " + freds.getBalance());
freds.withdraw(2.59);
System.out.println(freds);
}
}

class Account
{
String name;
String owner;
double balance;

Account()
{
name = "name";
owner = "owner";
balance = 0;
}

Account(String s, String t, double n)
{
name = s;
owner = t;
balance = n;
}

void deposit(double n)
{
balance = balance + n;
}

void withdraw(double n)
{
balance = balance - n;
}

void close()
{
owner = "";
balance = 0;
}

String getAccountName()
{
return name;
}

String getOwner()
{
return owner;
}

double getBalance()
{
return balance;
}

void setName(String s)
{
name = s;
}

void setOwner(String s)
{
owner = s;
}

public String toString()
{
return "name: " + name + ", owner: " + owner + ", balance: " + balance;
}
}