Reputation: 35
I am trying to create three different classes for my code: FutureValueApp
, Validator
, and FinancialCalculations
. I am following instructions, however, when I run the code, I keep getting the following error and and have no idea how to fix it.
==============================================================================
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at FutureValueApp.getDoubleWithinRange(FutureValueApp.java:64)
at FutureValueApp.main(FutureValueApp.java:17)
Java Result: 1
==================================================================================
Here is my code so far:
import java.util.*;
import java.text.*;
import java.util.Scanner;
public class FutureValueApp
{
public static void main(String[] args)
{
System.out.println("Welcome to the Future Value Calculator\n");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
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);
// calculate the future value
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
// get the currency and percent formatters
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
// format the result as a single string
String results =
"Monthly investment:\t"
+ currency.format(monthlyInvestment) + "\n"
+ "Yearly interest rate:\t"
+ percent.format(interestRate/100) + "\n"
+ "Number of years:\t"
+ years + "\n"
+ "Future value:\t\t"
+ currency.format(futureValue) + "\n";
// print the results
System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
sc.nextLine(); // discard any other data entered on the line
System.out.println();
}
}
private static int getIntWithinRange(Scanner sc, String enter_number_of_years_, int i, int i0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static double getDoubleWithinRange(Scanner sc, String enter_monthly_investment_, int i, int i0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static double calculateFutureValue(double monthlyInvestment, double monthlyInterestRate, int months) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Validator class
import java.util.Scanner;
public class Validator
{
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
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(); // discard any other data entered on the line
}
return d;
}
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0.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)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
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;
}
}
FinancialCalculations class
public class FinancialCalculations
{
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;
}
}
I know that the "Private UnsupportedOperationException" shouldn't be included in order to make the code work properly and that I need to make it public, but this is so confusing.
Upvotes: 0
Views: 6251
Reputation: 2108
You have two methods called getDoubleWithinRange
(same is true of getIntWithinRange
and calculateFutureValue
as well). Your issue is that you're calling the wrong ones.
This line:
double monthlyInvestment = getDoubleWithinRange(sc,
"Enter monthly investment: ", 0, 1000);
Is calling the static
getDoubleWithinRange
method of the FutureValueApp
class, which can only throw an UnsupportedOperationException
. It's doing this because you haven't qualified the method call with the owning class.
The method you want to call is the one in the Validator
class, which you need to call like this:
double monthlyInvestment = Validator.getDoubleWithinRange(sc,
"Enter monthly investment: ", 0, 1000);
Similarly, the line:
int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
Needs to be:
int years = Validator.getIntWithinRange(sc, "Enter number of years: ", 0, 100);
And:
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
Needs to become:
double futureValue = FinancialCalculations.calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
Whenever you're dealing with static
methods, it's good practice to always qualify the call with the owning class - had you done so in this instance you would have been able to spot the problem immediately.
It's also a good idea to delete the redundant methods from FutureValueApp
.
Upvotes: 1
Reputation: 2238
As mentioned, you have not implemented three methods, and they are throwing exceptions automatically.
private static int getIntWithinRange(Scanner sc, String enter_number_of_years_, int i, int i0) {
return 0;
}
private static double getDoubleWithinRange(Scanner sc, String enter_monthly_investment_, int i, int i0) {
return 0;
}
private static double calculateFutureValue(double monthlyInvestment, double monthlyInterestRate, int months) {
return 0;
}
In order to test your code, you can do what I have done above, just set a return value, and your console should print out:
Welcome to the Future Value Calculator
DATA ENTRY
FORMATTED RESULTS Monthly investment: NOK 0.00 Yearly interest rate: 0.0% Number of years: 0 Future value: NOK 0.00
Continue? (y/n):
Upvotes: 0
Reputation: 9162
You're missing implementation of the method
private static double getDoubleWithinRange(Scanner sc, String enter_monthly_investment_, int i, int i0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Just implement it and you'll be fine
Upvotes: 2