Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Mystery Question

Options
  • 21-09-2013 11:13am
    #1
    Registered Users Posts: 2,369 ✭✭✭


    Hey there I'm having trouble finding out what this question is asking me.

    Q. Suppose the input for the following program is 20 then 15. What is the final line of output from the program?

    import java.util.Scanner;
    class Mystery{
    public static void main(String args[]){

    //declare object(s)
    Scanner keyboard;

    //create object(s)
    keyboard = new Scanner(System.in);

    //declare contstant(s)
    final double X = 20.5;
    final int NUM = 10;

    //declare variable(s)
    int a, b;
    double z;

    //Get Input
    System.out.println("Enter the first integer:");
    a = keyboard.nextInt();

    System.out.println("Enter the second integer:");
    b = keyboard.nextInt();

    //process
    z = X + 2 * a - b;

    //output
    System.out.println("z = "+z);

    }
    }


Comments

  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    You're basically reading in two inputs, 20 is assigned to a and 15 is assigned to b. The output from the program is z which is a sum that includes a and b.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    I think the essence of the question is to be identify how and where the variables are being set (declared in the code and captured during run time), and to be able to assemble them to solve:
    z = X + 2 * a - b;
    And using the correct order of operations here, just to make sure you're paying attention.

    I guess it depends on what level of Java you're at. It could be this simple, or it could be a trick question involving some obscure, non-obvious behaviour. It doesn't look like one of those advanced mind-fuck questions, but I don't know Java well enough to say.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,086 Mod ✭✭✭✭Tar.Aldarion


    This is as straight forward as it seems.

    They are asking you to calculate z based on numbers you possess. Use BOMDAS for correct ordering adn come back to us with an answer.


  • Registered Users Posts: 2,369 ✭✭✭LostBoy101


    This is as straight forward as it seems.

    They are asking you to calculate z based on numbers you possess. Use BOMDAS for correct ordering adn come back to us with an answer.
    Ok, I got answer 112.5 from using integer a as 20 and integer b as 15. However according to my college quiz it said I had the wrong answer but I will bring it up to my lecturer and sort it out.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    You got there by doing:
    z = (20.5 + 2) * (20 - 15)
    If you were to calculate it using the right order of operations, it would have been:
    z = 20.5 + (2 * 20) - 15
    Giving 45.5
    It's a math lesson, but it happens a lot in code. I prefer to write such things completely unambiguously by bracketing the crap out of everything in the first place.


  • Advertisement
  • Registered Users Posts: 2,369 ✭✭✭LostBoy101


    You got there by doing:
    z = (20.5 + 2) * (20 - 15)
    If you were to calculate it using the right order of operations, it would have been:
    z = 20.5 + (2 * 20) - 15
    Giving 45.5
    It's a math lesson, but it happens a lot in code. I prefer to write such things completely unambiguously by bracketing the crap out of everything in the first place.
    Ah I see! thanks.

    However it's not the end of the world as they normally take my best 7 quizs out of 15. So lesson learnt.


Advertisement