• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Two Dimensional Object Array Confusion  RSS feed

 
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am stuck with two dimensional Object array.

My question
getPrice() method in Album suppose to do below. I coded exactly that in mind, why do I get the this error?
Create the getPrice method. The price of an album should be created by summing up the prices of each of the Photograph elements in the photos array. If the weight of an album is greater than 10.0, add 5.00 to the price. Otherwise only add 2.50 to the price.


Album


Photograpgh



Item


Error
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:
I am stuck with two dimensional Object array.

The photos reference variable is for an array of photograph objects. Where is the "two dimensional" object array?

Henry
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the test file
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I need to touch elements of Photograph object array, but I can't. I entered hard value of 1 to touch them there's still an error:

Album.java:17: error: bad operand types for binary operator '+'
            priceResult = photos[1] + priceResult;
                                    ^
  first type:  Photograph
  second type: double
1 error
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below code fixed that particular problem.


I simply needed to use a method to get the price, I was treating it a two dimensional array.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:So I need to touch elements of Photograph object array, but I can't. I entered hard value of 1 to touch them there's still an error:

Album.java:17: error: bad operand types for binary operator '+'
            priceResult = photos[1] + priceResult;
                                    ^
  first type:  Photograph
  second type: double
1 error


Should be like this. there is only one array. And photos[1] is Photograph, you should get the price by calling the instance method.

for(int i=0;i<photos.length;i++){
     priceResult = photos[i].getPrice() + priceResult;
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!