aspose file tools*
The moose likes Beginning Java and the fly likes is my code right? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

is my code right?

Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
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)!
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
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


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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.
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
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


SCJP, SCWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Agree with Garik Ustinov. Also: avoid Math.pow(x, 2) for squares. Use x * x which gives better performance and better precision.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
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
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
I will happily provide free testing, Fred. And I got




























. . . .




















compiler errors.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

That's a serious error too.


"If the facts don't fit the theory, get new facts" --Albert Einstein
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

well then...i would say it's not right.
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
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
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

Joined: Jun 21, 2010
Posts: 128
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

Joined: Mar 13, 2009
Posts: 492

rewriting wasn't the issue you were experiencing, testing was.


Hunter
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
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
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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).


Regards, Rene Larsen
Dropbox Invite
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
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
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

Joined: Jun 21, 2010
Posts: 128
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
Consider Paul's rocket mass heater.
 
subject: is my code right?