// Kardy Fong package futurevalueapp; import java.util.*; import java.text.*; pu
ID: 3841422 • Letter: #
Question
// Kardy Fong
package futurevalueapp;
import java.util.*;
import java.text.*;
public class FutureValueApp {
public static void main(String[] args) {
ArrayList<String> lis = new ArrayList<String>();
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.println("DATA ENTRY");
double monthlyInvestment = getDoubleWithinRange(sc,
"Enter monthly investment: ", 0, 1000);
double interestRate = getDoubleWithinRange(sc,
"Enter yearly interest rate: ", 0, 30);
int years = getIntWithinRange(sc,
"Enter number of years: ", 0, 100);
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
String results =
"Monthly investment: "
+ currency.format(monthlyInvestment) + " "
+ "Yearly interest rate: "
+ percent.format(interestRate/100) + " "
+ "Number of years: "
+ years + " "
+ "Future value: "
+ currency.format(futureValue) + " ";
System.out.println();
System.out.println("FORMATTED RESULTS STORED");
lis.add(results);
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
Iterator<String> it = lis.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static double getDouble(Scanner sc, String prompt)
{
boolean isValid = false;
double d = 0;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine();
}
return d;
}
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}
public static int getInt(Scanner sc, String prompt)
{
boolean isValidInt = false;
int i = 0;
while (isValidInt == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValidInt = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine();
}
return i;
}
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months)
{
double futureValue = 0;
for (int i = 1; i <= months; i++)
{
futureValue =
(futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
}
// Help me with this question
Java Programing. [Murach 4th ed. ch 16_ex1 (furture value)]
For this exercise, you'll modify the future Value application presented in the chapter 15 so that it uses a combo box and a list.
1. Open the project named ch 16_ex1_FutureValue in the ex_starts directory. Then, review the design and coude for the form.
2. Combo box. Code a method that fills this combo box with the value 1 through 20, and call this method from the constructor for the frame. Make any other necessary changes to provide for the combo box. Test the project to be sure it works correctly.
3. Replace the Future Value text field with a list that displays five rows and uses a vertical scroll bar but no horizontal scroll bar.
4. Modify the actionPerformed event for the Calculate button so that instead of calculation a single value, it calculates the future value for each year up to the year selected via the combo box and adds a string showing the calculation for each year to the list. 5. Test the project to be sure it works correctly.
// Thank you.
Explanation / Answer
package com.chegg.vending.test;
import java.util.*;
import java.text.*;
public class FutureValueApp {
public static void main(String[] args) {
ArrayList<String> lis = new ArrayList<String>();
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
System.out.println("DATA ENTRY");
double monthlyInvestment = getDoubleWithinRange(sc,
"Enter monthly investment: ", 0, 1000);
double interestRate = getDoubleWithinRange(sc,
"Enter yearly interest rate: ", 0, 30);
int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
double monthlyInterestRate = interestRate / 12 / 100;
int months = years * 12;
double futureValue = calculateFutureValue(monthlyInvestment,
monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String results = "Monthly investment: "
+ currency.format(monthlyInvestment) + " "
+ "Yearly interest rate: "
+ percent.format(interestRate / 100) + " "
+ "Number of years: " + years + " " + "Future value: "
+ currency.format(futureValue) + " ";
System.out.println();
System.out.println("FORMATTED RESULTS STORED");
lis.add(results);
System.out.print("Continue? (y/n): ");
System.out.println();
}
Iterator<String> it = lis.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public static double getDouble(Scanner sc, String prompt) {
boolean isValid = false;
double d = 0;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine();
}
return d;
}
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
d = getDouble(sc, prompt);
if (d <= min)
System.out.println("Error! Number must be greater than " + min
+ ".");
else if (d >= max)
System.out.println("Error! Number must be less than " + max
+ ".");
else
isValid = true;
}
return d;
}
public static int getInt(Scanner sc, String prompt) {
boolean isValidInt = false;
int i = 0;
while (isValidInt == false) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValidInt = true;
} else {
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine();
}
return i;
}
public static int getIntWithinRange(Scanner sc, String prompt, int min,
int max) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
i = getInt(sc, prompt);
if (i <= min)
System.out.println("Error! Number must be greater than " + min
+ ".");
else if (i >= max)
System.out.println("Error! Number must be less than " + max
+ ".");
else
isValid = true;
}
return i;
}
public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months) {
double futureValue = 0;
for (int i = 1; i <= months; i++) {
futureValue = (futureValue + monthlyInvestment)
* (1 + monthlyInterestRate);
}
return futureValue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.