• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Confusion on Automated Testing

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im in an intro to java programming course and we're doing a project in Bluej where we need to use automated testing to run our code. The test code is given and cannot be changed, and we have to write code and run the tests successfully. Mine is all running smoothly except for this last part. Here is a snippet from the test code:

And here is the method I wrote for it. It compiles but when I run it through the test I get a failed error that says "expected:<160> but was:<180>" I thought I had the formula correct but I guess not.

I'm not sure what I'm doing wrong here. Any insight would be awesome. Thanks in advance!
 
Marshal
Posts: 3457
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kib ben wrote:I'm not sure what I'm doing wrong here.


It looks like you are reducing by a value of 20, not 20 percent.
    200 - 20 = 180
 
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and why are you using doubles when you are passing ints?
 
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... And THIS is why we have unit testing!!!

It's only fair to note, however, that it's very easy to confuse percentage (often an integer from 0-100, inclusive wiith a percentage value (same thing, but divided by 100). So one has to keep that in mind when designing the application code. And the tests, of course!
 
Sheriff
Posts: 16103
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: The unit test itself contains the formula you need to use in your spray() method.
 
Junilu Lacar
Sheriff
Posts: 16103
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint #2: You can use the -= operator for this to simplify your code.
 
Junilu Lacar
Sheriff
Posts: 16103
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint #3: Be careful with integer division. 1 / 2 == 0, not 0.5 (you can try it in jshell)

Hint #4: Including a double value in an expression will force the conversion of other terms to double.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic