Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3985836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:56:20+00:00 2026-05-20T05:56:20+00:00

Hey. So my assignment was to read the text files to form the matrices

  • 0

Hey. So my assignment was to read the text files to form the matrices and then perform various functions such as addition and multiplication on the matrices. I fixed some of the errors but the compiler refuses to accept the .get and .set methods for some reason.

import java.io.*;
import java.util.*;
import java.util.Scanner;

 class Matrix {
  double [][] element;
  int rows, cols ;

 Matrix(int rows, int cols){
    this.rows = rows;
    this.cols = cols;
    element = new double [rows][cols];
 }

public double getValue (int row, int col){
return element[row][col];
}

public void setValue (int row, int col, double value){
element[row][col] = value;
}

 public int getNoRows(){  // returns the total number of rows
  return rows;
}

 public int getNoCols(){ // returns the total number of cols
  return cols;
}

// The methods for the main calculations

  public Matrix AddMatrix(Matrix m2){
  int row1 = getNoRows();
  int col1 = getNoCols();
  Matrix result = new Matrix(row1, col1);

  for (int i=0; i<row1; i++){
      for (int j=0; j<col1; j++) {
          result.setValue(i,j, (getValue(i,j) + m2.getValue(i,j)));
     }
}
  return result;
 }

  public Matrix  MultiplyMatrix(Matrix m2){
  if (this.getNoCols != m2.getNoRows)
      throw new IllegalArgumentException ("matrices can't be multiplied");
 int row2 = this.getNoRows();
 int col2 = m2.getNoCols();
  Matrix result = new Matrix(row2, col2);
  for (int i=0; i<row2; i++){
      for (int j=0; j<col2; j++){
             result.setValue(i,j,(this.getValue(i,j)*m2.getValue(i,j)));
     }
 }
 return result;

 }

public Matrix TransposeMatrix(){
  int row3 = this.getNoCols();
  int col3 = this.getNoRows();
  matrix result = new Matrix (row3, col3);
  for (int i=0; i<row3 ; i++){
     for (int j=0; j<col3; j++){
        result.setValue(i,j, (this.getValue(j,i)));
     }
  }
 return result;

}

  public void DisplayMatrix(){
  for (int i = 0; i < this.getNoRows; i++) {
  for (int j = 0; j < this.getNoCols; j++) {
    System.out.print((this.getValue(i,j)) + " ");
}
System.out.print("\n");
  }  
}




  public class Lab1 {
  public static void main(String args[]) {
 //      Matrix  m1 = new Matrix();
//          Matrix  m2 = new Matrix();
   //         Matrix  m3 = new Matrix();

      System.out.println("1. Add two matrices \n");
      System.out.println("2. Multiply two matrices \n");
      System.out.println("3. Take transpose of a matrix \n");
      System.out.println("4. Display a matrix \n");
      System.out.println("5. Exit \n");

      int choice;
      Scanner in = new Scanner (System.in);
      System.printout.ln("Enter your choice /n");
      choice = in.nextInt();

       Matrix m1 =  MatrixReader();
       m1.DisplayMatrix();
       Matrix m2 =  MatrixReader();
       m2.DisplayMatrix();


       if (choice==1){
       Matrix m3 = new Matrix(m1.getNoRows, m1.getNoCols);
       m3 = m1.AddMatrix(m2);
       m3.DisplayMatrix();
    }

       if (choice==2){
       Matrix m3 = new Matrix(m1.getNoRows, m2.getNoCols);
       m3 = m1.MultiplyMatrix(m2);
       m3.DisplayMatrix();
      }



    if (choice==3){
    Matrix m1 = DisplayMatrix();
    Matrix m3 = new Matrix (m1.getNoRows, m1.getNoCols);
    m3=m1.TransposeMatrix();
    m3.DisplayMatrix();
    }

    if (choice ==4){
    System.out.println("Will need to call the DisplyMatrix method for the object /n");
    }

 else {
    System.out.println("Incorrect input. Kindly enter again / n");
 }
}

public static Matrix MatrixReader() throws FileNotFoundException {
System.out.println("Give the filename for the matrix");
Scanner filescanner = new Scanner(System.in);
Scanner scanner = new Scanner(new File(filescanner.nextLine()));
scanner.nextLine(); // removes the first line in the input file
String rowLine = scanner.nextLine();
String[] arr = rowLine.split("=");
int rows = Integer.parseInt(arr[1].trim());

String colLine = scanner.nextLine();
String[] arr2 = colLine.split("=");
int cols = Integer.parseInt(arr2[1].trim());
Matrix test  = new Matrix(rows, cols);

 for (int i=0; i<rows;i++){
     for (int j=0; j<cols;j++) {
         test[i][j]= scanner.nextDouble();
        }
}


 return test;

}

}
                                                                     167,1         Bot
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:56:21+00:00Added an answer on May 20, 2026 at 5:56 am
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication3;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    
    
    /**
     *
     * @author asterisk
     */
    class Matrix {
    
        double[][] element;
        int rows, cols;
    
        Matrix(int rows, int cols) {
            this.rows = rows;
            this.cols = cols;
            element = new double[rows][cols];
        }
    
        public double getValue(int row, int col) {
            return element[row][col];
        }
    
        public void setValue(int row, int col, double value) {
            element[row][col] = value;
        }
    
        public int getNoRows() {  // returns the total number of rows
            return rows;
        }
    
        public int getNoCols() { // returns the total number of cols
            return cols;
        }
    
    // The methods for the main calculations
        public Matrix AddMatrix(Matrix m2) {
            int row1 = getNoRows();
            int col1 = getNoCols();
            Matrix result = new Matrix(row1, col1);
    
            for (int i = 0; i < row1; i++) {
                for (int j = 0; j < col1; j++) {
                    result.setValue(i, j, this.getValue(i, j) + m2.getValue(i, j));
                }
            }
            return result;
        }
    
        public Matrix MultiplyMatrix(Matrix m2) {
            if (this.getNoCols() != m2.getNoRows()) {
                throw new IllegalArgumentException("matrices can't be multiplied");
            }
            int row2 = this.getNoRows();
            int col2 = m2.getNoCols();
            Matrix result = new Matrix(row2, col2);
            for (int i = 0; i < row2; i++) {
                for (int j = 0; j < col2; j++) {
                    result.setValue(i, j, result.getValue(i, j) + this.getValue(i, j) * m2.getValue(i, j));
                }
            }
            return result;
    
        }
    
        public Matrix TransposeMatrix() {
            int row3 = this.getNoCols();
            int col3 = this.getNoRows();
            Matrix result = new Matrix(row3, col3);
            for (int i = 0; i < row3; i++) {
                for (int j = 0; j < col3; j++) {
                    result.setValue(i, j, this.getValue(j, i));
                }
            }
            return result;
    
        }
    
        public void DisplayMatrix() {
            for (int i = 0; i < this.getNoRows(); i++) {
                for (int j = 0; j < this.getNoCols();
                        j++) {
                    System.out.print((this.getValue(i, j)) + " ");
                }
                System.out.print("\n");
            }
        }
    }
    
    
    
    public class Lab1 {
    
        public static void main(String args[]) throws FileNotFoundException {
            //      Matrix  m1 = new Matrix();
    //          Matrix  m2 = new Matrix();
            //         Matrix  m3 = new Matrix();
    
            System.out.println("1. Add two matrices \n");
            System.out.println("2. Subtract two matrices \n");
            System.out.println("3. Take transpose of a matrix \n");
            System.out.println("4. Display a matrix \n");
            System.out.println("5. Exit \n");
    
            int choice;
            Scanner in = new Scanner(System.in);
            System.out.println("Enter your choice /n");
            choice = in.nextInt();
    
            Matrix m1 = MatrixReader();
            m1.DisplayMatrix();
            Matrix m2 = MatrixReader();
            m2.DisplayMatrix();
    
    
            if (choice == 1) {
                Matrix m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
                m3 = m1.AddMatrix(m2);
                m3.DisplayMatrix();
            }
    
            if (choice == 2) {
                Matrix m3 = new Matrix(m1.getNoRows(), m2.getNoCols());
                m3 = m1.MultiplyMatrix(m2);
                m3.DisplayMatrix();
            }
    
    
    
            if (choice == 3) {
                Matrix m1 = DisplayMatrix(); //? DisplayMatrix is a method of class
                                             // so it should be called like matrix.DisplayMatrix()
                Matrix m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
                m3 = m1.TransposeMatrix();
                m3.DisplayMatrix();
            }
    
            if (choice == 4) {
                System.out.println("Will need to call the DisplyMatrix method for the object /n");
            } else {
                System.out.println("Incorrect input. Kindly enter again / n");
            }
        }
    
        public static Matrix MatrixReader() throws FileNotFoundException {
            System.out.println("Give the filename for the matrix");
            Scanner filescanner = new Scanner(System.in);
            Scanner scanner = new Scanner(new File(filescanner.nextLine()));
            scanner.nextLine(); // removes the first line in the input file
            String rowLine = scanner.nextLine();
            String[] arr = rowLine.split("=");
            int rows = Integer.parseInt(arr[1].trim());
    
            String colLine = scanner.nextLine();
            String[] arr2 = colLine.split("=");
            int cols = Integer.parseInt(arr2[1].trim());
            Matrix test = new Matrix(rows, cols);
    
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    test.setValue(i, j, scanner.nextDouble());
                }
            }
    
    
            return test;
    
        }
    }
    

    Some fixes 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I've been working on this problem to read numbers from a text file
Hey folks, beneath is a piece of code i used for a school assignment.
Hey Guys. I need help understanding my hw assignment. I am starting out in
Hey there- I'm creating a Live Wallpaper which includes text that is drawn directly
hey guys, i have a c++ programming assignment that asks me to create a
Hey! I am currently doing some prep for my upcoming college assignment but am
hey, I want to be able to have a gradient fill on the text
Hey, I'm very new to backbone, and I've read a little bit of the
Hey. I need to upload some files (images/pdf/pp) to my SQLS Database and thereafter,
Hey everyone! I am having trouble with understanding modules -- I have two files,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.