Hello, I have the majority of this code done and I can\'t find a way to get it t
ID: 3716731 • Letter: H
Question
Hello, I have the majority of this code done and I can't find a way to get it to run successfully. Any help? It's the design a class named account assignment. Here is my code:
import java.util.*;
public class Account
{
private int id;
private double balance;
private static double annualInterestRate;
private Date dateCreated;
public Account()
{
dateCreated = new Date();
id=0;
balance=0;
}
{
setID(id);
setBalance(balance);
dateCreated = new Date();
}
public int getID()
{
return id;
}
public void setID(int i)
{
id=i;
}
public double getBalance()
{
return balance;
}
public void setBalance(double b)
{
balance=b;
}
public static double getAnnualInterestRate()
{
return annualInterestRate;
}
public static void setAnnualInterestRate(double r)
{
annualInterestRate=r;
}
public Date getDateCreated()
{
return dateCreated;
}
public static double getMonthlyInterestRate()
{
return annualInterestRate/12;
}
public void withdraw(double amount)
{
balance-=amount;
}
public void deposit(double amount)
{
balance+=amount;
}
}
public class Test
{
public static void main(String[] args)
{
Account test= new Account(1122, 20000);
Account.setAnnualInterestRate(0.045);
test.withdraw(2500);
test.deposit(3000);
System.out.println(" Balance: "+test.getBalance());
System.out.println(" Monthly interest: "+(test.getMonthlyInterestRate()*test.getBalance()));
System.out.println(" Date created: "+test.getDateCreated());
}
}
Explanation / Answer
Note: Just a Small mistake .U didnt provided the parameterized constructor name.Except that everything is fine.
________________
Account.java
import java.util.Date;
public class Account
{
//Declaring instance variables
private int id;
private double balance;
private static double annualInterestRate;
private Date dateCreated;
//Zero argumented constructor
public Account()
{
dateCreated = new Date();
id = 0;
balance = 0;
}
//Parameterized constructor
public Account(int id,double balance)
{
setID(id);
setBalance(balance);
dateCreated = new Date();
}
//getters and setters
public int getID()
{
return id;
}
public void setID(int i)
{
id = i;
}
public double getBalance()
{
return balance;
}
public void setBalance(double b)
{
balance = b;
}
public static double getAnnualInterestRate()
{
return annualInterestRate;
}
public static void setAnnualInterestRate(double r)
{
annualInterestRate = r;
}
public Date getDateCreated()
{
return dateCreated;
}
//This method will find the monthly interest rate
public static double getMonthlyInterestRate()
{
return annualInterestRate / 12;
}
//This method will perform withdraw operation
public void withdraw(double amount)
{
if(amount>balance)
System.out.println("** Your Account Balance is Low **");
else
balance -= amount;
}
//This method will perform deposit operation
public void deposit(double amount)
{
balance += amount;
}
}
___________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an Instance of Account class by passing values as arguments
Account test= new Account(1122, 20000);
//calling the methods
Account.setAnnualInterestRate(0.045);
test.withdraw(2500);
test.deposit(3000);
//Displaying the output
System.out.println(" Balance: "+test.getBalance());
System.out.println(" Monthly interest: "+(test.getMonthlyInterestRate()*test.getBalance()));
System.out.println(" Date created: "+test.getDateCreated());
}
}
__________________
Output:
Balance:
20500.0
Monthly interest:
76.875
Date created:
Thu Apr 26 06:32:28 IST 2018
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.