2013-04-11 4 views
0

단일 차원 배열을 다차원 배열로 변환하려고합니다. 기존의 Interest Calculator Batch 애플리케이션을 여러 개의 단일 차원 배열 에서 1 개의 다차원 배열로 변환하십시오. 다음 데이터 형식을 사용하십시오 : double [] [] values; 힌트 : 최대 데이터 세트 수 (5)가 행이며 원래 InterestCalculatorBatch (7)의 각 데이터 배열이 열입니다. 2. 모든 데이터는 단일 다차원 배열 내에 포함됩니다. 3. sortBySimple 및 displayInterest 메서드를 수정하여 여러 단일 차원 배열 대신 단일 다차원 배열을 적용하도록1 차원 배열을 다차원 배열로 변환

단일 다차원 배열을 허용하도록 displayInterest를 수정했습니다. 나는 sortBySimple을 올바르게 설정하고 프로그램을 수정하거나 유지해야 할 때 프로그램에서 관심을 계산할 때 혼란 스러울뿐입니다. 도와 주셔서 감사합니다.

import java.util.Scanner; 
public class InterestCalculatorBatchMDA { 
public static void main(String[] args) 
{ 
    int cnt = 0; 
    double[][] arrPrincipalAmt = new double[5][7]; 
    double[][] arrInterestRate = new double[5][7]; 
    double[][] arrTerm = new double[5][7]; 
    double[][] arrSimple = new double[5][7]; 
    double[][] arrCompoundMonthly = new double[5][7]; 
    double[][] arrCompoundDaily = new double[5][7]; 
    double[][] arrCompoundWeekly = new double[5][7]; 


    do{ 
     arrPrincipalAmt[cnt] = getPrincipalAmount(1); 
     arrInterestRate[cnt] = getInterestRate(1); 
     arrTerm[cnt] = getTerm(1); 

     arrSimple[cnt] =   round(calculateSimpleInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt]),5); 
     arrCompoundMonthly[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt],arrTerm[cnt] ,12.0),5); 
     arrCompoundWeekly[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt], 52.0),5); 
     arrCompoundDaily[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt], 365.0),5); 

     cnt++; 
    }while (cnt < 5 && askYesNo("Enter another set of data (Yes/No):")); 

    displayInterest(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt); 
    sortBySimple(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt); 
    displayInterest(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt); 

} 


