0

I want to write a java code which based upon the Matrix.I am using jama matrix.

The code is like that:

import Jama.Matrix;
public class Landmarkassist {
public static void main(String args[]){
    double [] time1st
 {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};// Array of some 1st time slot.
    double[] time2nd={2.0,3.0,6.0,7.0,8.0};//Array of 2nd time slot
    double[] code={40.0,20.0,10.0,40.0,10.0};// Value of Marks
    Matrix Calculation=new Matrix(8,8);// A 8*8 Jama Matrix
    int k1=0,k2=0,l2=0,l1=0;
    while(k1<time1st.length && k2<time2nd.length){

        if(time1st[k1]<time2nd[k2]) {
       //Just to increment counter k1++
            k1++;
        }
   //If time slot 1 is equal to time slot 2 the we check for the codes if the codes matches with our criteria we update the cell of Jama matrix
        else if(time1st[k1]==time2nd[k2]){
            if(code[l1]==40.0){
                Calculation.set(l2,k1,40.0);//set the value to a specific cell of Jama Matrix l2 reperesent row number and k1 represent column no, and 40.0 is the value of that Cell
                l2++;
            }
            else if(code[l1]==20.0){
                Calculation.set(l2,k1,20.0);
                l2++;
            }
            else if(code[l1]==10.0){
                Calculation.set(l2,k1,10.0);
                l2++;
            }
            else{
                System.out.println("Nothing to do");
            }
            k2++;
            l1++;


        }



    }
    Calculation.print(9,1);
}

}

Output:

0.0   40.0    0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     20.0   0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  10.0 0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  40.0       0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0       10.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0

Desired output:

0.0   40.0    40.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     20.0   0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  10.0  10.0       0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0

That means all the same number of Marks should allocated at the same row. All the marks 40.0 is allocated at 1st row. All the marks 10.0 is allocated at 3rd row etc.

How could I iterate the loops to achieve this?

Thank you in advance

Encipher
  • 1,370
  • 1
  • 14
  • 31
  • 1
    Please follow [Java naming convention](https://www.geeksforgeeks.org/java-naming-conventions/) : lowerCamelCase for variables, attributes, package, methods – azro Jul 25 '18 at 09:14
  • Your code explicitly sets the elements of the matrix. So in your `if(code[l1]==40.0){...}` clause do not set the value to row `l2` but to row `0`. – Thomas Behr Jul 25 '18 at 09:18
  • @ThomasBehr I didnot get the answer of my question. – Encipher Jul 25 '18 at 11:29
  • @EllenaMori What do you think does the line `Calculation.set(l2,k1,40.0);` do? – Thomas Behr Jul 25 '18 at 11:39
  • This set the value at Calculation matrix in a specific row and column. What I need to implement whenever it get 40.0 again it take the same row number as it take previous when it 1st encounter 40.0 but the Column number will be change according to k1. – Encipher Jul 25 '18 at 12:21

0 Answers0