Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Single dimension array to multidimension array

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Single dimension array to multidimension array

    I'm trying to convert my single dimensional array into a multidimensional array [5][7]. I know I have to convert my methods sortBySimpleInterest and displayInterest to accept a single multidimensional array instead of multiple single dimensional arrays. With making those two methods into a multidimensional array will I have to change the calculation to accept multidimensional arrays as well? I am also confused on how I should set up the SortbySimpleInterest method using the selection sort.

     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[j]<arrSimple[i])
          {
          double temp = arrSimple[i];
          arrSimple[i] = arrSimple[j];
          arrSimple[j] = temp;
     
          double temp1 = arrPrincipalAmt[i];
          arrPrincipalAmt[i] = arrPrincipalAmt[j];
          arrPrincipalAmt[j] = temp1; 
     
          double temp2 = arrInterestRate[i];
          arrInterestRate[i] = arrInterestRate[j];
          arrInterestRate[j] = temp2; 
     
          double temp3 = arrTerm[i];
          arrTerm[i] = arrTerm[j];
          arrTerm[j] = temp3; 
     
          double temp4 = arrSimple[i];
          arrSimple[i] = arrSimple[j];
          arrSimple[j] = temp4;
     
          double temp5 = arrCompoundMonthly[i];
          arrCompoundMonthly[i] = arrCompoundMonthly[j];
          arrCompoundMonthly[j] = temp5; 
     
          double temp6 = arrCompoundDaily[i];
          arrCompoundDaily[i] = arrCompoundDaily[j];
          arrCompoundDaily[j] = temp6; 
     
          double temp7 = arrCompoundDaily[i];
          arrCompoundDaily[i] = arrCompoundDaily[j];
          arrCompoundDaily[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]+"                ");
        System.out.print(+interest[i]+"           ");
        System.out.print(+ term[i]+"           ");
        System.out.print(+simple[i]+"          ");
        System.out.print(+monthly[i]+"           ");
        System.out.print(+weekly[i]+"           ");
        System.out.println(+arrCompoundDaily[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;
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,340
    Thanks
    49
    Thanked 2,195 Times in 2,168 Posts

    Default Re: Single dimension array to multidimension array

    how I should set up the SortbySimpleInterest method using the selection sort.
    How do you want to order the contents of the 2 dim array? By row depending on the contents of a specific column?

    Can you give an example of a small unsorted array and what it would be after sorting.


    NOTE: The posted code is full of compiler errors you need to fix.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single dimension array to multidimension array

    It would be sorted by the row.

    [Line #] [Principal Amount] [Interest Rate] [Term] [Simple Interest][Compound Monthly] [Compound Weekly][Compound Daily]
    1 $1375.0 1.175% 7 $9.42448 $9.45221 $9.45578 $9.4567
    2 $100.0 3.25% 6 $1.625 $1.63604 $1.63776 $1.6382
    3 $100.0 3.25% 12 $3.25 $3.29885 $3.30234 $3.30324
    [Line #] [Principal Amount] [Interest Rate] [Term] [Simple Interest][Compound Monthly] [Compound Weekly][Compound Daily]
    1 $100.0 3.25% 6 $1.625 $1.63604 $1.63776 $1.6382
    2 $100.0 3.25% 12 $3.25 $3.29885 $3.30234 $3.30324
    3 $1375.0 1.175% 7 $9.42448 $9.45221 $9.45578 $9.4567

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,340
    Thanks
    49
    Thanked 2,195 Times in 2,168 Posts

    Default Re: Single dimension array to multidimension array

    It would be sorted by the row based on the contents of the first column?

    Where is the sortBySimpleInterest() method? I don't find it in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single dimension array to multidimension array

    It's the third last method
    /** 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[j]<arrSimple[i])
          {
          double temp = arrSimple[i];
          arrSimple[i] = arrSimple[j];
          arrSimple[j] = temp;
     
          double temp1 = arrPrincipalAmt[i];
          arrPrincipalAmt[i] = arrPrincipalAmt[j];
          arrPrincipalAmt[j] = temp1; 
     
          double temp2 = arrInterestRate[i];
          arrInterestRate[i] = arrInterestRate[j];
          arrInterestRate[j] = temp2; 
     
          double temp3 = arrTerm[i];
          arrTerm[i] = arrTerm[j];
          arrTerm[j] = temp3; 
     
          double temp4 = arrSimple[i];
          arrSimple[i] = arrSimple[j];
          arrSimple[j] = temp4;
     
          double temp5 = arrCompoundMonthly[i];
          arrCompoundMonthly[i] = arrCompoundMonthly[j];
          arrCompoundMonthly[j] = temp5; 
     
          double temp6 = arrCompoundDaily[i];
          arrCompoundDaily[i] = arrCompoundDaily[j];
          arrCompoundDaily[j] = temp6; 
     
          double temp7 = arrCompoundDaily[i];
          arrCompoundDaily[i] = arrCompoundDaily[j];
          arrCompoundDaily[j] = temp7; 
          }
          }
          } 
      }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,340
    Thanks
    49
    Thanked 2,195 Times in 2,168 Posts

    Default Re: Single dimension array to multidimension array

    So the the sortBySimpleInterest() method is named the sortBySimple()?

    The formatting on that posted code is poor. Statements nested inside of {} should be indented 3-4 spaces. Poor formatting makes the code hard to read.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single dimension array to multidimension array

    yes, sorry!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,340
    Thanks
    49
    Thanked 2,195 Times in 2,168 Posts

    Default Re: Single dimension array to multidimension array

    Array manipulation:
          int[][] twoDim = {{1,2}, {3,4}};  //  define and give some values
          int[] oneDim = twoDim[0];            // copy first row to save
          twoDim[0] = twoDim[1];               //  copy second row to first row
          twoDim[1] = oneDim;                   // change second row to old first row
          System.out.println(Arrays.deepToString(twoDim)); // [[3, 4], [1, 2]]
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 02:22 PM
  2. error: array dimension missing x 2. Any help?
    By giga in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 30th, 2012, 09:50 AM
  3. tic tac toe single array code help
    By angelus2402004 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 09:17 PM
  4. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  5. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 12:17 AM