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 499581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:00:00+00:00 2026-05-13T06:00:00+00:00

I know this question was answered before, but I cant still handle it with

  • 0

I know this question was answered before, but I cant still handle it with my code.
Please can someone pointing out how can I fix it on this particular code. Id like to call trace(); but dont know where to call new trace. I tried different stuff from here but it does not work for me. Thank you!

package matr;

import java.util.Scanner;

final public class Matrix {

    private final int M;
    private final int N;
    private double[][] data;



    public Matrix(int M, int N) {

        this.M = M;
        this.N = N;

        data = new double[M][N];

    }

    public Matrix(double[][] data) {
        M = data.length;
        N = data[0].length;
        this.data = new double[M][N];
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                this.data[i][j] = data[i][j];
    }

    private Matrix(Matrix A) {
        this(A.data);
    }

    public static Matrix random(int M, int N, int r) {

        Matrix A = new Matrix(M, N);
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                A.data[i][j] = (Math.random() * r);
            }
        }
        return A;
    }

    public double trace() {
        // trace a = new trace();

        double t = 0;
        for (int i = 0; i < Math.min(M, N); i++) {
            t += data[i][i];
        }
        return t;
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("rows: ");
        try {
            int x = Math.abs(scan.nextInt());

            System.out.println("columns: ");
            int y = Math.abs(scan.nextInt());
            System.out
                    .println("generate: ");
            int r = scan.nextInt();

            Matrix A = Matrix.random(x, y, r);
            System.out.println("random A");

            trace();

        } catch (java.util.InputMismatchException e) {
            System.out.println("Please enter a valid int");
        }
    }
}
  • 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-13T06:00:01+00:00Added an answer on May 13, 2026 at 6:00 am

    call A.trace()

    1. use lower-case names for variables and fields
    2. your main method is static. and your trace() method is a non-static method of the Matrix class. That means you have to call trace() on an instance of Matrix.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this type of question has been asked and answered before but I
I know this question can be answered by searching in google. But I have
I know this question was asked here many times before but I am still
I know this must have been answered before here, but I simply can't find
I know this question has been answered before in this thread , but I
I know this question has been asked and answered before, but none of the
I know this question has been asked and answered before. But when I try
I know this question has been answered before but looking at most of them,
I know this question has been answered before, but it's just not working for
I know this question might be little silly but I can't seem to find

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.