Results 1 to 8 of 8
  1. #1
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Java exam question help

    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)
    y(0,4) is 1
    y(2,4) is 13
    y(4,4) is 33
    y(6,4) 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!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    1. first the class code and method code and/or block code must be between curly brackets, so your class shoud be

    class Dowhat {
    public static void doIt(int b, int[] array) {
    // other code
    } // end of method doit
    } // end of class

    2. Each line of code should end with a semicolon or a curly brace if a block of code follows.

    Cosidering 1. and 2. are you able to correct your code that compiles and runs without errors?

  3. #3
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    Yeah i was just typing that in a hurry so left out the curly brackets.Its a theory based exam so id be writing the code on paper.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Ok, the loop, the calculations and the printf statements seem to be ok but the code will not compile because of "Syntax error, insert "}" to complete Block" or "Syntax error, insert ";" to complete Statement".

  5. #5
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    We were given the code as a question in the exam and then asked "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." . Any idea how to do this?

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    inside the class DoWhat you can declare a main() method and inside this method you use these two lines of code

    Java Code:
    		int[] arr = {0,2,4,6};
    		new Dowhat().doIt(4, arr);
    and the output will be

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

  7. #7
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks very much :)

  8. #8
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    What does the following code do where max is the class Math method which returns the greater of two int values?
    Give an example of the code to call the doIt method with an int array containing the elements 4,5,8,9,10 and the argument for m is 8
    complete the actual output produced
    class Dowhat{
    public static void doIt(int array[],int m){

    for(int n:array){
    int f=Math.max(n,m)/2+1;
    System.out.printf("f(%3d,%3d) is &4d\n",n,m,f);
    } }
    }


    Ok i know the code gets the greater of the two values,divides the answer by 2 and adds 1
    den outputs the the result of every calculation in the form(first one) f(4,8) is 5

    When I ran the code using netbeans the output for the fourth calculation was f(9,8) is 5.I know int cant take floating point numbers so did it just round 5.5 off to 5?
    Just wondering as well would an example of the code to call doIt method just be

    int array[]={4,5,8,9,10};
    DoWhat.do.it[array,8];

Similar Threads

  1. Exam prep Question
    By tonto2010 in forum New To Java
    Replies: 4
    Last Post: 05-10-2011, 01:11 AM
  2. Java 6 Exam
    By varsh in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2010, 06:14 PM
  3. Discount Java Certification Exam Vouchers
    By mbrown36 in forum Reviews / Advertising
    Replies: 17
    Last Post: 12-15-2009, 06:10 PM
  4. Sun Java Exam Vouchers at 40% Discount! Validity-10th Nov
    By sudeepa@whizlabs.com in forum Reviews / Advertising
    Replies: 1
    Last Post: 07-14-2009, 01:10 PM
  5. Java cert exam samples
    By orchid in forum New To Java
    Replies: 5
    Last Post: 06-04-2007, 09:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •