Results 1 to 7 of 7
  1. #1
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Need help with my java homework.

    I need declare at least one instance variable to store the radius. Show the input dialog in method init(). Show the results in method paint(Graphics g). I have to use Joption.pane to get user input on the radius of the circle.
    Then i have to use that input of radius and show the result of area,circumference. How do i do that? This is what i have so far.

    import java.awt.Graphics;

    import javax.swing.JOptionPane;

    public class Assign3
    {
    public void init()
    {

    String firstNumber = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );

    double number1 = Double.parseDouble( firstNumber );
    double diameter

    circumference = 2x( firstNumber )
    area = 2xMath.PI(firstNumber)
    }

    public void paint( Graphics g )
    {
    super.paint( g );

    g.drawRect( 40, 35, 270, 20 );

    g.drawString( "The diameter is " + firstNumber , 25, 25 );
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Does it compile?

    The Java™ Tutorials

    db

  3. #3
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    No i get this message.
    The method parseDouble(String) in the type Double is not applicable for arguments (double)

    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;

    public class Assign3 extends JApplet
    {
    private double diameter;
    private double circum;
    private double area;

    public void init()
    {
    String radius = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );

    double radius1 = Double.parseDouble( radius1 );

    double diameter;
    double circum;
    double pi = Math.PI;
    double area;

    diameter = 2*radius1;
    circum = 2*(pi*radius1);
    area = 2*pi*(radius1*radius1);
    }

    public void paint( Graphics g )
    {
    super.paint( g );

    String radius = null;
    g.drawString( "The radius is " + radius , 25, 25);
    g.drawString( "The diameter is " + diameter , 25, 35);
    g.drawString( "The circumference is " + circum , 25, 45);
    g.drawString( "The area is " + area , 25, 55);
    }
    }

  4. #4
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    No i get this message.
    The method parseDouble(String) in the type Double is not applicable for arguments (double)
    Look at the code. Why you have this :
    Java Code:
    double radius1 = Double.parseDouble( radius1 );
    the error is radius1 at parseDouble();

  5. #5
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    okay i got it to work but i have a minor error. when joption asks for the radius and puts the result out in the applet, it saids 0 even though i enter other numbers.

    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;

    public class Assign3 extends JApplet
    {
    private double diameter;
    private double circum;
    private double area;
    private double pi = Math.PI;
    private double radius;

    public void init()
    {
    String radius = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );

    double radius1 = Double.parseDouble( radius );
    radius = radius;
    diameter = 2*radius1;
    circum = 2*(pi*radius1);
    area = pi*(radius1*radius1);
    }
    public void paint( Graphics g )
    {
    super.paint( g );
    g.drawString( "The radius is " + radius , 25, 25);
    g.drawString( "The diameter is " + diameter , 25, 45);
    g.drawString( "The circumference is " + circum , 25, 65);
    g.drawString( "The area is " + area , 25, 85);
    }
    }

  6. #6
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Quote Originally Posted by nightrise420 View Post
    private double radius;
    String radius = JOptionPane.showInputDialog("Enter the radius of the circle" );
    The problem is in these declarations.

  7. #7
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    I'm a total newbie but I'll take a stab at it,

    String radius = JOptionPane.showInputDialog(

    you declared the variable in one of the methods and not in the class, therefore once you close the method you close the variable

    private String radius; to just after the public class Assign3 extends JApplet and before the symbol {

    then put
    radius = JOptionPane.showInputDialog(
    instead of
    String radius = JOptionPane.showInputDialog(

Similar Threads

  1. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 08:12 PM
  2. Having trouble with java homework...
    By purinlove88 in forum New To Java
    Replies: 3
    Last Post: 03-02-2009, 03:02 PM
  3. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 02:48 AM
  4. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 06:00 AM
  5. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •