ACS-1904 Review Exercises Jan 8, 2009 Complete the following code segment so that the larger of x and y is printed Scanner kb = new Scanner(System.in); x=kb.nextDouble(); y=kb.nextDouble(); // determine and display the larger of x and y System.out.println (“The larger value is “+z); Rewrite the following if-else structure as a switch statement if (category == 1) discount = 0.20 else if (category == 2) discount = 0.25 else if (category == 4) discount =0.40 else discount = 0.10; Rewrite the following if-else structure using the conditional operator ? if (category == 1) discount = 0.20 else discount = 0.25 Write the code necessary to exchange the first and last values of an array x. For example, if x = {3,5,7,3,5,8,2,2,2,0} then after your code segment finishes x = {0,5,7,3,5,8,2,2,2,3}. Complete the code segment below so that the lines of MyFile.txt are displayed with line numbers. If MyFile.txt contained the 3 lines: This is a sample file of three lines used for illustration purposes Then the output from your code segment is 1 This is a sample 2 file of three lines used for illustration 3 purposes // Open the file. FileReader fr = new FileReader("MyFile.txt"); BufferedReader bf = new BufferedReader(fr); What is the output from int sum = 0; int cc = 9025; while (cc!=0) { sum = sum+cc%10; System.out.print(sum+" "); cc = cc/10; System.out.println(cc); } 1/3 ACS-1904 Review Exercises Jan 8, 2009 Write a value-returning method, gpa(), that calculates a student’s grade point average. The method has two parallel arrays as parameters: an array of grade points, and an array of credit hours. Suppose for example a student has taken 5 courses and the two arrays are: gradePoint = {4.0, 3.5, 3.0, 4.0, 4.5} creditHours = {3, 6, 6, 3, 3}. The ith element of gradePoint is the grade point achieved for the ith course and the ith element of creditHours is the credit hours associated with the ith course. A grade point average is defined as the sum of the products of grade points and credit hours divided by the sum of the credit hours. In this case we have: (4.0*3+3.5*6+3.0*6+4.0*3+4.5*3) / (3+6+6+3+3) = 76.5/21 = 3.64 /** * main() uses gpa() to calculate a gpa */ public class StudentGpa { public void main() { // the following displays a value of 3.642857142857143 double[] gradePoint = {4.0, 3.5, 3.0, 4.0, 4.5} ; int[] creditHours = {3, 6, 6, 3, 3} ; System.out.println(gpa(gradePoint, creditHours)); } /** * gpa calculates the gpa using two parallel arrays * * @param gradePoint array of grade points * @param creditHours array of credit hours * @return the student's gpa */ public double gpa(double[] gradePoint, int[] creditHours) { // your code goes here: 2/3 ACS-1904 Review Exercises Jan 8, 2009 Write a value returning method average() that determines the average of numbers that appear in a string. For example, if the string is “1.0 3.0 4.5 7.5” then the average is (1.0 + 3.0 + 4.5 + 7.5) / 4 = 16.0/4 = 4.0 During the term we have used the Scanner class as an aid to get data from the keyboard. The Scanner class can also be used with strings, as in the following: String xyz = " LECTURES BEGIN on January 6 "; Scanner s = new Scanner(xyz); System.out.println(s.next()); // displays the token: LECTURES System.out.println(s.hasNext()); // displays: true /** * main() uses average() to calculate an average */ import java.util.Scanner; /** * Calculate the average of some numbers * */ public class Average { public static void main() { Scanner s = new Scanner(System.in); System.out.println("Enter some numbers then press enter"); String lineOfNumbers = s.nextLine(); System.out.println(average(lineOfNumbers)); } /** * average returns the average of numbers appearing in a string * @param s the string containing numbers * @return the average */ public static double average(String n) { // your code goes here: 3/3