Calculations using different data types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlgraham
    New Member
    • Jun 2008
    • 10

    Calculations using different data types

    I have an assignment that wants me to write an application that takes 5 numbers from a user. As the user inputs the numbers I have to make them into a double, float, long, int and byte. All I need after this is to output the result of the following calculation and store the result in an int variable.
    Calculation to do
    Subtract the floatValue from the intValue, then multiply by the doubleValue, then divide by the longValue, then multiply by the byteValue and then divide by 2. I am to store the result in an int variable. I can get the user input but am at a loss as to how to do the calculation and store the result in an int variable. Can someone out in cyberworld help? Thanks to anyone who responds.

    CODE
    import java.util.*;


    public class TestNumbers
    {
    public static void main(String args[])
    {

    Scanner input = new Scanner(System. in);

    int result;

    System.out.prin tln("Enter a number:");
    double num1 = input.nextDoubl e();


    System.out.prin tln();
    System.out.prin tln("Enter second number:");
    float num2 = input.nextFloat ();


    System.out.prin tln();
    System.out.prin tln("Enter third number:");
    long num3 = input.nextLong( );


    System.out.prin tln();
    System.out.prin tln("Enter fourth number:");
    int num4 = input.nextInt() ;


    System.out.prin tln();
    System.out.prin tln("Enter fifth number:");
    byte num5 = input.nextByte( );


    System.out.prin tln();

    }

    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by jlgraham
    Subtract the floatValue from the intValue, then multiply by the doubleValue, then divide by the longValue, then multiply by the byteValue and then divide by 2. I am to store the result in an int variable. I can get the user input but am at a loss as to how to do the calculation and store the result in an int variable.
    That assignment text almost reads like COBOL; what is the problem? Assume
    the variables are fv, iv, dv, lv and bv. The formula reads as:

    (iv-fv)*dv/lv*bv/2

    The result needs to be an int again so you have to cast. In Java that reads:

    [code=java]
    int result= (int)((iv-fv)*dv/lv*bv/2);
    [/code]

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      What you have to do is spelled out in detail, no?

      1. Subtract the floatValue from the intValue,
      2. then multiply by the doubleValue,
      3. then divide by the longValue,
      4. then multiply by the byteValue and
      5. then divide by 2.
      6. I am to store the result in an int variable.

      My advice is to try to do this and post it if you still have questions.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by BigDaddyLH
        What you have to do is spelled out in detail, no?
        Admit it: you programmed in COBOL too didn't you?

        kind regards,

        Jos ;-)

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          [CODE=java]import java.util.*;

          public class TestNumbers
          {
          public static void main(String args[])
          {

          Scanner input = new Scanner(System. in);

          int result;

          System.out.prin tln("Enter a number:");
          double num1 = input.nextDoubl e();

          System.out.prin tln();
          System.out.prin tln("Enter second number:");
          float num2 = input.nextFloat ();

          System.out.prin tln();
          System.out.prin tln("Enter third number:");
          long num3 = input.nextLong( );

          System.out.prin tln();
          System.out.prin tln("Enter fourth number:");
          int num4 = input.nextInt() ;

          System.out.prin tln();
          System.out.prin tln("Enter fifth number:");
          byte num5 = input.nextByte( );

          System.out.prin tln();
          }
          }[/CODE]Hi jlgraham!
          Calculations in Java aren't difficult at all. There are a lot of automatic conversions. Just make sure, you don't loose precision.
          The data types you named are sorted as follows: (top being the most precise)
          1. double
          2. float
          3. long
          4. int
          5. byte
          For example:
          Originally posted by jlgraham
          Subtract the floatValue from the intValue
          [code=java]float res1=num4-num2;[/code]Of course, you can use double from the beginning. You should be able to figure the rest of the calculations out yourself.

          To save the result as an integer, use
          Code:
          int result = (int) res5;
          Greetings,
          Nepomuk

          Comment

          • jlgraham
            New Member
            • Jun 2008
            • 10

            #6
            Originally posted by JosAH
            That assignment text almost reads like COBOL; what is the problem? Assume
            the variables are fv, iv, dv, lv and bv. The formula reads as:

            (iv-fv)*dv/lv*bv/2

            The result needs to be an int again so you have to cast. In Java that reads:

            [code=java]
            int result= (int)((iv-fv)*dv/lv*bv/2);
            [/code]

            kind regards,

            Jos
            I tried this and it still will not work. I am posting the code and I still get errors. Is it because I am using the Scanner and determining the data types as the user enters them? It will not allow me to cast the result to an integer. Please help
            [CODE]
            import java.util.*;


            public class TestNumbers
            {
            public static void main(String args[])
            {

            Scanner input = new Scanner(System. in);

            int result;

            System.out.prin tln("Enter a number:");
            double num1 = input.nextDoubl e();

            System.out.prin tln();
            System.out.prin tln("Enter second number:");
            float num2 = input.nextFloat ();

            System.out.prin tln();
            System.out.prin tln("Enter third number:");
            long num3 = input.nextLong( );

            System.out.prin tln();
            System.out.prin tln("Enter fourth number:");
            int num4 = input.nextInt() ;

            System.out.prin tln();
            System.out.prin tln("Enter fifth number:");
            byte num5 = input.nextByte( );

            int result= (int)((num2 - num4) * numl / num3 * num5 / 2);

            System.out.prin tln("Result is: " + result);

            }

            }
            [CODE]

            Comment

            • jlgraham
              New Member
              • Jun 2008
              • 10

              #7
              Originally posted by nepomuk
              [CODE=java]import java.util.*;

              public class TestNumbers
              {
              public static void main(String args[])
              {

              Scanner input = new Scanner(System. in);

              int result;

              System.out.prin tln("Enter a number:");
              double num1 = input.nextDoubl e();

              System.out.prin tln();
              System.out.prin tln("Enter second number:");
              float num2 = input.nextFloat ();

              System.out.prin tln();
              System.out.prin tln("Enter third number:");
              long num3 = input.nextLong( );

              System.out.prin tln();
              System.out.prin tln("Enter fourth number:");
              int num4 = input.nextInt() ;

              System.out.prin tln();
              System.out.prin tln("Enter fifth number:");
              byte num5 = input.nextByte( );

              System.out.prin tln();
              }
              }[/CODE]Hi jlgraham!
              Calculations in Java aren't difficult at all. There are a lot of automatic conversions. Just make sure, you don't loose precision.
              The data types you named are sorted as follows: (top being the most precise)
              1. double
              2. float
              3. long
              4. int
              5. byte
              For example:[code=java]float res1=num4-num2;[/code]Of course, you can use double from the beginning. You should be able to figure the rest of the calculations out yourself.

              To save the result as an integer, use
              Code:
              int result = (int) res5;
              Greetings,
              Nepomuk
              Thank you . It worked doing one calculation at a time.

              Comment

              • jlgraham
                New Member
                • Jun 2008
                • 10

                #8
                Originally posted by JosAH
                Admit it: you programmed in COBOL too didn't you?

                kind regards,

                Jos ;-)
                I am 63 and I have never seen a programming code until I signed up for Java 1. I find it very difficult and sure have a lot of respect for you youngsters and how much you know and can do.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by jlgraham
                  I am 63 and I have never seen a programming code until I signed up for Java 1. I find it very difficult and sure have a lot of respect for you youngsters and how much you know and can do.
                  Well, I'm 51 which is a whoooole lot younger than you are of course but I'm
                  not exactly a youngster anymore ;-)

                  I appreciate it that you're taking up Java programming, I started with this
                  mysery (not Java) when I was 18 and computers were made of wood; my compliments.

                  kind regards,

                  Jos

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    Originally posted by JosAH
                    Well, I'm 51 which is a whoooole lot younger than you are of course but I'm
                    not exactly a youngster anymore ;-)

                    I appreciate it that you're taking up Java programming, I started with this
                    mysery (not Java) when I was 18 and computers were made of wood; my compliments.

                    kind regards,

                    Jos
                    Wood!?1 Didn't you bash little holes into stone tablets and then give them to an elephant to compile together?

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by RedSon
                      Wood!?1 Didn't you bash little holes into stone tablets and then give them to an elephant to compile together?
                      Elephant Compiler? Luxury!

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #12
                        Originally posted by BigDaddyLH
                        Elephant Compiler? Luxury!
                        I suppose you had a dozen monkeys with abacuses?

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by RedSon
                          I suppose you had a dozen monkeys with abacuses?
                          Abacuses? Poofter, we used the knuckles of our diseased sibblings to do our
                          calculations on the bottom of our lake where we lived; if our dad wouldn't beat
                          us; as he did every night; we had it rough ...

                          kind regards,

                          Jos ;-)

                          Comment

                          Working...