Razvan Adam wrote:Hi.
The problem in your code is that you are not accessing the values of the variables of the created object correctly. To do so you need to use the this keyword as in the bellow example.
mph = this.distance / (this.minutes * 60) + this.hours;
Jonah Jones wrote:1) Part of my code was supplied by my instructor, who had already defined distance as int. I'm not super well-versed in how to define things, so I left it as-is. Is distance being defined as int causing an issue?
Jonah Jones wrote:2) The formula doesn't work. Thank you for pointing that out. I changed it to mph = distance / ((minutes / 60) +hours);
If there is another issue with my formula that I didn't address with my change, please let me know.
. . . and that didn't work either, did it? Assuming you are counting minutes the usual way, work out what minutes / 60 will evaluate to. This is a problem you will encounter every time you try division with integer types; it is because of the slightly strange rules of integer division and those rules apply in other languages, too.Jonah Jones wrote:. . . mph = distance / ((minutes / 60) +hours); . . .
Carey Brown wrote:If you attempt to divide two integer values you'll get an integer result:
1 / 3 = 0 because 3 goes into 1 zero times
If you divide where either (or both) operand is a double, then the result will be a double.
1 / 3.0 = 0.3333333333333
Try 100 miles in 2hours 30min. That should average out to 40mph.Yesterday, I wrote:. . . mph = distance / ((minutes / 60) +hours); . . .. and that didn't work either, did it? . . . .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |