Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Matrix {
- int data[][]; // os elementos da matriz em si
- int rows; // numero de linhas
- int cols; // numero de colunas
- // construtor padrao de matriz
- Matrix(int r, int c) {
- data = new int[r][c];
- rows = r;
- cols = c;
- }
- // Ler os rows x cols elementos da matriz
- public void read(Scanner in) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- data[i][j] = in.nextInt();
- }
- }
- }
- // Representacao em String da matrix
- public String toString() {
- String ans = "";
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- ans += data[i][j] + " ";
- }
- ans += "\n";
- }
- return ans;
- }
- public static Matrix identity(int n) {
- Matrix v;
- v = new Matrix(n, n);
- for (int i = 0; i < n; i++) {
- if (v.rows == v.cols) {
- v.data[i][i] = 1;
- }
- }
- return v;
- }
- public Matrix transpose() {
- Matrix novaNig = new Matrix(this.cols, this.rows);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- novaNig.data[j][i] = this.data[i][j];
- }
- }
- return novaNig;
- }
- }
- class TestMatrix {
- public static void main(String[] args) {
- Scanner stdin = new Scanner(System.in);
- Matrix oof = new Matrix(4, 5);
- oof.read(stdin);
- System.out.println(oof);
- System.out.println("----------------------\n\n");
- Matrix oofTranspose = oof.transpose();
- System.out.println(oofTranspose);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement