Problem with a program that is suppose to compute mas

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    Problem with a program that is suppose to compute mas

    I am new to Java and am currently taking a class where one of our assignments is to compute the mass of an aluminum block. We are currently on week 2 and only into our second chapter. I am completely lost! I have followed every example from the book I can find and I am getting errors all over the place. If anyone could help I would be extremely greatful! Thanks!

    Here is what I have so far!

    public class W3Ex33 {

    //main(): application entry point
    public static void main(String[] args) {
    int density = 2.7;
    int volume = length * Width * height;
    int mass = density * volume;
    int length
    int width
    int height

    Scanner stdin = new Scanner(System. in); //set up input string

    System.out.prin t("Length is: "); //read int length
    double length = stdin.nextInt() ;

    System.out.prin t("Width is: "); //read int width
    double width = stdin.nextInt() ;

    System.out.prin t("Height is: "); //read int height
    doublt height = stdin.nextInt() ;

    double volume = length * Width * height; //convert to metric equivalents

    double mass = density * volume; //perform mass calculation

    System.out.prin tln("An aluminum block with:"); //display results
    System.out.prin tln(" length " + length);
    System.out.prin tln(" width " + width);
    System.out.prin tln(" height " + height);
    System.out.prin tln("has a Mass of " + mass);

    } //End method main

    } // end class W3Ex33



    Once again, if anyone can help! Thanks so much!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    In the code below

    Code:
     
    int volume = length * Width * height;
    int mass = density * volume;
    int length
    int width
    int height
    You use length, Width and height before defining them. When you do define them, the statements are missing the ; at the end. Go through the rest of your code and check for any similar mistakes.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      @OP: did you read what the compiler was trying to tell you? Compiler diagnostics
      are extremey useful if you know how to read them. From a quick scan:

      1) width and Width are two separate words/identifiers;
      2) no semicolons after the definition of a few variables;
      3) use before set errors for a few variables;
      4) scanning ints and assigning them to doubles.

      First follow up the advice the compiler tried to give to you.

      kind regards,

      Jos

      Comment

      • AZRebelCowgirl73
        New Member
        • Nov 2006
        • 47

        #4
        Here are all the corrections I could personally find and the results left over:
        I still get 7 errors:

        public class W3Ex33 {

        //main(): application entry point
        public static void main(String[] args) {
        double length;
        double width;
        double height;
        double density = 2.7;
        double volume = length * width * height;
        double mass = density * volume;


        Scanner stdin = new Scanner(System. in); //set up input string

        System.out.prin t("Length is: "); //read int length
        double length = stdin.nextInt() ;

        System.out.prin t("Width is: "); //read int width
        double width = stdin.nextInt() ;

        System.out.prin t("Height is: "); //read int height
        double height = stdin.nextInt() ;

        double volume = length * width * height; //convert to metric equivalents

        double mass = density * volume; //perform mass calculation

        System.out.prin tln("An aluminum block with:"); //display results
        System.out.prin tln(" length " + length);
        System.out.prin tln(" width " + width);
        System.out.prin tln(" height " + height);
        System.out.prin tln("has a Mass of " + mass);

        } //End method main

        } // end class W3Ex33





        The Errors I get are as follows:

        From the Command Prompt Window:

        C:\W3Ex33>javac W3Ex33.java
        W3Ex33.java:31: cannot find symbol
        symbol : class Scanner ( I get this error message twice for line to set up input strings.

        then for each input I am getting a length width height volume and mass are already defined in main(java.lang. String[])

        Thanks For the HELP!

        ALI

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          When you are reading into the variables, you say something like

          Code:
          double length = stdin.nextInt();
          which is trying to declare a new variable called length. You should say

          Code:
          length = stdin.nextInt();
          to assign a value to the alread-assigned variabled named length. You do the same thing when initializing volume and mass.

          Alternatively, you can get rid of the variable declarations at the beginning of main() except for density.

          Comment

          • AZRebelCowgirl73
            New Member
            • Nov 2006
            • 47

            #6
            public class W3Ex33 {

            //main(): application entry point
            public static void main(String[] args) {
            double length;
            double width;
            double height;
            double density = 2.7;
            double volume = length * width * height;
            double mass = density * volume;


            Scanner stdin = new Scanner(System. in); //set up input string

            System.out.prin t("Length is: "); //read int length
            length = stdin.nextInt() ;

            System.out.prin t("Width is: "); //read int width
            width = stdin.nextInt() ;

            System.out.prin t("Height is: "); //read int height
            height = stdin.nextInt() ;

            volume = length * width * height; //convert to metric equivalents

            mass = density * volume; //perform mass calculation

            System.out.prin tln("An aluminum block with:"); //display results
            System.out.prin tln(" length " + length);
            System.out.prin tln(" width " + width);
            System.out.prin tln(" height " + height);
            System.out.prin tln("has a Mass of " + mass);

            } //End method main

            } // end class W3Ex33





            The Errors I get are as follows: 2 errors left!

            From the Command Prompt Window:

            C:\W3Ex33>javac W3Ex33.java
            W3Ex33.java:31: cannot find symbol
            symbol : class Scanner ( I get this error message twice for line to set up input strings.
            They are in reference to the word Scanner twice on that line!

            Thanks again! ALI

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by AZRebelCowgirl7 3
              W3Ex33.java:31: cannot find symbol
              symbol : class Scanner ( I get this error message twice for line to set up input strings.
              They are in reference to the word Scanner twice on that line!

              Thanks again! ALI
              The Scanner class is defined in the "java.util" package so you should import it.
              Only the classes defined in "java.lang" don't need an explicit import statement.

              At the top of your source file you should add a line:
              Code:
              import java.util.Scanner;
              ... see what happens then.

              kind regards,

              Jos

              Comment

              • AZRebelCowgirl73
                New Member
                • Nov 2006
                • 47

                #8
                I guess when they mean trial and error, atleast in my case they definately mean the error part. now I am getting a completely new message:

                Here is my code:

                import java.util.Scann er;
                public class W3Ex33 {

                //main(): application entry point
                public static void main(String[] args) {
                double length;
                double width;
                double height;
                double density = 2.7;
                double volume = length * width * height;
                double mass = density * volume;


                Scanner stdin = new Scanner(System. in); //set up input string

                System.out.prin t("Length is: "); //read int length
                length = stdin.nextInt() ;

                System.out.prin t("Width is: "); //read int width
                width = stdin.nextInt() ;

                System.out.prin t("Height is: "); //read int height
                height = stdin.nextInt() ;

                volume = length * width * height; //convert to metric equivalents

                mass = density * volume; //perform mass calculation

                System.out.prin tln("An aluminum block with:"); //display results
                System.out.prin tln(" length " + length);
                System.out.prin tln(" width " + width);
                System.out.prin tln(" height " + height);
                System.out.prin tln("has a Mass of " + mass);

                } //End method main

                } // end class W3Ex33



                Here is the error message I am getting:

                I get it in three places on the same line:

                W3Ex33.java:27: variable length might not have been initialized;
                double volume = length * width * height;

                W3Ex33.java:27: variable width might not have been initialized;
                double volume = length * width * height;

                W3Ex33.java:27: variable height might not have been initialized;
                double volume = length * width * height;

                Thanks Again! ALI

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by AZRebelCowgirl7 3
                  IHere is the error message I am getting:

                  I get it in three places on the same line:

                  W3Ex33.java:27: variable length might not have been initialized;
                  double volume = length * width * height;

                  W3Ex33.java:27: variable width might not have been initialized;
                  double volume = length * width * height;

                  W3Ex33.java:27: variable height might not have been initialized;
                  double volume = length * width * height;

                  Thanks Again! ALI
                  Yup, that's what you get if you're trying to use variables before they've got a
                  sensible value. Don't try to multiply those three variables until after they've
                  received a sensible value, i.e. after you've scanned the values for them

                  kind regards,

                  Jos

                  Comment

                  • AZRebelCowgirl73
                    New Member
                    • Nov 2006
                    • 47

                    #10
                    Originally posted by JosAH
                    Yup, that's what you get if you're trying to use variables before they've got a
                    sensible value. Don't try to multiply those three variables until after they've
                    received a sensible value, i.e. after you've scanned the values for them

                    kind regards,

                    Jos

                    Well what you just wrote was like korean to me! However, i think I have figured it out, you are a genius and a saint, and I thank you from the bottom of my frazzled heart. I just hope I can get things alittle easier as the semester progresses and once again! Thanks A Million!

                    ALI :P

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by AZRebelCowgirl7 3
                      Well what you just wrote was like korean to me! However, i think I have figured it out, you are a genius and a saint, and I thank you from the bottom of my frazzled heart. I just hope I can get things alittle easier as the semester progresses and once again! Thanks A Million!

                      ALI :P
                      It's not as complicated as it seems; this was your code:
                      Code:
                      double length;
                      double width;
                      double height;
                      double volume= length*width*heigth;
                      Local variables don't have a value before you explicitly give them one, either
                      by initialization or by assignment. The top three local variables don't have a
                      value. Then all of a sudden you want to initialize variable 'volume' with the
                      value of the product of the other three variables. What's their value then?

                      The compiler is smart enough to notice that and starts to complain about it.
                      You don't know their values either because the user hasn't supplied values
                      for them yet. That only happens later in your program when you use your
                      Scanner object. Only then, after all three variables have a value assigned
                      you can calculate your formula length*width*he ight. Programming is not
                      about voodoo and psychic events you know, computers are as stupid as they
                      can be.

                      kind regards,

                      Jos

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        Originally posted by JosAH
                        ...computers are as stupid as they can be.
                        In fact, they are so stupid, they can only do what we tell them to do.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by AZRebelCowgirl7 3
                          ... you are a genius and a saint...
                          Jos? Genius and saint?

                          Wait 'till he puts his nitpicking armour on.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by r035198x
                            Jos? Genius and saint?

                            Wait 'till he puts his nitpicking armour on.
                            Small correction: my shiny nitpicking armour ;-)

                            kind regards,

                            Jos

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by JosAH
                              Small correction: my shiny nitpicking armour ;-)

                              kind regards,

                              Jos
                              Exactly what I was talking about.

                              Comment

                              Working...