Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Proyecto 6 - Programación 2

NotificationsYou must be signed in to change notification settings

KPlanisphere/Rational-Matrix-Operations-in-Java

Repository files navigation

This repository contains a Java project that performs operations on matrices of rational numbers. The project includes classes for defining rational numbers, handling matrix operations, and displaying results.

Features

  • Rational Number Representation: Defines rational numbers with methods for basic arithmetic operations.
  • Matrix Operations: Implements operations such as addition, subtraction, and multiplication for matrices of rational numbers.
  • User Interaction: Handles user input to create matrices and perform operations.
  • Result Display: Outputs the results of the matrix operations.

Code Snippets

Main Class

The main class initializes the program, handles user input, and invokes methods for matrix operations.

publicclassPrincipal {publicstaticvoidmain(String[]args) {Scannerscanner =newScanner(System.in);// Read matrix dimensionsSystem.out.print("Enter the number of rows: ");introws =scanner.nextInt();System.out.print("Enter the number of columns: ");intcols =scanner.nextInt();// Create and read matricesTMatrizRacionalmatrix1 =newTMatrizRacional(rows,cols);TMatrizRacionalmatrix2 =newTMatrizRacional(rows,cols);System.out.println("Enter the elements of the first matrix:");matrix1.readMatrix(scanner);System.out.println("Enter the elements of the second matrix:");matrix2.readMatrix(scanner);// Perform operationsTOpMatricesRacionalesoperations =newTOpMatricesRacionales();TMatrizRacionalsum =operations.add(matrix1,matrix2);TMatrizRacionaldifference =operations.subtract(matrix1,matrix2);TMatrizRacionalproduct =operations.multiply(matrix1,matrix2);// Display resultsSystem.out.println("Sum of matrices:");sum.printMatrix();System.out.println("Difference of matrices:");difference.printMatrix();System.out.println("Product of matrices:");product.printMatrix();    }}

Rational Number Class

TheTRacional class represents a rational number and includes methods for arithmetic operations and simplification.

publicclassTRacional {privateintnumerator;privateintdenominator;publicTRacional(intnumerator,intdenominator) {this.numerator =numerator;this.denominator =denominator;simplify();    }privatevoidsimplify() {intgcd =gcd(numerator,denominator);numerator /=gcd;denominator /=gcd;    }privateintgcd(inta,intb) {while (b !=0) {inttemp =b;b =a %b;a =temp;        }returna;    }publicTRacionaladd(TRacionalother) {intnum =this.numerator *other.denominator +other.numerator *this.denominator;intden =this.denominator *other.denominator;returnnewTRacional(num,den);    }publicTRacionalsubtract(TRacionalother) {intnum =this.numerator *other.denominator -other.numerator *this.denominator;intden =this.denominator *other.denominator;returnnewTRacional(num,den);    }publicTRacionalmultiply(TRacionalother) {intnum =this.numerator *other.numerator;intden =this.denominator *other.denominator;returnnewTRacional(num,den);    }publicTRacionaldivide(TRacionalother) {intnum =this.numerator *other.denominator;intden =this.denominator *other.numerator;returnnewTRacional(num,den);    }@OverridepublicStringtoString() {returnnumerator +"/" +denominator;    }}

Matrix Class

TheTMatrizRacional class represents a matrix of rational numbers and includes methods for reading and printing matrices.

publicclassTMatrizRacional {privateTRacional[][]matrix;privateintrows;privateintcols;publicTMatrizRacional(introws,intcols) {this.rows =rows;this.cols =cols;matrix =newTRacional[rows][cols];    }publicvoidreadMatrix(Scannerscanner) {for (inti =0;i <rows;i++) {for (intj =0;j <cols;j++) {System.out.print("Enter numerator for element [" +i +"][" +j +"]: ");intnum =scanner.nextInt();System.out.print("Enter denominator for element [" +i +"][" +j +"]: ");intden =scanner.nextInt();matrix[i][j] =newTRacional(num,den);            }        }    }publicvoidprintMatrix() {for (inti =0;i <rows;i++) {for (intj =0;j <cols;j++) {System.out.print(matrix[i][j] +" ");            }System.out.println();        }    }publicTRacionalgetElement(introw,intcol) {returnmatrix[row][col];    }publicvoidsetElement(introw,intcol,TRacionalvalue) {matrix[row][col] =value;    }publicintgetRows() {returnrows;    }publicintgetCols() {returncols;    }}

Matrix Operations Class

TheTOpMatricesRacionales class contains methods for performing operations on matrices of rational numbers.

publicclassTOpMatricesRacionales {publicTMatrizRacionaladd(TMatrizRacionalm1,TMatrizRacionalm2) {introws =m1.getRows();intcols =m1.getCols();TMatrizRacionalresult =newTMatrizRacional(rows,cols);for (inti =0;i <rows;i++) {for (intj =0;j <cols;j++) {TRacionalsum =m1.getElement(i,j).add(m2.getElement(i,j));result.setElement(i,j,sum);            }        }returnresult;    }publicTMatrizRacionalsubtract(TMatrizRacionalm1,TMatrizRacionalm2) {introws =m1.getRows();intcols =m1.getCols();TMatrizRacionalresult =newTMatrizRacional(rows,cols);for (inti =0;i <rows;i++) {for (intj =0;j <cols;j++) {TRacionaldifference =m1.getElement(i,j).subtract(m2.getElement(i,j));result.setElement(i,j,difference);            }        }returnresult;    }publicTMatrizRacionalmultiply(TMatrizRacionalm1,TMatrizRacionalm2) {introws =m1.getRows();intcols =m1.getCols();TMatrizRacionalresult =newTMatrizRacional(rows,cols);for (inti =0;i <rows;i++) {for (intj =0;j <cols;j++) {TRacionalproduct =newTRacional(0,1);for (intk =0;k <cols;k++) {TRacionaltemp =m1.getElement(i,k).multiply(m2.getElement(k,j));product =product.add(temp);                }result.setElement(i,j,product);            }        }returnresult;    }}

Usage

  1. Compile the Java files using a Java compiler (e.g.,javac).
  2. Run the main class (Principal) to start the program.
  3. Follow the prompts to enter the dimensions and elements of the matrices.
  4. The program will perform the specified operations and display the results.

Classes and Methods

  • Principal: The main class that handles user input and program execution.
    • main(String[] args): The entry point of the program.
  • TRacional: A class representing a rational number with methods for arithmetic operations and simplification.
    • TRacional(int numerator, int denominator): Constructor that initializes the rational number.
    • add(TRacional other),subtract(TRacional other),multiply(TRacional other),divide(TRacional other): Methods for arithmetic operations.
    • toString(): Method that returns the string representation of the rational number.
  • TMatrizRacional: A class representing a matrix of rational numbers with methods for reading and printing matrices.
    • TMatrizRacional(int rows, int cols): Constructor that initializes the matrix dimensions.
    • readMatrix(Scanner scanner): Method that reads matrix data from the user.
    • printMatrix(): Method that prints the matrix.
    • getElement(int row, int col),setElement(int row, int col, TRacional value): Methods to get and set matrix elements.
  • TOpMatricesRacionales: A class containing methods for performing matrix operations.
    • add(TMatrizRacional m1, TMatrizRacional m2): Method that adds two matrices.
    • subtract(TMatrizRacional m1, TMatrizRacional m2): Method that subtracts two matrices.
    • multiply(TMatrizRacional m1, TMatrizRacional m2): Method that multiplies two matrices.

[8]ページ先頭

©2009-2025 Movatter.jp