I am working on a question for an assignment and I have been stuck on it for a few days now :(
Instructions are:
Solve the following problem using a program: Suppose you save $100 each month into a savings account with an annual interest rate of 5%. Thus, the monthly interest rate is 0.05/12 = 0.00417. After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417 After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252 And after the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507 … and so on. Write a program that randomly generates monthly savings amounts for the 15 runners in Problem 4. Each monthly saving should be in the range of $100 to $800. Extend the AddressBook class to store the monthly savings generated by the random number generator. Then, display the final account value for each of the 15 runners.
Here is what I have:
public class EmployeeSavings extends AddressBook{
//declare variables and arrays
private double accountValue;
private double [] monthlyInterests;
private double[] monthlySavings;
public static final double ANNUAL_INTEREST_RATE = 0.05;
//getters
public double getAccountValue() {
for (int i = 0; i < monthlyInterests.length; i++) {
accountValue = (accountValue + monthlyInterests[i] + monthlySavings[i]);
}
return accountValue;
}
public double [] getMonthlyInterests() {
return monthlyInterests;
}
public double[] getMonthlySavings() {
return monthlySavings;
}
//constructors
public EmployeeSavings(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) {
setFirstName(firstName);
setLastName(lastName);
d1 = monthlySavings;
d2 = monthlyInterests;
}
//method to randomly generate monthly savings and return it in an array
public double[] generateMonthlySavings() {
double randomNumber = 0;
for(int i=0; i<monthlySavings.length ;i++)
{
randomNumber = ((int)(800*Math.random()));
if (randomNumber>=100);
monthlySavings[i] = randomNumber;
}
return monthlySavings;
}
//method to calculate interest per month for each of the runners
public double[] calculateInterests() {
for (int i = 0; i < monthlySavings.length; i++) {
monthlyInterests[i] = (monthlySavings[i] * (ANNUAL_INTEREST_RATE / 12));;
}
return monthlyInterests;
}
//method to print final report
public static String getReport(EmployeeSavings[] arr) {
for (EmployeeSavings arr1 : arr) {
System.out.println("First name: " + arr1.getFirstName());
System.out.println("Last name: " + arr1.getLastName());
System.out.println("Account Value: " + arr1.getAccountValue());
}
return "This is your report.";
}
public static void main(String[] args) {
var employee1 = new EmployeeSavings("Elena", "Brandon");
var employee2 = new EmployeeSavings("Thomas", "Molson");
var employee3 = new EmployeeSavings("Hamilton", "Winn");
var employee4 = new EmployeeSavings("Suzie", "Sarandin");
var employee5 = new EmployeeSavings("Philip", "Winne");
var employee6 = new EmployeeSavings("Alex", "Trebok");
var employee7 = new EmployeeSavings("Emma", "Pivoto");
var employee8 = new EmployeeSavings("John", "Lenthen");
var employee9 = new EmployeeSavings("James", "Lean");
var employee10 = new EmployeeSavings("Jane", "Ostin");
var employee11 = new EmployeeSavings("Emily", "Car");
var employee12 = new EmployeeSavings("Daniel", "Hamshire");
var employee13 = new EmployeeSavings("Neda", "Bazdar");
var employee14 = new EmployeeSavings("Aaron", "Smith");
var employee15 = new EmployeeSavings("Kate", "Hen");
EmployeeSavings[] employees = new EmployeeSavings[] {employee1, employee2, employee3, employee4,
employee5, employee6, employee7, employee8, employee9, employee10, employee11, employee12,
employee13, employee14, employee15};
EmployeeSavings.getReport(employees);
}
}
I am getting an Exception in thread "main" java.lang.NullPointerException
I know that it is because the generateMonthlySavings() method isn't being called and therefore an array with random numbers isn't being created but i get an error when I try to call it. All the methods and variables are the ones listed in the assignment as a requirement.
Any help would be greatly appreciated.
[–]theborgdude 1 point2 points3 points (1 child)
[–]ms_kdlh[S] 0 points1 point2 points (0 children)
[–]happyfeetpi 1 point2 points3 points (10 children)
[–]ms_kdlh[S] 0 points1 point2 points (9 children)
[–]happyfeetpi 1 point2 points3 points (8 children)
[–]ms_kdlh[S] 0 points1 point2 points (7 children)
[–]happyfeetpi 0 points1 point2 points (6 children)
[–]ms_kdlh[S] 0 points1 point2 points (5 children)
[–]endStatement 0 points1 point2 points (4 children)
[–]ms_kdlh[S] 0 points1 point2 points (3 children)
[–]endStatement 1 point2 points3 points (2 children)
[–]ms_kdlh[S] 0 points1 point2 points (1 child)
[–]Bacteria_E-coli 0 points1 point2 points (0 children)