/** Round **/ 
    public static double round(double numb1, double numb2) { 
    double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));; 
    return round; 
    } 

    /** Calculate Simple **/ 
    public static double calculateSimpleInterest(double numb1, double numb2, double numb3) { 
    double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0)); 
    return calculateSimpleInterest; 
    } 

    /** Calculate Compounded Daily **/ 
    public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4) { 
    double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1; 
    return calculateCompoundInterest; 
    } 

    /** Get principal amount **/ 
    public static double getPrincipalAmount(double numb1) { 
     Scanner input = new Scanner(System.in); 
     double numb2 = 1; 
    do{System.out.print("Enter Loan Amount: "); 
     numb2 = input.nextDouble(); 
     if(numb2 > 0); 

      else{ 
        System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2); 
      }  
      }while (numb2 < 0); 
    return numb2; 
    } 

    /** Get interest rate **/ 
    public static double getInterestRate(double numb1) { 
     Scanner input = new Scanner(System.in); 
     double numb2=1; 
     do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); 
     numb2 = input.nextDouble(); 
     double getInterestRate = 0; 
    if (numb2 >= 0 && numb2 <= 100) 
     getInterestRate = numb2; 
      else{ 
        System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2); 
      }  
      }while (numb2 <= 0 || numb2 >= 100); 
    return numb2; 
    } 

    /** Get term **/ 
    public static double getTerm(double numb1) { 
     Scanner input = new Scanner(System.in); 
     double numb2=1; 
     do{System.out.print("Enter the Term (in months): "); 
     numb2 = input.nextInt(); 
     double getTerm = 0; 
     if (numb2 > 0) 
     getTerm = numb2; 
      else{ 
        System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2); 
      }  
      }while (numb2 <= 0); 
    return numb2; 
    } 

    /** Sort by simple interest **/ 
    public static void sortBySimple(double[][] arrPrincipalAmt ,double[][] arrInterestRate, double[][] arrTerm, double[][] arrSimple, double[][] arrCompoundMonthly, double[][] arrCompoundWeekly, double[][] arrCompoundDaily, double count){ 
     for(int i = 0;i<count;i++) 
     { 
     for(int j=i+1; j<count;j++) 
     { 
     if(arrSimple[i][j] < arrSimple[i][j]) 
     { 
     double temp = arrSimple[i][j]; 
     arrSimple[i][j] = arrSimple[i][j]; 
     arrSimple[i][j] = temp; 

     double temp1 = arrPrincipalAmt[i][j]; 
     arrPrincipalAmt[i][j] = arrPrincipalAmt[i][j]; 
     arrPrincipalAmt[i][j] = temp1; 

     double temp2 = arrInterestRate[i][j]; 
     arrInterestRate[i][j] = arrInterestRate[i][j]; 
     arrInterestRate[i][j] = temp2; 

     double temp3 = arrTerm[i][j]; 
     arrTerm[i][j] = arrTerm[i][j]; 
     arrTerm[i][j] = temp3; 

     double temp4 = arrSimple[i][j]; 
     arrSimple[i][j] = arrSimple[i][j]; 
     arrSimple[i][j] = temp4; 

     double temp5 = arrCompoundMonthly[i][j]; 
     arrCompoundMonthly[i][j] = arrCompoundMonthly[i][j]; 
     arrCompoundMonthly[i][j] = temp5; 

     double temp6 = arrCompoundDaily[i][j]; 
     arrCompoundDaily[i][j] = arrCompoundDaily[i][j]; 
     arrCompoundDaily[i][j] = temp6; 

     double temp7 = arrCompoundWeekly[i][j]; 
     arrCompoundWeekly[i][j] = arrCompoundWeekly[i][j]; 
     arrCompoundWeekly[i][j] = temp7; 
     } 
     } 
     } 
    } 

    /** Display Interest **/ 
    public static void displayInterest(double[][] amt ,double[][] interest, double[][] term, double[][] simple, double[][] monthly, double[][] weekly, double[][] arrCompoundDaily, int count){ 
    int i=0; 
    System.out.println("[Line #] [Principal Amount] [Interest Rate] [Term] [Simple Interest] [Compound Monthly] [Compound Weekly] [Compound Daily]"); 
    do{ 
    System.out.print((i+1)+"    "); 
    System.out.print(amt[i][i]+"    "); 
    System.out.print(+interest[i][i]+"   "); 
    System.out.print(+ term[i][i]+"   "); 
    System.out.print(+simple[i][i]+"   "); 
    System.out.print(+monthly[i][i]+"   "); 
    System.out.print(+weekly[i][i]+"   "); 
    System.out.println(+arrCompoundDaily[i][i]); 
    i++; 
    }while(i < count); 
    } 

    /**ask yes or no **/ 
    public static boolean askYesNo(String question) { 
     Scanner input = new Scanner(System.in); 
     String enteredText; 
     boolean isAnswerValid; 

     do{ 
      isAnswerValid = false; 
      System.out.println(question); 
      enteredText = input.nextLine(); 

      if (enteredText.length() > 0) 
      { 
       enteredText = enteredText.toUpperCase(); 

       if(enteredText.equals("YES") || enteredText.equals("Y") || enteredText.equals("NO") || enteredText.equals("N")) 
       { 
        isAnswerValid = true; 
       } 
      } 

      if(isAnswerValid == false) 
      { 
       System.out.println("Please enter 'Yes' or 'No'?"); 
      } 

     } while(isAnswerValid == false); 

     if(enteredText.equals("YES") || enteredText.equals("Y")) 
     { 
      return true; 
     } 

     return false; 
    } 

} 
+0

이것은 프로그래밍 문제처럼 들리지는 않습니다. 질문을 완전히 이해하지 못하고 교수에게 이메일을 보내야하는 것 같습니다. – RyPope

답변

0

코드는 Java로 C를 작성하려는 것 같습니다. Java는 OO 언어이며, 에 프로그램을 작성하고 작성하는 경우을 사용하면 더 많은 성공을 거둘 수 있습니다. 이와 같이 여러 어레이에서 "번지는"데이터 구조는 근본적으로 다루기가 어렵습니다.

이 문제를 해결하려면 먼저 배열의 항목을 나타내는 클래스를 만들어야합니다.

"얼룩이 묻은"데이터 구조 디자인을 유지하면서 프로그램을 수정하려고하면 ... 시간이 낭비됩니다.

관련 문제