How to use the constructor? java -
package aprilchap3; /** * * @author ericaross */ public class savingsaccount { double interest; double balance; public savingsaccount() { balance = 0; interest = 0.1; } public void addinterest() { balance = interest*balance + balance; } public void deposit(double amount) { balance= balance + amount; } public void getbalance(){ return balance; } }
``and savingsaccounttester class:
package aprilchap3; /** * * @author ericaross */ public class savingaccounttester { public static void main(string[] args) { savingsaccount erica = new savingsaccount(); erica.deposit(1000); erica.addinterest(); double ericabalance = erica.getbalance() ; system.out.println(ericabalance + "this how owe. ");` } }
so problem want use constructor set value instead of depositing, when try error shows in savings account declaration.
here want put savingsaccount erica = new savingsaccount();
error shows every time can 1 show me best way set constructor work?
public savingsaccount() { balance = 0; interest = 0.1; }
you don't need this. have made initial values of variables. need is
public savingsaccount(double balance, double interest) { this.balance = balance; this.interest = interest; }
and use via new savingsacccount(balance, interest)
.
nb hope , trust homework. should never ever ever use floating-point datatypes money.
Comments
Post a Comment