Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ACS-1903 Test Your name: _______________________________________ Your student number: _______________________________ Instructions: It is advised that you indent code within whiles, fors, and ifs. Additional paper is available on request. Each question is worth 10 marks. Do questions 1, 2, 3, and 4, and do one of questions 5 or 6. Computers, phones, calculators, etc. are not permitted. Hand in the test paper and any additional paper used. The next page lists a number of useful Java methods Oct 18, 2016 ACS-1903 Class Test Method Brief explanation String a = "HELLO"; String toLowerCase() equals() charAt() length() Oct 18, 2016 Example String b = "hello"; Converts all of the characters in this String to lower case Determine if two strings are the same or not. Returns a boolean value. Returns the character at a specific position within a string. Returns the length of a string. a.toLowerCase() returns "hello" a.equals(b) returns false a.charAt(3) returns āLā a.length() returns 5 Create a scanner for System.in: Scanner kb = new Scanner(System.in); Create a scanner for a String, say line: Scanner s = new Scanner(line); kb.hasNext() Scanner hasNext() Returns true if the scanner object has another token; returns false otherwise. kb.next() next() Returns the next token from the scanner as a String value. kb.nextInt() nextInt () Returns the next token from the scanner as an int value. kb.nextDouble() nextDouble () Returns the next token from the scanner as a double value. Create a random number generator: Random r = new Random(); Random nextInt(j) Returns a random integer r.nextInt(6) returns an between 0 (inclusive) and integer value in the range 0 to 5 j (exclusive) nextBoolean() Returns a random r.nextBoolean() returns boolean value either true or false. The Integer class provides utility methods Integer parseInt( s ) Returns the int value corresponding to the string argument s Integer.parseInt(ā123ā) returns the int value 123 ACS-1903 1. Test What is the output from: public class Program1{ public static void main(String args[]){ System.out.println( "5 + 5 + 5" ); System.out.println( 33 % 5 - 33 % 5 ); System.out.println( 33 + 5 - 33 % 5 ); System.out.println( 33 / 10 / 10 ); int k = 5; int sum = 0; for (int i=0; i<k; i++){ sum = sum + i; } System.out.println(sum); int count = 0; for (int r=1; r<5; r++){ for (int s=1; s<r; s++){ count++; } } System.out.println( count); int m = 9753; sum = 0; while (m > 0) { sum = sum + m % 10; m = m / 10; } System.out.println(sum); System.out.println( 14.0 - 14 / 10 * 10 ); System.out.println( "sum is "+5+5+5 ); System.out.println( 5+5+5+" is sum" ); } } Oct 18, 2016 ACS-1903 2. Test Oct 18, 2016 Complete the following program so it calculates and displays the sum of even numbers, the number of even numbers, and the average of the even numbers. The program must get integers from the user until end-of-file occurs. For instance if the input provided by the user was 10 6 3 4 2 then the program would display total=22 number of even values=4 average=5.5 import java.util.Scanner; /** * This program prompts the user for integers until end-of-file */ public class Program2 { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter integers (at least one) and then " +"on a new line signal end-of-file"); ACS-1903 3. Test Oct 18, 2016 Complete the following program so it displays a table showing the conversion of New Zealand Dollars to Australian Dollars where New Zealand Dollars vary from 0 to 1,000 in increments of 10. Your program must use a loop. If the conversion factor from New Zealand Dollars to Australian Dollars is 0.936 then the first few rows of output would be: NZ Aus 0 0.0 10 9.36 20 18.72 import java.util.Scanner; public class Program3 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double conversionFactor; // get conversion factor from user System.out.println("\nEnter conversion factor:"); ACS-1903 4. Test Oct 18, 2016 The following program was discussed in class. The program prompts the user for a mark between 0 and 100 and then displays the grade to be assigned. For instance, if the user entered 78 then the program displays B+. Modify the program to do essentially the opposite: the user will provide a grade and the program will display the corresponding mark range. For instance, if the user enters B+ then the program displays 75-79. public class Program4 { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a mark:"); int mark = kb.nextInt(); String grade = "??"; if (mark >= 90) grade = "A+"; else if (mark >= 85) grade = "A"; else if (mark >= 80) grade = "A-"; else if (mark >= 75) grade = "B+"; else if (mark >= 70) grade = "B"; else if (mark >= 65) grade = "C+"; else if (mark >= 60) grade = "C"; else if (mark >= 50) grade = "D"; else grade = "F"; System.out.println("You entered a mark of "+mark +" so grade is "+grade); } } ACS-1903 Test Oct 18, 2016 DO ONE OF QUESTION 5 OR QUESTION 6 5. The following program is a sample solution to a question in Lab 4. public class Program6 { public static void main(String[] args){ // get a text string from user String text = JOptionPane.showInputDialog( "Enter some text with no embedded spaces"); // count occurrences of vowels int count=0; for (int i=0; i<text.length(); i++){ char c= text.charAt(i); if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') count++; } // show user how many vowels appear in the text JOptionPane.showMessageDialog(null, text +" contains "+count+" vowels"); } } Modify the program where now the user is expected to enter a comma-separated list of words, and the program will display the number of words entered. For instance, if the user entered: A,dog,a,pant,a,panic,in,a,Patna,pagoda then the program displays: number of words is 10 ACS-1903 Test Oct 18, 2016 DO ONE OF QUESTION 5 OR QUESTION 6 6. Complete the following program that uses a random number generator to simulate tossing a coin. When a coin is tossed it can land heads, or it can land tails. Complete the program so it displays the toss and the number of the toss. The program must continue tossing the coin until a head is thrown. An example of output where, by chance, a head appeared on the 4th toss: Toss tail tail tail head Toss number 1 2 3 4 import java.util.Random; public class Program6 { public static void main(String[] args){ Random coin = new Random();