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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:17:31+00:00 2026-05-23T11:17:31+00:00

I have an assignment where Im supposed to finish the implementation on a generic

  • 0

I have an assignment where Im supposed to finish the implementation on a generic sparse matrix. Im stuck on the addition part. The matrix is only going to support numbers so I had it extend Number hoping I could then add the numbers, thats wrong. The data structure is NOT an array, it is essentially 2 linked lists. (one for rows and one for columns) Here is the code in question:

public MatrixSparse<? extends Number> addition(MatrixSparse<? extends Number> A, MatrixSparse<? extends Number> B, MatrixSparse<? extends Number> result) {
    for (int i = 0; i < r.length; i++) {
        for(int j = 0; j < c.length; j++) {
            // set (i, j) to the sum of A(i,j) and B(i,j) is giving me an error
            // "+" is undefined for type capture#2-? etc.
            result.set(i, j, (A.get(i, j) + B.get(i, j)));
        }
    }
    return result;
}

and the class header + class variables:

class MatrixSparse<T extends Number> { 
final Links r[]; 
final Links c[];
final int rows, columns; 
final T zero; 

any suggestions about how to implement this add method?

  • 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-23T11:17:31+00:00Added an answer on May 23, 2026 at 11:17 am

    Treating then that as a mental/school exercise: You cannot add two generics together with “+” operator – operators are not “genericable” and you cannot overload them in Java (pretty much different than C++) and auto-boxing does not help. The only thing you can do I think is to write a generic T add(T paramLeft,T paramRight) in your Matrix and do something like that:

    if (paramLeft instanceof Integer) {
         return new Integer(((Integer)paramLeft).intValue()+ ((Integer)paramRight).intValue());
    } elseif (paramLeft instanceof Double) { 
         ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment in a language-independent class, and part of it is using
I have an assignment where I am supposed to be able to display the
I have an assignment from school where the program is supposed to close when
Have a homework assignment in which I'm supposed to create a vector of pointers
I have an assignment that I'm supposed to implement the MIPS processor in C++
I was given an assignment to write a program to print only even numbers
For an assignment, part of what I have to do involves the use of
I have a homework assignment where I'm supposed to find the cheapest airfares between
Our assignment is to have a server-client model.... We're supposed to check for a
I am a student trying to finish a lab assignment. 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.