Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Does anyone know why I\'m getting this error when it is set as ints in my classe

ID: 3817376 • Letter: D

Question




Does anyone know why I'm getting this error when it is set as ints in my classes, and if I take away the .01 it fails. Can someone help?

the results produced by your tests Your code failed to compile cottectly against the reference tests this is most likely because you have not named your classes) as required in the assignment, have failed to provide one or more required method Failure to follow these constraints will prevent the proper assessment of your solution and your tests. The following specific emor(s) were discovered while compiling reference tests against your submissi int test Var 894 error ssible loss of precision getLayers0 required: int es/completed Code. Projecte9Tests java 998: errar ssible loss of precision int testvar wc-get Tiers required int double 2 errors Interpreting Your Correctness/Testing Score (ol65) Downloadables ar submission

Explanation / Answer

As per the error trace shown, the issue is that yoou are trying to assign a double value to the int value . Here is the detailed explanation of your issue: -

method Assert.assertEquals has following signature: -

assertEquals(double expected, double actual, double delta) i.e it holds three parameters, expected actual and the acceptable difference between them, i.e. the method will throw AssertionError if, expected and actual DOUBLE is not equal and even not within the possible delta.

So this method accepts all three doubles. There is no issue with 0.01 you have in your call. as it is double only.

The issue is with expected/actual value. while calling the method, you have supplied two integers in place of double. which is ok, because double holds higher precedence than int, So int can be stored/cast into double easily and this task is done automatically by compiler without lossing precision. i.e. expected value 7 will be casted to 7.0 to be sent to the method for execution.

Now here comes the problem. Your test case is expecting int value , not double. As int has lesser precedence than double, each double needs to be type casted manually because there is loss of precision involved. Loss of precision means if double is 4.23, than int will take 4 and ignore 0.23.

As both your expected and actual were casted to double by compiler(because of assertEquals), you have to now manually type cast to int for test case to work. But you are not doing this. So this error is coming.

So please take care of type casting in your test cases whereever you need to store double in int . The program will work without errors then.

Sample type casting for you is as follows: -

int testVar = (int)wc.getTiers();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote