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 question

Options
  • 18-08-2011 5:40pm
    #1
    Registered Users Posts: 603 ✭✭✭


    Just an part of an exam question for basic java having trouble with

    What does the following code do?
    (I think this is what the code does)The code puts the values 0,2,4 and 6 into the formula x*x+b*x+1 and outputs a value for y each time.
    Give an example of the code to call the doIt method where the parameter b is 4 and the array contains the elements 0,2,4,6.
    Havent a clue here
    Complete the actual output produced(use a character or space per cell)
    0*0+4*0+1 is 1
    2*2+4*2+1 is 13
    4*4+4*4+1 is 33
    6*6+4*6+1 is 61


    class Dowhat
    public static void doIt(int b, int[] array)
    for(int x: array)
    int y=x*x+b*x+1
    System.out.printf("y(%2d,%2d) is %3d\n",x,b,y);
    //end method doIt
    //end class DoWhat

    Just a bit of help needed.Am i right for the first and third question and how would you do the second question? Any help appreciated!


Comments

  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Ahem (with regard to the second question).


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    Complete the actual output produced(use a character or space per cell)
    0*0+4*0+1 is 1
    2*2+4*2+1 is 13
    4*4+4*4+1 is 33
    6*6+4*6+1 is 61
    You made an effort, so... do note the first y in
    System.out.printf("y(%2d,%2d) is %3d\n",x,b,y);
    
    Is in quotes! So it is a literal and the output will be in the form:
    y(valueof.x, valueof.b) is valueof.y


  • Registered Users Posts: 603 ✭✭✭eoins23456


    y(0,4) is 1
    y(2,4) is 13
    y(4,4) is 33
    y(6,4) is 61

    I get ya now thanks
    For the second questions I dont know where to start really
    int[] c;
    c = new int[4]
    c[0]=0
    c[1]=2
    c[2]=4
    c[3]=6

    class do what
    public static void doIt(int b,c) ?

    dont really know where to go .


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    re: q2 - how to call doit...
    It really doesn't get any simpler than this so I could not answer without breaking the rules Sparks reminded us of. I think you need to read your java notes & book again if you have one.

    But all the info you need is there in the method declaration
    public static void doIt(int b, int[] array)
    void will mean it does not expect a returned value
    and in brackets it defiint b, int[] arraynes it expects an integer and an array of integers to be passed to it when called.


Advertisement