• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculate my cylinders volume in a different method.  RSS feed

 
Robin Al-salehy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everybody!

I have made a code and it works perfectly fine just need fine tuning after I'm done with it.

What I'm trying to figure out as a beginner is that how do I calculate my cylinders volume in a different method? What am I doing wrong?'

I also know that I have used (try- catch) which I will change to do-while so the program won't overload.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JOptionPane.showMessageDialog(null, "The volume of the cylinder is approximately "+ r.format(volume) +"CM^3"); // output results.

You have used volume variable but not defined it. You could us like:



I also know that I have used (try- catch) which I will change to do-while so the program won't overload.

Also try/catch and do/while have different purposes and not related with each other. I don't understand what do you mean by "so the program won't overload. "
 
Robin Al-salehy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been told by professionals that the program I'm trying to make will overload in the end with do- catch. But what do I know? Could you please explain what the difference is?

And thank you for your input sir! =)

I have fixed my code by my self actually so this topic was a waste lol. maybe I can try to explain what I did...! so other beginners will understand.

I took the declared variables in the beginning which the user gave a value out of the do- while. Which gave a connection to the other method downstairs, the one that calculates the volume of the cylinder.

I fixed the "volume unresolved" ->r.format(volume)<- and changed it to ->r.format(volume(rad,hei))<-

Anyway now I have a new problem. My problem is I want to make sure the user inputs value or Error message appears. Right now my error message appears anyway after
the calculations and that is not suppose to happen. What kind of logic method should I write in "While (logic--) " so i can make my error message work properly?

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing a good program is like explaining something to a child. Start with small simple words explaining broad concepts. Here's what your program is supposed to do:

These few lines could be the body of your main method, slightly rewritten so it compiles. For each line, you can write another short story and implement it as a new method.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

My, you have some very old‑fashioned code in that program. Stop using option panes for keyboard input. Try this instead.
Stop using number format. Use System.out.printf which you can read more about in the Java™ Tutorials, or try String#format(), which works very much like printf.
If you are having difficulty with the loop, there are several possible answers:-
  • 1: Get rid of the loop. It doesn't actually do anything. If you did manage to pass null, which is difficult from option pane and scanner, you would have suffered an Exception and the program would have crashed before you reach the loop.
  • 2: Read very carefully what the condition for entering the loop is. Read it aloud several times slowly until you see the problem.
  • The volume method is good, except that you should usually avoid Math#power if there is a simpler way to do it. There is:-Please avoid long lines inside code tags. I shall try to correct your code so you can see how to break the lines. And get rid of the empty lines. You should have some empty lines but you have too many.

    Also only comment what is not already obvious. The comment in line 13 does not tell us anything we don't know already. I shan't correct that.
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is what your code looks like with most of the formatting errors corrected:Don't write CM^3; write cm\u00b3. I think that is correct for cubed.

    You can write volume because it is a valid name of a method but method calls have to be followed by (...). So you need to pass radius and height as numbers to that method, and it will return a result. I corrected that in your code.
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I hve also changed the type of imports. You should usually avoid * in ordinary imports.
     
    Robin Al-salehy
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hmm I guess I will go back to try - catch. Why I used JOptionPane is because that i was asked to do so by my teacher, he also asked to write the method that calculated the cylinder in a different method. Otherwise I would have been done long time ago.

    I have read about the Scanner in the books, sounds complicated to use, but I guess its the new choice and the better version?

    I have a lot of white space because I have sometimes hard time focusing and get lost in the code. But I always fix it after I'm done.

    Thank you for making my code smaller by the way! it's easier to read when the lines are shorter.

    I guess the only thing left is to make the Scanner work! I'll be back to show you how my code looks like! =)
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Option Pane is very old‑fashioned, but if you were told to use it do that, you will have to do that.
    Your volume method was the best part of what you posted. The only change I would suggest was using radius * radius rather than Math.power.

    If you only want a few entries, you can find an example in the Scanner documentation. Why not use doubles for the input?
    There is a much better way to use Scanners, in my opinion, but you would have to write a special utility class to encapsulate the Scanner.
     
    Robin Al-salehy
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    FINISHED!


    How does it look?! =D

    Do I need to improve something? =D

     
    Paweł Baczyński
    Bartender
    Posts: 2087
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would use a parse method instead of creating Scanners to get values of radius and height.

    Your main method is recursive. If an user retries the calculation many times they can suffer from stack overflow. I would use a loop.

    I would not use JOptionPane.ERROR_MESSAGE for a goodbye message.
     
    Robin Al-salehy
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Like this?


     
    Knute Snortum
    Sheriff
    Posts: 4287
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I can get an error by not entering anything in the Input Dialogues.

    I can get an error by entering letters in the Input Dialogues.

    If I hit Cancel on Input Radius, I still have to enter Input Height.

    I can enter negative numbers for radius and height.

    I'd probably use Double.parseDouble() to change a string into a double.
     
    Robin Al-salehy
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting point of view on the program you got there!

    I've got a lot of work to do, thank you!
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Robin Al-salehy
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey I can't seem to find the if statement logic for " if user writes letters it will output and error."

    Anyone who can direct me? =)

     
    Knute Snortum
    Sheriff
    Posts: 4287
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey I can't seem to find the if statement logic for " if user writes letters it will output and error."


    Tyr a "try/catch" block.
     
    Robin Al-salehy
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried but somehow I think I got it wrong somewhere. And I also got informed that using try-catch might overload the program or whatever it was. :S

    My "else" is getting unexpected error. but if I delete one "else if" then my else works. I don't know why it happens.
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Robin Al-salehy wrote: . . . I also got informed that using try-catch might overload the program or whatever it was.
    That sounds like incorrect advice. Please explain more fully.
    My "else" is getting unexpected error. but if I delete one "else if" then my else works. I don't know why it happens.
    Please explain fully, otherwise we cannot help.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!