카테고리 없음

클래스, 메서드 coding 연습 6

akasha.park 2023. 3. 17. 15:41

import java.util.Random;

public class Array {
    private String title;
    private int row;
    private int col;
    int[][] array ;
	public Array(String title, int row, int col) {		 
		this.title = title;
		this.row = row;
		this.col = col;
		array = new int[row][col];
	}
	public int getRow() {
		return row;
	}
	public void setRow(int row) {
		this.row = row;
	}
	public int getCol() {
		return col;
	}
	public void setCol(int col) {
		this.col = col;
	}
	public int[][] getArray() {
		return array;
	}
	public void setArray(int[][] array) {
		this.array = array;
	}
	public void makeArrayData() {
		for(int i=0;i<array.length;i++) {
			for(int j=0;j<array[i].length;j++) {
				array[i][j] = getRandomNumber();
			}
		}
	}
	private int getRandomNumber() {
		Random r = new Random();
		return r.nextInt(row*col)+1;
	}
	public void printArray() {
		System.out.println("## "+title+" Array 출력");
		for(int i=0;i<array.length;i++) {
			for(int j=0;j<array[i].length;j++) {
				 System.out.print (array[i][j]+" ");
			}
			System.out.println();
		}
	}
	public void findMatchNumber(Array src, Array desc) {
		int count = 0;
		System.out.print("#일치하는 숫자 : ");
		for(int i=0;i<array.length;i++) {
			for(int j=0;j<array[i].length;j++) {
				if(src.getArray()[i][j]==desc.getArray()[i][j]) {
				System.out.print("["+i+"]["+j+"]="+array[i][j]+",");
				count++;
				}
			}			
		}
		System.out.println();
		System.out.print ("#일치하는 숫자 갯수: " + count);
	}
    
}
public class ArrayTest {

	public static void main(String[] args) {
		Array src = new Array("첫번째",  3 ,4);
		Array desc = new Array("두번째", 3 ,4 );
		src.makeArrayData();
		desc.makeArrayData();
		src.printArray();
		desc.printArray();
		src.findMatchNumber(src, desc);
	}

}