Programming (1 Viewer)

Loppan

Senior Member
Jul 13, 2002
2,528
#1
Can someone who's good with inteface and datatypes help me?

I have problems with one method:

My interface:

public interface Add {

public int plus(Add r2);



}


The method:

public Add plus(Add r2) {

t = (this.getT() * r2.getN()) + ((this.getN()) * r2.getT());
n = (this.getN() * r2.getN());

Add tal = new Addimp(t,n);


return tal;

}

the problem is that it requires the datatype Add but it found an int.
 

Buy on AliExpress.com
OP
Loppan

Loppan

Senior Member
Jul 13, 2002
2,528
  • Thread Starter
  • Thread Starter #2
    I solved the problem but I have another one:

    I do I add two matrix in java? And to see if they match?
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #3
    Addition of matrices seems rather straightforward... assuming matrices have array representation..

    Code:
    for (int length = 0; length < matrix1.length; length++) {
      for (int depth= 0; depth< matrix1[0].length; depth++) {
        resultmatrix[length][depth] = 
          matrix1[length][depth] + matrix2[length][depth];
      }
    }
    ...obviously assuming the matrices have matching dimensions, otherwise you could handle the problem with an exception (ArrayIndexOutOfBoundsException).

    I'm not sure I understand what you mean by whether they match however.. :undecide:

    I'm glad you didn't ask about multiplication of matrices, that would be worse :D
     
    OP
    Loppan

    Loppan

    Senior Member
    Jul 13, 2002
    2,528
  • Thread Starter
  • Thread Starter #4
    Thanks :D

    Now I just gotta figure out how I can create a copy of them and getValue at a certian position?
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #5
    Hell, I wanted to excercise the mind, here's a class to add, subtract and multiply matrices :D

    Code:
    class matrix {
    
    	public static void main(String[] args) {
    		int[][] matrix1 = {{2,1},{1,3},{2,3},{4,2}};
    		int[][] matrix2 = {{1,3,2,1}};
    		int[][] matrix3 = {{-2,1},{-3,3},{2,-3},{14,2}};		
    		
    		addMatrices(matrix1, matrix3);
    		subtractMatrices(matrix3, matrix1);
    		multiplyMatrices(matrix1, matrix2);
    	}
    	
    	public static void addMatrices(int[][] matrix1, int[][] matrix2) {
    		
    		int[][] resultmatrix = new int[matrix1.length][matrix1[0].length];
    		
    		try {
    		
    			for (int y = 0; y < matrix1[0].length; y++) {
    	  		for (int x = 0; x < matrix1.length; x++) {
      	  		resultmatrix[x][y] = 
        	  		matrix1[x][y] + matrix2[x][y];
    	  		}
    			}
    			
    			printComment("A", "B", "+", matrix1, matrix2, resultmatrix);
    			
    		} catch (ArrayIndexOutOfBoundsException e) {
      		System.out.println("Error: Matrix dimensions don't match");
      	}
    	}
    	
    	public static void subtractMatrices(int[][] matrix1, int[][] matrix2) {
    		
    		int[][] resultmatrix = new int[matrix1.length][matrix1[0].length];
    		
    		try {
    		
    			for (int y = 0; y < matrix1[0].length; y++) {
    	  		for (int x = 0; x < matrix1.length; x++) {
      	  		resultmatrix[x][y] = 
        	  		matrix1[x][y] - matrix2[x][y];
    	  		}
    			}
    			
    			printComment("A", "B", "-", matrix1, matrix2, resultmatrix);
    			
    		} catch (ArrayIndexOutOfBoundsException e) {
    			System.out.println("Error: Matrix dimensions don't match");
    		}
    	}	
    	
    	public static void multiplyMatrices(int[][] matrix1, int[][] matrix2) {
    		
    		int[][] resultmatrix = new int[matrix2.length][matrix1[0].length];
    
    		try {
    			for (int x = 0; x < resultmatrix.length; x++) {
    				for (int y = 0; y < resultmatrix[0].length; y++) {
    	  			for (int leftmatrix = 0; leftmatrix < matrix2[0].length; leftmatrix++) {
    	  				resultmatrix[x][y] += matrix1[leftmatrix][y] * matrix2[x][leftmatrix];
    	  			}
    	  		}
    			}		
    
    		} catch (ArrayIndexOutOfBoundsException e) {
    			System.out.println("Error: Matrix dimensions don't match");
    		}		
    		printComment("A", "B", "*", matrix1, matrix2, resultmatrix);
    
    	}
    	
    	public static void printMatrix(int[][] matrix) {
    		for (int y = 0; y < matrix[0].length; y++) {
      		for (int x = 0; x < matrix.length; x++) {
        		System.out.print("[" + matrix[x][y] + "]");
      		}
      		System.out.println();
    		}
    		
    			
    	}
    	
    	public static void printComment(String namefirst, String namesecond, String operation, 
    			int[][] first, int[][] second, int[][] result) {
    		print(namefirst + "[" + first[0].length + "x" + first.length + "] " + ":");
    		printMatrix(first);
    		print("\n" + namesecond + "[" + second[0].length + "x" + second.length + "] " + ":");
    		printMatrix(second);
    		print("\n" + namefirst + "[" + first[0].length + "x" + first.length + "]" + " " + operation + " "
    			+ namesecond + "[" + second[0].length + "x" + second.length + "]" 
    			+ " = C" + "[" + result[0].length + "x" + result.length + "] ");
    		printMatrix(result);
    		print("\n\n");
    	}
    	
    	public static void print(String output) {
    		System.out.println(output);	
    	}
    	
    }
    Mayood, eat my dust :D
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #6
    ++ [ originally posted by Loppan ] ++
    Thanks :D

    Now I just gotta figure out how I can create a copy of them and getValue at a certian position?
    Copy the matrix? In other words copy one array to another?

    Code:
    System.arraycopy(array1, 0, array2, 0, array1.length);
    
    public int getValue(int[][] matrix, int x, int y) {
      return int[y][x];
    }
    :)
     
    OP
    Loppan

    Loppan

    Senior Member
    Jul 13, 2002
    2,528
  • Thread Starter
  • Thread Starter #7
    The problem is that I'm working with a interface. I have problems with these methods:

    public Matrix getValueAt(int x, int y) {
    return (x+ +y);
    }


    public Matrix Addera(int m1, int m2) {

    Matriximp resultmatrix =
    new Matriximp (matrix.length,matrix[0].length);

    for (int y = 0; y < matrix[0].length; y++) {

    for (int x = 0; x < matrix[0].length; x++) {

    resultmatrix.matrix[x][y] =
    m1.getValAt() + m2.getValAt();
    }
    }



    return resultmatrix;

    }
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #8
    Is that the code as in your program cause it looks pretty strange to me.. why use getValue() in the first place when you can just pick out the numbers from the array? and getValue() looks pretty strange too.

    Just don't get what you're trying to accomplish..
     

    Majed

    Senior Member
    Jul 17, 2002
    9,630
    #10
    ++ [ originally posted by Alex ] ++
    Hell, I wanted to excercise the mind, here's a class to add, subtract and multiply matrices :D


    Mayood, eat my dust :D
    i'm having a dust feast! :stress:

    :D

    nice job :thumb:
     
    OP
    Loppan

    Loppan

    Senior Member
    Jul 13, 2002
    2,528
  • Thread Starter
  • Thread Starter #15
    It's the method Add who doesn't work, this line resultmatrix.setValAt[x][y] = m1.getValAt(x) + m2.getValAt(y);:


    Code:
    public class Matriximp implements Matrix {
    	
    	private int [][] matrix;
    	
    	private int x, y;
    	
    	public Matriximp(int x, int y) {
    		
    		this.x=x;
    		this.y=y;
    		
    		matrix = new int[x][y];
    	}
    	
    
    		public Matrix Add(int m1, int m2) {
    		
    		Matriximp resultmatrix = new Matriximp (matrix.length,matrix[0].length);
    		
    		for (int y = 0; y < this.y; y++) {
    			
      			for (int x = 0; x < this.x; x++) {
      				
        								
          		 	  		resultmatrix.setValAt[x][y] = m1.getValAt(x) + m2.getValAt(y);
    	  		}
    		}
    			
    			
    			
    		return resultmatrix;
    		
    	}	
    	
    public int getValAt(int x, int y) {
    		
    		return matrix[x][y];
    		
    	}	
    	
    	
    	public int setValAt(int x, int y) {
    		
    		return matrix[x][y];
    		
    	}
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #16
    I haven't tested your code but make sure you get the coordinates right, that can be tricky. Print your results so that you always make sure your x and y variables have the intended value, I found out it doesn't take a lot to get them mixed up.
     
    OP
    Loppan

    Loppan

    Senior Member
    Jul 13, 2002
    2,528
  • Thread Starter
  • Thread Starter #17
    Can someone please explain this code to me espescilly with the mark ?????:

    void brute(int a[], int lo, int hi) throws Exception{
    if ((hi-lo) == 1) { /// ??????
    if (a[hi] < a[lo]) {
    int T = a[lo];
    a[lo] = a[hi];
    a[hi] = T;
    pause();
    }
    }
    if ((hi-lo) == 2) { ////?????
    int pmin = a[lo] < a[lo+1] ? lo : lo+1;
    pmin = a[pmin] < a[lo+2] ? pmin : lo+2;
    if (pmin != lo) {
    int T = a[lo];
    a[lo] = a[pmin];
    a[pmin] = T;
    pause();
    }
    brute(a, lo+1, hi);
    }
    if ((hi-lo) == 3) { /// ????the whole loop
    int pmin = a[lo] < a[lo+1] ? lo : lo+1;
    pmin = a[pmin] < a[lo+2] ? pmin : lo+2;
    pmin = a[pmin] < a[lo+3] ? pmin : lo+3;
    if (pmin != lo) {
    int T = a[lo];
    a[lo] = a[pmin];
    a[pmin] = T;
    pause();
    }
    int pmax = a[hi] > a[hi-1] ? hi : hi-1;
    pmax = a[pmax] > a[hi-2] ? pmax : hi-2;
    if (pmax != hi) {
    int T = a[hi];
    a[hi] = a[pmax];
    a[pmax] = T;
    pause();
    }
    brute(a, lo+1, hi-1);
    }
    }

    void sort(int a[], int lo0, int hi0) throws Exception {
    int lo = lo0;
    int hi = hi0;
    pause(lo, hi);
    if ((hi-lo) <= 3) {
    brute(a, lo, hi);
    return;
    }
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #18
    Well it's a recursive sorting algorithm from what I can tell. I don't know what the question marks are for, I guess maybe a binary operator or something. Obviously the "/// ????the whole loop" part is just a comment.
     

    Users Who Are Viewing This Thread (Users: 0, Guests: 1)