Java Programming: public class AccountDriver { public static void main(String[]
ID: 3669238 • Letter: J
Question
Java Programming:
public class AccountDriver {
public static void main(String[] args) {
Account myAccount = new Account(127892, 2000, 0.2);
System.out.println(myAccount.toString());
//Add $100 to balance
myAccount.deposit(100);
System.out.println(myAccount.toString());
//subtract $400 from balance
myAccount.withDraw(400);
System.out.println(myAccount.toString());
}
public String toString() {
String ret = "Account Summary: ";
ret = "Balance: " + balance;
return ret;
}
}
Explanation / Answer
AccountDriver.java
// class
public class AccountDriver {
public static void main(String[] args)
{
// create object of class Account
Account myAccount = new Account(127892, 2000, 0.2);
// call function
myAccount.withdraw(2500);
myAccount.deposit(3000);
// display result
System.out.println("The balance of the account is $" +myAccount.getBalance());
System.out.println("The monthly interest rate is " +myAccount.getMonthlyInterestRate()+ "%");
System.out.println("The account was created " +myAccount.getDateCreated());
}
}
Account.java
import java.util.Date;
public class Account {
// declare variables
private int id;
private double balance;
private double annualInterestRate;
Date dateCreated;
// constructor
public Account()
{
id = 0101;
balance = 100;
annualInterestRate = .15;
dateCreated = new Date();
}
// con with parameter
public Account(int id, double balance, double annualInterestRate)
{
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new Date();
}
// getting id
public double getId()
{
return id;
}
// getting balance
public double getBalance()
{
return balance;
}
//get interest rate
public double getAnnualInterestRate()
{
return annualInterestRate;
}
// set id
public void setId(int id)
{
this.id = id;
}
// set balance
public void setBalance(double balance)
{
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
// created date
public Date getDateCreated()
{
return new Date();
}
public double getMonthlyInterestRate()
{
return annualInterestRate/12;
}
public void withdraw(double balance)
{
this.balance -= balance;
}
public void deposit(double balance)
{
this.balance += balance;
}
}
output
The balance of the account is $2500.0
The monthly interest rate is 0.016666666666666666%
The account was created Thu Feb 18 09:06:54 UTC 2016
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.