Each digit should have the same probability of being

This preview shows page 8 - 14 out of 18 pages.

Each digit should have the same probability of being returned. If you complete the randomOddDigit() method correctly, the output of the above application Q5 might be similar to the following: C:\> java Q5 3 7 1 C:\> java Q5 9 9 5
Image of page 8

Subscribe to view the full document.

Question/Answer Sheet - Page 9 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 6 (6 marks) What is the output when the following application is executed? public class Q6 { public static void main(String[] args) { boolean isReady = false; int num1, num2; num1 = 20; num2 = 30; if (num1<20 && num2>15) { if(isReady) System.out.println("Line 1"); else System.out.println("Line 2"); System.out.println("Line 3"); } else if (!(num1<20 || num2>30)) { if(isReady) System.out.println("Line 4"); else System.out.println("Line 5"); System.out.println("Line 6"); } if (num1<100 || num2>30) { if(!isReady) System.out.println("Line 7"); else System.out.println("Line 8"); System.out.println("Line 9"); } System.out.println("Line 10"); } }
Image of page 9
Question/Answer Sheet - Page 10 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 7 (6 marks) Write a method, printLuckyColour() , which first generates a random integer between 0 and 6 inclusive and then prints a message to the DOS window depending on the value of the random number. If the random number is either 0 or 1 then the message printed is “ Lucky colour: green If the value of the random number is 2, 3 or 4 then the message is “ Lucky colour: red Otherwise the message is “ Lucky colour: blue ”. public static void printLuckyColour() { }
Image of page 10

Subscribe to view the full document.

Question/Answer Sheet - Page 11 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 8 (6 marks) What is the output when the following application is executed? public class Q8 { public static void main(String[] args) { boolean b1 = 6 < 10; boolean b2 = true; boolean b3 = !b1; b1 = 10 > 6 && b2; System.out.println("b1: "+b1); System.out.println("b2: "+b2); System.out.println("b3: "+b3); } }
Image of page 11
Question/Answer Sheet - Page 12 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 9 (10 marks) Complete the method, printColumn() , which prints two columns of numbers to the DOS window. The method takes one parameter, num , which is the start number for the left hand column. The number on the left of each row decreases from the start number down to 1 and the number on the right of each row increases from 1 up to the start number. In each row the numbers are separated by a dash. public static void printColumn(int num){ } For example, if the following three calls to the printColumn() method are made: printColumn(3); printColumn(5); printColumn(4); the following output is obtained: 3 – 1 2 – 2 1 – 3 5 – 1 4 - 2 3 – 3 2 – 4 1 – 5 4 - 1 3 – 2 2 – 3 1 – 4
Image of page 12

Subscribe to view the full document.

Question/Answer Sheet - Page 13 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ...........................................................
Image of page 13
Image of page 14
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, English-language films, ........., ASCII, syntax error, Logic error

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern