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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

is my code right?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
is my code right? i am trying to calculate the 3 inputs by this formula..a+b*55+a^2/c-(7+a)!+a*2^a+(a/b/c)!
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hey

^ is an bitwise XOR operator, not a power of. You can find power of function and other useful math functions in java.lang.Math class.

Your ! operation might best fit into its own function
 
Marshal
Posts: 80759
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Beware of java.io.Console; if you use javaw to start your application from a .jar, the Console will be null and you will suffer a NullPointerException.

You will have to write down the algorithm with a pencil and paper to see whether your code is correct.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I think having a number of functions with meaningless names doesn't help with code readability. I would do all the calculations in one method. Alright, two - one would be needed for factorial operation, which by the way can be nicely calculated via recursion java factorial recursive
 
Campbell Ritchie
Marshal
Posts: 80759
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Agree with Garik Ustinov. Also: avoid Math.pow(x, 2) for squares. Use x * x which gives better performance and better precision.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why don't you test your code yourself? Wouldn't that be faster than asking strangers to do it?

I'd also suggest you try writing less at a time before testing. I often write as little as 1 or two lines before I re-compile, test, fix, etc.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:Why don't you test your code yourself? Wouldn't that be faster than asking strangers to do it?

I'd also suggest you try writing less at a time before testing. I often write as little as 1 or two lines before I re-compile, test, fix, etc.



alright , i edited my whole code, i think it is okay,, check it above (i edited it)

i tested it
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:alright , i edited my whole code, i think it is okay,, check it above (i edited it)

i tested it


If you tested it, then you should know if it's right or not. If you are aware of some specific problem, tell us.

The purpose of this site is not to provide free code, testing, or homework checks. We are thrilled to help you learn, and people will bend over backwards to help you if you have a question about something.

I doubt that many folks are going to provide you with free testing services.
 
Campbell Ritchie
Marshal
Posts: 80759
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I will happily provide free testing, Fred. And I got




























. . . .




















compiler errors.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That's a serious error too.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
well then...i would say it's not right.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:

Dmitri Makovetskiy wrote:alright , i edited my whole code, i think it is okay,, check it above (i edited it)

i tested it


If you tested it, then you should know if it's right or not. If you are aware of some specific problem, tell us.

The purpose of this site is not to provide free code, testing, or homework checks. We are thrilled to help you learn, and people will bend over backwards to help you if you have a question about something.

I doubt that many folks are going to provide you with free testing services.




i tested it, but i am not sure if i have a mistake somewhere...

i dont know if the calculation behind this code is right..cause sometimes i get a large minus
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:well then...i would say it's not right.



I think we need a definition of "right". Dmitri, what do you mean by right? Are you asking if the code compiles, if it returns correct results, if it is well written OO-wise, if you have followed all the proper naming conventions, etc.? Some of these (compiling, returning correct results) are easy enough for you to test on your own. Other pieces may require a more experienced eye.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Well this is a function you can test by hand yes? So if your result by hand doesn't match the result of your program, there is a mistake.


Hunter
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Hunter McMillen wrote:Well this is a function you can test by hand yes? So if your result by hand doesn't match the result of your program, there is a mistake.


Agreed.

Dmitri, what you're basically saying is that you are too lazy to check it yourself, and you want someone here to do it for you.

If, instead, you wrote a post saying "I used values 3, 4 and 5 for my inputs, and was expecting 54 but got -3622014.05", then people would see you trying and want to help you.

instead, many people see you doing as little as possible and asking people to do the work you should do. They're getting tired of it, and many will not bother with you anymore.

Please, for your own sake, ShowSomeEffort.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:

Hunter McMillen wrote:Well this is a function you can test by hand yes? So if your result by hand doesn't match the result of your program, there is a mistake.


Agreed.

Dmitri, what you're basically saying is that you are too lazy to check it yourself, and you want someone here to do it for you.

If, instead, you wrote a post saying "I used values 3, 4 and 5 for my inputs, and was expecting 54 but got -3622014.05", then people would see you trying and want to help you.

instead, many people see you doing as little as possible and asking people to do the work you should do. They're getting tired of it, and many will not bother with you anymore.

Please, for your own sake, ShowSomeEffort.



fair enough fred, i re wrote half of the code after i posted it and after your comments.. i have no idea how to polish it further
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
rewriting wasn't the issue you were experiencing, testing was.


Hunter
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Hunter McMillen wrote:rewriting wasn't the issue you were experiencing, testing was.



Agreed. If you can't test it, you don't know why it's wrong. And if you don't know why it's wrong, then the "rewriting" are just non-targeted changes.

Henry
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Henry Wong wrote:

Hunter McMillen wrote:rewriting wasn't the issue you were experiencing, testing was.



Agreed. If you can't test it, you don't know why it's wrong. And if you don't know why it's wrong, then the "rewriting" are just non-targeted changes.

Henry



i did test it.

it gave me a long number, sometimes the number is negative..i havent got an idea if i am right in my calculation
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If you post the 3 numbers used - and then the result, then we can see if you have done the calculation correct

BTW. if you are using the above code - then the factorial operation methods g and i are not correct (but almost).
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rene Larsen wrote:If you post the 3 numbers used - and then the result, then we can see if you have done the calculation correct

BTW. if you are using the above code - then the factorial operation methods g and i are not correct (but almost).



i use 1, 2, 3 and get - -40208
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
is that what YOU calculated for the answer, or what you code gave you?

Why does the fact that your answer 'is sometimes negative' bother you? (7+a)! is going to be a rather large number if a is anything greater than 1. The only other term.

a*2^a might get large, but factorial grows faster (I think).
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:is that what YOU calculated for the answer, or what you code gave you?

Why does the fact that your answer 'is sometimes negative' bother you? (7+a)! is going to be a rather large number if a is anything greater than 1. The only other term.

a*2^a might get large, but factorial grows faster (I think).




thanks Fred, this thread is history, i have already submitted my homework

okay the code is correct, my teacher confirmed it.. thank you for your unhelpful assistence everyone
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:okay the code is correct, my teacher confirmed it.. thank you for your unhelpful assistence everyone


People on JavaRanch have good intentions to help you learn Java. You don't learn anything if someone else does your homework for you and just gives you the answer. So instead, we try to give you hints so that you can solve the problem yourself, and learn by doing it yourself.

If you don't like that, and rather be lazy and get the answer spoonfed, then JavaRanch maybe isn't the right place for you.
 
    Bookmark Topic Watch Topic
  • New Topic