Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: CylinderApplet

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CylinderApplet

    Hi, why doesn't this work?

    import java.awt.Container;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
     
    public class CylinderApplet extends JApplet {
     
        public void init() 
        {
     
           String radiusStr = null ;
           String heightStr = null;
     
            double volume = 0;
            double radius= Double.parseDouble(radiusStr);
            double height= Double.parseDouble(heightStr);
     
            JTextArea outputArea= new JTextArea();
     
            Container container =getContentPane();
     
            container.add(outputArea);
     
     
     
             for (int i = 1; i <= 5; i++ ){
     
            radiusStr = JOptionPane.showInputDialog("Enter in the radius here");
            heightStr  = JOptionPane.showInputDialog("Enter in the radius here");
     
            outputArea.setText("The Volume of the Cylinder is" +volume);
             }
     
        }
           public double myvolume (double radius, double height)
            {
     
            double volume;
     
             volume= Math.PI*radius*radius*height;
     
            return volume;
     
         } // end of method
    } // end of volume class

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: CylinderApplet

    Define "doesn't work." Are you getting errors, incorrect results? If so, post the specifics (error messages, sample outputs, desired outputs, etc.) and ask specific questions.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CylinderApplet

    why doesn't this work?
    Please explain what "doesn't work" means. Post something that shows the problem or explain with some details.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CylinderApplet

    Quote Originally Posted by GregBrannon View Post
    Define "doesn't work." Are you getting errors, incorrect results? If so, post the specifics (error messages, sample outputs, desired outputs, etc.) and ask specific questions.
    I'm sorry about that. The variables radius and height does not work, here are the errors from the console:
    java.lang.NullPointerException
    	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    	at java.lang.Double.parseDouble(Unknown Source)
    	at CylinderApplet.init(CylinderApplet.java:23)
    	at sun.applet.AppletPanel.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: CylinderApplet

    The variables radiusStr and heightStr are defined as null and then parsed to obtain radius and height. You have the steps out of order. Reorder them so that you're not parsing nulls.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CylinderApplet

    Okay, I changed it a bit and got the applet to start but now it returns a value of 0.0 when I type in the digits for radius and height.

    import java.awt.Container;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
     
    public class CylinderApplet extends JApplet {
     
    	public void init() 
    	{
    		String radiusStr = JOptionPane.showInputDialog("Enter in the radius here");
    		String heightStr  = JOptionPane.showInputDialog("Enter in the radius here");
     
    		double volume = 0;
    		double radius = Double.parseDouble(radiusStr);
    		double height = Double.parseDouble(heightStr);
     
     
    		JTextArea outputArea = new JTextArea();
     
    		Container container = getContentPane();
     
    		container.add(outputArea);
     
    		for (int i = 1; i <= 5; i++ ){
     
     
    			radiusStr = null;
    			heightStr = null;
     
    			outputArea.setText("The Volume of the Cylinder is" +volume);
    		}
     
    	}
    	public double myvolume (double radius, double height)
    	{
     
    		double volume;
     
    		volume= Math.PI*radius*radius*height;
     
    		return volume;
     
    	} // end of method
    } // end of volume class

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: CylinderApplet

    Where do you compute the volume using radius and height?

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CylinderApplet

    In the loop?

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: CylinderApplet

    Let me ask it another way. Where do you call the method myVolume() that computes/returns the volume based on radius and height?