Beginner -- Applet problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jcnews

    #1

    Beginner -- Applet problems

    I am a beginner programmer just starting to learn Java. Can someone tell me
    what is wrong with this applet?
    The output is:
    Circumference is 47.1225
    Area is 176.70938
    Volume is 1767.0938

    I don't think that is the correct value of the volume because it is exactly
    10 times the area.

    What went wrong and also, is there an easier way to square or cube a number?

    Thanks,
    Josh



    //Calculates circumference, area of circle and volume of sphere based on a
    radius

    import java.awt.*;
    import java.applet.App let;

    public class Calculations extends Applet {
    public void paint(Graphics g) {
    float pi = 3.1415f;
    float radius = 7.5f;
    float circumference = 2.0f * pi * radius;
    float area = pi * (radius * radius);
    float volume = 4.0f * pi * (radius * radius * radius) / 3;


    g.drawString("C ircumference is: " + circumference, 50, 50);
    g.drawString("A rea is: " + area, 50, 75);
    g.drawString("V olume is: " + volume, 50, 100);
    }
    }





  • Murray

    #2
    Re: Beginner -- Applet problems


    "jcnews" <jcnews@earthli nk.net> wrote in message
    news:FEzid.1784 8$KJ6.13441@new sread1.news.pas .earthlink.net. ..[color=blue]
    >I am a beginner programmer just starting to learn Java. Can someone tell
    >me
    > what is wrong with this applet?
    > The output is:
    > Circumference is 47.1225
    > Area is 176.70938
    > Volume is 1767.0938
    >
    > I don't think that is the correct value of the volume because it is
    > exactly
    > 10 times the area.
    >
    > What went wrong and also, is there an easier way to square or cube a
    > number?
    >
    > Thanks,
    > Josh
    >
    >
    >
    > //Calculates circumference, area of circle and volume of sphere based on a
    > radius
    >
    > import java.awt.*;
    > import java.applet.App let;
    >
    > public class Calculations extends Applet {
    > public void paint(Graphics g) {
    > float pi = 3.1415f;
    > float radius = 7.5f;
    > float circumference = 2.0f * pi * radius;
    > float area = pi * (radius * radius);
    > float volume = 4.0f * pi * (radius * radius * radius) / 3;
    >
    >
    > g.drawString("C ircumference is: " + circumference, 50, 50);
    > g.drawString("A rea is: " + area, 50, 75);
    > g.drawString("V olume is: " + volume, 50, 100);
    > }
    > }[/color]

    If you bothered to check yourself, you'd have found the area and volume
    values are correct. 4/3 * 7.5 = 10

    java.util.Math. pow(radius, 3);


    Comment

    • jcnews

      #3
      Re: Beginner -- Applet problems


      "Murray" <parps@SMAFFoff SPAMMER.optusne t.SPAMMAGE.com. au> wrote in message
      news:v9Aid.1375 5$K7.11364@news-server.bigpond. net.au...[color=blue]
      >
      > "jcnews" <jcnews@earthli nk.net> wrote in message
      > news:FEzid.1784 8$KJ6.13441@new sread1.news.pas .earthlink.net. ..[color=green]
      > >I am a beginner programmer just starting to learn Java. Can someone tell
      > >me
      > > what is wrong with this applet?
      > > The output is:
      > > Circumference is 47.1225
      > > Area is 176.70938
      > > Volume is 1767.0938
      > >
      > > I don't think that is the correct value of the volume because it is
      > > exactly
      > > 10 times the area.
      > >
      > > What went wrong and also, is there an easier way to square or cube a
      > > number?
      > >
      > > Thanks,
      > > Josh[/color]
      >
      >
      >
      > If you bothered to check yourself, you'd have found the area and volume
      > values are correct. 4/3 * 7.5 = 10
      >
      > java.util.Math. pow(radius, 3);[/color]


      Thanks for your help. The last time I was in a math class was 14 years ago
      and I have not used any math since. I ran it through on a calculator and
      had came up with a different number so I figured there was something wrong.
      I guess it was right.

      Thanks again.




      Comment

      • Ian Shef

        #4
        Re: Beginner -- Applet problems

        "jcnews" <jcnews@earthli nk.net> wrote in
        news:FEzid.1784 8$KJ6.13441@new sread1.news.pas .earthlink.net:
        [color=blue]
        > I am a beginner programmer just starting to learn Java. Can someone
        > tell me what is wrong with this applet?[/color]
        <snip>[color=blue]
        >
        > import java.awt.*;
        > import java.applet.App let;
        >
        > public class Calculations extends Applet {
        > public void paint(Graphics g) {
        > float pi = 3.1415f;[/color]
        <snip>

        In the future, you may want to consider using java.lang.math. PI

        --
        Ian Shef 805/F6 * These are my personal opinions
        Raytheon Company * and not those of my employer.
        PO Box 11337 *
        Tucson, AZ 85734-1337 *

        Comment

        • jcnews

          #5
          Re: Beginner -- Applet problems

          "Ian Shef" <invalid@avoidi ng.spam> wrote in message
          news:Xns959878F 93203Cvaj4088ia nshef@138.126.2 54.210...[color=blue]
          > "jcnews" <jcnews@earthli nk.net> wrote in
          > news:FEzid.1784 8$KJ6.13441@new sread1.news.pas .earthlink.net:
          >[color=green]
          > > I am a beginner programmer just starting to learn Java. Can someone
          > > tell me what is wrong with this applet?[/color]
          > <snip>[color=green]
          > >
          > > import java.awt.*;
          > > import java.applet.App let;
          > >
          > > public class Calculations extends Applet {
          > > public void paint(Graphics g) {
          > > float pi = 3.1415f;[/color]
          > <snip>
          >
          > In the future, you may want to consider using java.lang.math. PI[/color]

          Ok... I have no idea what java.lang.math. PI is yet or how to use it because
          I am just a couple of days into Java, but I will look into it. Thanks.


          Comment

          Working...