3.1 Write a Java program that will compute the future investment value at a given interest rate for a specified number of years. The java program should have a minimum of two classes.
3.1.1 classA
The main method (starting point) (5)
The object of classb (5)
Should also call the void method named futureInvestmentValue (double investment Amount, double monthlyInterestRate, int years) (15)
3.1.2 classB
A get method named count_words(String str) (15)
Expected Output:
Input the investment amount: 28000
Input the rate of interest: 7
Input number of years: 6
Years Future Value
1. 30024.12
2. 32194.57
3. 34521.92
4. 37017.51
5. 39693.51
6. 42562.95
import java.util.Scanner;
public class classb {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter words in one line separated by a space: ");
count_words(in.nextLine());
}
static void count_words(String str) {
int count = 0;
if (str.length() != 0) {
count++;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
count++;
}
}
}
System.out.println("There are " + count + " words in a sentence.");
}
}
import java.util.Scanner;
public class classB {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Input the investment amount: ");
double investment = keyBoard.nextDouble();
System.out.print("Input the rate of interest: ");
double rate = keyBoard.nextDouble();
System.out.print("Input number of years: ");
int year = keyBoard.nextInt();
rate *= 0.01;
System.out.printf("%-10s%-15s\n", "Years", "FutureValue");
for (int i = 1; i <= year; i++) {
System.out.printf("%-10d%-15.2f\n", i, futureInvestmentValue(investment, rate / 12, i));
}
}
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) {
return investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
}
}
Comments
Leave a comment