Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

' + ' Operator problem  RSS feed

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,


I have to classes, class istances and a main class TestDistances. In test distances I like to calculate the rate , so I called the method milesperhour. and I am getting an error at 1. Can anybody please tell me why am I worng.

Error : operator + cannot be applied to distances.Distances,distances.Distances






 
Rick Goldstein
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at what your static convertXXX methods return. Remember that you can only use the '+' operator for numeric types (and only primitive numerics up until Java 5) or Strings.

-Rick
 
Rick Goldstein
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when you're done with that, I just noticed that you might want to look at your calculations one more time. (Hint: what kind of operation does '%' represent? I don't think it's what you think. And that's not the only mistake.)

Rick

Ed. for punctuation.
[ April 01, 2005: Message edited by: Rick Goldstein ]
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get you.
I have to use return testyards; in convertXXX() method. I just want to calcuate the rate by calling the 2 methods from Distance class.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prerna boja:
I did not get you.
I have to use return testyards; in convertXXX() method. I just want to calcuate the rate by calling the 2 methods from Distance class.


Having the convertXXX() methods returning distance objects is perfectly valid. However, Java doesn't know how to add them... hence, the error.

Henry
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how can I solve my problem.
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prerna,

As Rick pointed out, you can perform the + operation only on string and numeric primitives. Your return type is an object: distances.

Secondly, you do not need to instantiate a new Distances in the same class, coz u can use the private variables (they are not local, they are instance variables) as they are.

Also, I didnt quite get the purpose behind declaring distance1 in the main method.

You need to change your methods and return types in Distances to make this function work.

Have a good one!

-Kiennjal

[ April 01, 2005: Message edited by: kiennjal shah ]
[ April 01, 2005: Message edited by: kiennjal shah ]
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am still unable to get the result, I can't change the return type in convertXXX() mtd,because the question requires tht. Can you please tell me how can I go about it....
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post ur new code and also the question as specified in the book or wherever that you got it from... so that we can get a better idea.

Thank You

-Kiennjal
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



-----------------------------------------------------------------------------------------------




Actual Question is : Write a helper mthd in TestDistances tht takes a varialbe of class Distances and a time(in minutes) and calculates miles per hour. Convert the variable of class Distances to miles(a float variable) before performing the calculation.The result should be float value representing miles per hour rather than a value of Distances.
function heading should be as shown below:
public float milesPerHour(Distances distance,long time)

Use your function to calculate the miles per Hour where the distance is 15,000 feet + 12,000 yards +37 miles and time is 45 minutes.
 
Rick Goldstein
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your Distances class has a lovely set of accessor methods (getXXX). Note their return types. Think about what you are actually trying to add together. Also note that Distances has an add() method--you might think about whether that would be useful.

Also, don't forget to carefully check your convertXXX methods. The computations are not correct. There are several other problems with your Distances class, which you will probably discover eventually. Was Distances provided as part of the problem, or did you write it yourself? Perhaps you were intended to debug it?

Rick

[ April 01, 2005: Message edited by: Rick Goldstein ]
[ April 01, 2005: Message edited by: Rick Goldstein ]
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your ConvertXXX() methods should have return type float/double.

Also, according to the question: i get an idea tht:

public float milesPerHour(Distances distance, float time)
{
//my understanding as per the problem: here is where u call the ConvertXXX methods using the object distance
//now u have float/double miles converted from yards and feet
//use these values to calculate rate (which btw, u are not doing)
// return rate
}

also, where are you calling this milesPerHour method?

hope this helps.

- Kiennjal
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to insert as you said

distance = (distance.convertFeet(1500)) +(distance.convertYards(1500)) + (37) ;

but still I get the same error.

And my convertXXX() mtd should return testyards of type Distance as per the question for Distance class,I can not change it to float/double.
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleaseeeeeeeeee help me out with the above problem.
Thanks in advance.
 
Rick Goldstein
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're trying really hard to help. I have given you a lot of hints. Seems like the style here is to try not to explicitly give the answers to homework problems. Once again:

1. Look at the getXXX methods of the Distances class.
2. Look at the add() method of the Distances class.

THINK about what you are trying to do, and how that relates to 1) and 2) above.

Rick
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What many are trying to say is this:

You can only add primitives, but your method returns an object. How do you obtain the primitive values to add from the objects that are returned? Use the getXXX methods. Alternatively, use the add method in the objects that you have and add one to the other.

Any more and we're writing the code for you...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!