In: Computer Science
I am tasked with creating a Java application that used 2 synchronized threads to calculate monthly interest and update the account balance
This is the parameters for reference to the code. Create a class
AccountSavings. The class has two instance variables: a double
variable to keep annual interest rate and a double variable to keep
savings balance. The annual interest rate is 5.3 and savings
balance is $100.
• Create a method to calculate monthly interest. • Create a method
to run two threads. Use anonymous classes to create these threads.
The first thread calls the monthly interest calculation method 12
times, and then displays the savings balance (the balance in 12th
month). After that, this thread sleeps 5 seconds. The second thread
calls the monthly interest calculation method 12 times, and then
displays the savings balance (the balance in 12th month). Before
the main thread ends, these two threads must be completed. • Add
your main method into the same class and test your threads. After
these two threads are executed, the savings balance must remain
same
I am getting an error when calling monthlyInterest method inside my runThread method. non-static method monthlyInterest() cannot be referenced from a static context and I cant seem to figure out how to fix the issue.
import static java.lang.Thread.sleep;
class AccountSavings {
double annualInterest=5.3;
double savings=100.00;
public void monthlyInterest(){
double monthlyRate;
monthlyRate = annualInterest/12;
double balance = 0;
balance+=savings*monthlyRate;
}
public synchronized static void
runThread(){
Thread t1;
t1 = new Thread(){
AccountSavings accountSavings= new AccountSavings();
@Override
public void run(){
for(int i=1;i<13;i++){
System.out.println("Balance after " + i + "month: " +
monthlyInterest());
}
try{sleep(5000);}
catch(InterruptedException e){e.printStackTrace();}
}
};
Thread t2= new Thread(){
AccountSavings accountSavings=new
AccountSavings();
@Override
public void run(){
for(int
i=1;i<13;i++){
System.out.println("Balance after " + i + " month: " +
monthlyInterest(balance));
}
try{sleep(5000);}
catch(InterruptedException
e){e.printStackTrace();}
}
};
t1.start();
t2.start();
}
public static void main(String[] args){
runThread();
}
}
Object:- To correct the program, check the flow and organise answer.
Solution:-
import static java.lang.Thread.sleep;
public class AccountSavings {
static double annualInterest=5.3;
static double savings=100.00;
static double balance = 0;
public static double monthlyInterest(double bal){
double monthlyRate;
monthlyRate = annualInterest/12;
if (bal == 0)
balance+=savings*monthlyRate;
else
bal+=savings*monthlyRate;
return bal==0?balance:bal;
}
public synchronized static void runThread(){
Thread t1 = new Thread(){
AccountSavings accountSavings= new AccountSavings();
@Override
public void run(){
for(int i=1;i<13;i++){
System.out.println("Balance after " + i + " month: " +
monthlyInterest(0));
}
}
};
Thread t2= new Thread(){
AccountSavings accountSavings=new AccountSavings();
@Override
public void run(){
try{sleep(5000);}
catch(InterruptedException e){e.printStackTrace();}
for(int i=1;i<13;i++){
System.out.println("Balance after " + i + " month: " +
monthlyInterest(balance));
}
}
};
t1.start();
t2.start();
}
public static void main(String[] args){
runThread();
}
}
Note: 1:- Never call non static variable from static methods (make static variables or go for singleton structure).
2:- No need to sleep in first run just put it inside at the begining of second run.
3:- Pass balance parameter in monthlyInterest method so that we can achieve the desired single method functionality.
4:- Compile this code using "javac classname.java" and then run this code using "java classname" if you are not using any editor tool like netbeans or eclips.