You can assume that the string passed as a parameter

This preview shows page 6 - 9 out of 22 pages.

may not be chosen to appear in the code more than once. You can assume that the String passed as a parameter has more than 3 characters and contains no repeated letters. You do not have to use the methods declared in the Q4 application below, although you may find them useful. Example: If the method was passed "ABCDE" as the parameter, then it would return a set of 3 letters such as "BCA", "DAB", "EBC". The method could not return "AAB" however, since the letter A is repeated. public class Q4{ private static String remove(String s, int index){ return s.substring(0,index) + s.substring(index+1); } private static int rand(int low, int high){ return (int)( Math.random()*(high-low+1) + low); } //Write your method here: private static String uniqueCode( String s ){ String code = ""; for(int i = 0; i < 3; i++){ int j = rand( 0, s.length()-1 ); code += s.charAt(j); s.remove(s, j); } return code; } public static void main(String[] args){ String code = uniqueCode("ABCDE"); System.out.println(code); } } (5 marks)
Image of page 6

Subscribe to view the full document.

Candidate's Name:___________________ 7 COMPSCI 101 SC/ST SECTION B 5. Read the code below and answer the following questions public class Q5{ public static void main(String[] args){ int num1 = …; //see part ii) below int num2 = …; //see part ii) below if (num1>=50 && num2<30){ System.out.println("Output 1"); if (num1>50 || num2<40) { System.out.println("Output 2"); } } else { System.out.println("Output 3"); } System.out.println("Output 4"); } } i) Rewrite the if statement using correct indentation. public class Q5{ public static void main(String[] args){ int num1 = …; //see part ii) below int num2 = …; //see part ii) below if (num1>=50 && num2<30){ System.out.println("Output 1"); if (num1>50 || num2<40) { System.out.println("Output 2"); } } else { System.out.println("Output 3"); } System.out.println("Output 4"); } } ii) When I run the above application I would like the output to be: Output 3 Output 4 Assign two integer values to the variables num1 , num2 so that I obtain this output. num1 = 49 //or smaller num2 = 30 //or bigger (6 marks)
Image of page 7