2014-04-01 91 views
0

我想为我的代码创建三个不同的类:FutureValueApp,ValidatorFinancialCalculations。但是,当我运行代码时,我收到以下错误信息,并且不知道如何解决此问题。java不支持的操作异常

============================================================================== 
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 

================================================================================== 

这是到目前为止我的代码:

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类

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类

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; 
    } 
} 

我知道 “私人UnsupportedOperationException异常” 不应该是包括为了使代码正常工作,我需要公开,但这是如此混乱。

回答

2

你错过了实现方法

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. 
} 

的只是实现它,你会被罚款

0

如前所述,你还没有实现三种方法,它们会自动抛出异常。

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; 
} 

为了测试你的代码,你可以做什么,我在上面所做的,只是设置一个返回值,你的控制台应该打印出来:

欢迎来到未来的价值计算器

DATA ENTRY

格式的结果每月投资:NOK 0.00年利息率 :年0.0%数:0终值:NOK 0.00

继续吗? (Y/N):

+0

没有冒犯,但他已经实施了。两次。正如JonK正确指出的那样,问题在于使用了错误版本的方法,它们抛出异常。 –

+0

没有采取,但这些是他所调用的方法,我只是指那个。他没有两次执行相同的方法,但是有两个类具有共享相同名称的方法,这与不同的方法是不一样的。 :-) –

1

你有方法称为getDoubleWithinRange(同样是getIntWithinRangecalculateFutureValue也是如此)。你的问题是,你是调用错误的

这条线:

double monthlyInvestment = getDoubleWithinRange(sc, 
       "Enter monthly investment: ", 0, 1000); 

是调用FutureValueApp类,它只能抛出UnsupportedOperationExceptionstaticgetDoubleWithinRange方法。这是因为你没有通过拥有类来限定方法调用。

要调用的方法是一个在Validator类,你需要这样的呼吁:

double monthlyInvestment = Validator.getDoubleWithinRange(sc, 
       "Enter monthly investment: ", 0, 1000); 

同样,该行:

int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); 

需要是:

int years = Validator.getIntWithinRange(sc, "Enter number of years: ", 0, 100); 

And:

double futureValue = calculateFutureValue(
       monthlyInvestment, monthlyInterestRate, months); 

需求,成为:

double futureValue = FinancialCalculations.calculateFutureValue(
       monthlyInvestment, monthlyInterestRate, months); 

每当你处理static方法,这是很好的做法,总是资格与所属的类调用 - 假若你在这种情况下这样做,你将有能够立即发现问题。

FutureValueApp删除冗余方法也是一个好主意。

相关问题