Private void word systemoutprintln 6 marks version

This preview shows page 20 - 25 out of 25 pages.

private void printWithBlanks(String word){ System.out.println(); } (6 marks)
Image of page 20

Subscribe to view the full document.

VERSION 00000003 - 21 - COMPSCI 101 Question/Answer Sheet ID ……….………… CONTINUED c) Complete the isTooLong() method which is passed a String parameter, phrase , and an int parameter, numberAllowed . The method returns true if the String parameter contains more characters than the number allowed (given by the parameter, numberAllowed ), otherwise the method returns false . For example, the following code: boolean result = isTooLong("GARBLED", 9); System.out.println(result); System.out.println(isTooLong("WHOOPEE DO", 9)); System.out.println(isTooLong("HAPPINESS", 9)); would print: false true false private boolean isTooLong(String phrase, int numberAllowed) { } (6 marks)
Image of page 21
VERSION 00000003 - 22 - COMPSCI 101 Question/Answer Sheet ID ……….………… CONTINUED Question 24 [13 marks] a) Consider the following Java code: String s = "I like loops"; int i = 0; while (i < s.length()) { System.out.println(s.substring(i)); i++; } System.out.println("I looped " + i + " times"); Rewrite this code using a for -loop (the first and last lines of code have already been provided for you). String s = "I like loops"; System.out.println("I looped " + i + " times"); (6 marks)
Image of page 22

Subscribe to view the full document.