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
14-22.1 // COMP 14, Spring 2006 // Examples of java methods (subroutines). public class MethodExamples { // No parameter greeting public static void greet() { System.out.println(" Hello "); } // One parameter greeting public static void greet(String s) { System.out.println(" Hello "+s); } // Two parameter greeting public static void greet(String s, int n) { System.out.println(" Hello "+s+", you are customer number "+n); } // Add two integers and return the sum public static int sum(int x, int y) { return x+y; } // Determine if the double number x is an integer in 0...10 public static boolean intIn0to10(double x) { if ((double)Math.round(x)!=x) return false; // not an integer. if (x<0 || x>10) return false; // out of range return true; } // Return a string containing n copies of the character c. public static String repeat(char c, int n) { String s=""; for (int i=0;i<n;i++) s=s+c; return s; } 14-22.2 public static void main(String[] args) { // Demonstrate the various versions of the greet methods. greet(); greet("Steve"); greet("Joe",50); // Demonstrate sum method. for (int i=0;i<10;i++) { System.out.println("Test number "+ i+ ", "+ 10*i+ " plus "+ 2*i+ " = "+ sum(10*i,2*i)); } // Demonstrate integer test method for (double i=-1.0;i<=11.0;i=i+.5) System.out.println("Testing "+i+", result is "+intIn0to10(i)); // Demonstrate repeat method for (int i=0;i<10;i++) System.out.println("Repeat test number "+i+" } } Ï // End of main // End of class "+repeat('$',i)); 14-22.3 ----jGRASP exec: java MethodExamples ÏÏ§Ï ÏÏ§Ï Hello ÏÏ§Ï Hello Steve ÏÏ§Ï Hello Joe, you are customer number 50 ÏϧÏTest number 0, 0 plus 0 = 0 ÏϧÏTest number 1, 10 plus 2 = 12 ÏϧÏTest number 2, 20 plus 4 = 24 ÏϧÏTest number 3, 30 plus 6 = 36 ÏϧÏTest number 4, 40 plus 8 = 48 ÏϧÏTest number 5, 50 plus 10 = 60 ÏϧÏTest number 6, 60 plus 12 = 72 ÏϧÏTest number 7, 70 plus 14 = 84 ÏϧÏTest number 8, 80 plus 16 = 96 ÏϧÏTest number 9, 90 plus 18 = 108 ÏϧÏTesting -1.0, result is false ÏϧÏTesting -0.5, result is false ÏϧÏTesting 0.0, result is true ÏϧÏTesting 0.5, result is false ÏϧÏTesting 1.0, result is true ÏϧÏTesting 1.5, result is false ÏϧÏTesting 2.0, result is true ÏϧÏTesting 2.5, result is false ÏϧÏTesting 3.0, result is true ÏϧÏTesting 3.5, result is false ÏϧÏTesting 4.0, result is true ÏϧÏTesting 4.5, result is false ÏϧÏTesting 5.0, result is true ÏϧÏTesting 5.5, result is false ÏϧÏTesting 6.0, result is true ÏϧÏTesting 6.5, result is false ÏϧÏTesting 7.0, result is true ÏϧÏTesting 7.5, result is false ÏϧÏTesting 8.0, result is true ÏϧÏTesting 8.5, result is false ÏϧÏTesting 9.0, result is true ÏϧÏTesting 9.5, result is false ÏϧÏTesting 10.0, result is true ÏϧÏTesting 10.5, result is false ÏϧÏTesting 11.0, result is false ÏϧÏRepeat test number 0 ÏϧÏRepeat test number 1 $ ÏϧÏRepeat test number 2 $$ ÏϧÏRepeat test number 3 $$$ ÏϧÏRepeat test number 4 $$$$ ÏϧÏRepeat test number 5 $$$$$ ÏϧÏRepeat test number 6 $$$$$$ ÏϧÏRepeat test number 7 $$$$$$$ ÏϧÏRepeat test number 8 $$$$$$$$ ÏϧÏRepeat test number 9 $$$$$$$$$Ï ÏÏ©Ï ----jGRASP: operation complete. ÏÏ 14-22.4 Self help exercises: Write methods to do the following 1. Display a line of 20 asterisks. 2. Display a line of n asterisks, where n is a parameter. 3. Take two integer parameters and return the larger. 4. Take two String parameters (a first name and a last name) and return a String that is the name in last-name-first order. For example, if the parameters were "John" and "Smith", the method would return "Smith, John". 5. Take three parameters: an integer array a and two integers i and j, and swap a[i] and a[j]. This requires only three lines of code in addition to the method header. 6. Take two parameters, an integer array a and an integer key, and return the index of the leftmost occurrence of key in the array, or return -1 if key is not in the array. Hint: for an array a, a.length is the number of elements in the array. 7. Take one parameter, an integer array, and return the smallest value in the array. 8. Take one parameter, an integer array, and return the index of the smallest value in the array. If the smallest value occurs more than once, return the index of the leftmost occurrence. 9. Take one parameter, an integer array, and store a zero in every element. 10. Take an integer parameter r and return the area of a circle whose radius is r. Hint: the formula for the area of a circle with radius r is πr2. 11. Take two double parameters, the number of hours worked and the hourly rate, and return the (double) salary where salary is hours*rate for the first 40 hours and 1.5*hours*rate for any hours over 40. 12. Take a String parameter and return a String that is the same as the parameter, except that leading and trailing blanks have been removed. So, for example, if the parameter is " 123 ", then the returned value is "123". 13. Take an integer array parameter and return the sum of the elements. 14. Take an integer array parameter and return the average of the elements. 15. Take an integer array parameter and return true if the array contains only numbers greater than or equal to zero. Try to be efficient.1 16. Take an integer array parameter and return true if the array is sorted in non-decreasing order left to right. 1 By efficient, I mean that you should return as soon as you know the answer.