0

Its just a OOP test I was doing, could you please help me understanding the concept of how to set the Multidimensional array value by passing the object. Please check the below code;

 public class agnts {



 private int rate[][] = new int [4][5];

 public agnts(){
 rate[0][1] = new pric(25);
 rate[1][1]=new pric(30);
 rate[2][1]= new pric(45);
 rate[3][1]=new pric(55);
 }    
 }
 class pric {
 //int pric [][]= new int [4][5];
 int pric;
public pric(int p){

    this.pric=p;
}
 public int getPric(){
    return pric;
}
}
 public static void main(String[] args) {        

  agnts tz=  new agnts();
  }

I get an error of type incompatible required:int found:pric Am I doing something wrong? please advise.

1
  • well you have an int[][] and your are trying to set its elements to your class pric. You need to do rate[X][Y]=new pric(Z).getPric();. Better yet, read this. Commented Apr 9, 2013 at 21:42

4 Answers 4

2

You're getting an error because of this

private int rate[][] = new int [4][5];
// You declare a multi-dimensional integer array.

public agnts(){
  // you then attempt to assign objects to that array.
  rate[0][1] = new pric(25);
  rate[1][1]=new pric(30);
  rate[2][1]= new pric(45);
  rate[3][1]=new pric(55);
}    

you declared matrix of integers but you're initializing it with your pric class..

UPDATE 1: as bmorris591 suggested at is comment:

 public agnts(){
     rate[0][1] = new pric(25).getPric();
     rate[1][1]=new pric(30).getPric();
     rate[2][1]= new pric(45).getPric();
     rate[3][1]=new pric(55).getPric();
  }    

UPDATE 2: if you want to set random number for the all matrix:

for (int row = 0; row < rate.length; row ++ )
 for (int col = 0; col < col[row].length; ; col ++)
    rate[row][col] = (int)(Math.Random()*10+1); // this will generate number between 1 to 10
Sign up to request clarification or add additional context in comments.

1 Comment

can you please point me what I need to do to fix it any example would be very appreciated
0

rate is an array of arrays of integers:

private int rate[][] = new int [4][5];

so rate[0][1] is an int.

So, since it's an int, assigning it an instance of pric makes no sense:

rate[0][1] = new pric(25);

You might want to initialize it to an int:

rate[0][1] = 25;

Or you might want to make rate an array of arrays of pric:

private pric[][] rate = new pric[4][5];

Note that Java conventions tell that classes should start with an uppercase letter. Choosing good names makes the code more readable also. What is a pric? What is an agnts?

Comments

0

You have declared an array of int

private int rate[][] = new int[4][5];

And you have class pric which contains an int.

You are trying to set members of the array to be equal to new pric(X), this is the same as

int i = new pric(X);

If you cannot see what is wrong with that then maybe start here and work your way through.

Comments

0

You are calling the constructor for pric here: rate[0][1]=new pric(25);

The constructor returns an object of class pric, but you want to call getPric() on that instead: rate[0][1]=new pric(25).getPric();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.