Results 1 to 8 of 8
Thread: Java exam question help
- 08-18-2011, 09:39 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
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!
- 08-18-2011, 10:01 PM #2
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?
- 08-18-2011, 10:03 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
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.
- 08-18-2011, 10:07 PM #4
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".
- 08-18-2011, 10:08 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
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?
- 08-18-2011, 10:14 PM #6
inside the class DoWhat you can declare a main() method and inside this method you use these two lines of code
and the output will beJava Code:int[] arr = {0,2,4,6}; new Dowhat().doIt(4, arr);
y( 0, 4) is 1
y( 2, 4) is 13
y( 4, 4) is 33
y( 6, 4) is 61
- 08-18-2011, 10:23 PM #7
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
Thanks very much :)
- 08-20-2011, 07:04 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
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
-
Exam prep Question
By tonto2010 in forum New To JavaReplies: 4Last Post: 05-10-2011, 01:11 AM -
Java 6 Exam
By varsh in forum Advanced JavaReplies: 3Last Post: 04-23-2010, 06:14 PM -
Discount Java Certification Exam Vouchers
By mbrown36 in forum Reviews / AdvertisingReplies: 17Last Post: 12-15-2009, 06:10 PM -
Sun Java Exam Vouchers at 40% Discount! Validity-10th Nov
By sudeepa@whizlabs.com in forum Reviews / AdvertisingReplies: 1Last Post: 07-14-2009, 01:10 PM -
Java cert exam samples
By orchid in forum New To JavaReplies: 5Last Post: 06-04-2007, 09:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks