Assignment 3 Methods Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments. E-mail address is: csc123csudh@gmail.com Each program is to be submitted in a separate file with the file name being the class name with extension.java as shown below. I only need the source file. GeneralAverage.java The other homework file should be submitted with your name using the following format: RosenthalH_Ax.doc, where x is the assignment number. Note: use your name, not mine All answers will be posted on web site, and most will be reviewed in class. Review Chapter 6 in Bravaco Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous 2. This appears at the beginning of a method definition. a. semicolon b. parentheses c. body d. header 3. The body of a method is enclosed in. a. curly braces { } b. square brackets [] c. parentheses () d. quotation marks "" 4. A method header can contain. a. method modifiers b. the method return type c. the method name d. a list of parameter declarations e. all of these f. none of these
5. A value that is passed into a method when it is called is known as a(n). a. parameter b. argument c. signal d. return value 6. A variable that receives a value that is passed into a method is known as a(n). a. parameter b. argument c. signal d. return value 7. This statement causes a method to end and sends a value back to the statement that called the method. a. end b. send c. exit d. return 8. True or False: You terminate a method header with a semicolon. 9. True or False: When passing an argument to a method, Java will automatically perform a widening conversion (convert the argument to a higher-ranking data type), if necessary. 10. True or False: When passing an argument to a method, Java will automatically perform a narrowing conversion (convert the argument to a lower-ranking data type), if necessary. 11. True or False: A parameter variable s scope is the entire program that contains the method in which the parameter is declared. 12. True or False: When code in a method changes the value of a formal parameter, it also changes the value of the actual parameter that was used to call the method. 13. True or False: When passing multiple arguments to a method, the order in which the arguments are passed is not important. 14. True or False: No two methods in the same program can have a local variable with the same name. 15. True or False: You must have a return statement in a value-returning method. 16. What is the output of this program public class ThisComputesSomeWeirdStuff { public static int method1(int a, int b) { if (a%2 == 0) return (a) else return (b) }
} public static int method2(int a, int b) { while (a!= 1) {b++; a = a / 2;} return (b); } public static void main(string[] args) { System.out.println(method2(method1(3, 10), method1(16, 57))); System.out.println(method2(method1(190, 10), method1(16, 57))); System.out.println(method2(method2(3, 10), method1(16, 57))); System.out.println(method1(method2(3, 10), method2(16, 57))); } 17. Overloaded Methods a. Method add(...) is overloaded as follows: static double add( int a, double b) { static double add(double a, int b) { return a + b; return a + b; } } Which, if any, of the following invocations fail to compile? Give reasons. i. add(1,2) ii. add(1.0,2.0) iii. add(1.0, 2) iv. add(2.0,2) b. Method sub( ) is overloaded as follows: int sub( int a, int b) { double sub(double a, double b) { return a - b; return a - b; } } Which, if any, of the following invocations fail to compile? Give reasons. i. sub(1,2) ii. sub(1.0,2.0) iii. sub(1.0, 2) iv. sub(2.0,2) c. What is the problem with the following overloaded method that returns a product as either an int or a long? int mul(int a, int b) { long mul(int a, int b) { return a * b; return a * b; } } Programming Exercises 1. HypotenuseAreaCalc Define a method hypotenuse that calculates the hypotenuse of a right triangle when the lengths of the two legs are given. The method accepts two type double arguments and returns the value of the hypotenuse as type double. Define a second method called areatri that calculates the area of the right triangle from the legs (remember Area =.5*b*h). The main method reads in the two values. It checks to make sure that both legs are greater than 0.0 or requests a reentry of both
values. It then calls each of the two methods and prints out a line that looks like this with values correct to two decimal places: The right triangle with legs leg1 and leg2 has a hypotenuse of length hypotonuselength and an area of trianglearea. 2. Multiples Write a method ismultiple that determines for a pair of integers, whether the second is a multiple of the first. The main method reads in the two integers and then calls ismultiple with the two integers as parameter, returning true if the second is a multiple of the first, otherwise false. The main program prints out the resulting answer of true or false. 3. EvenOddCounter Write a program with a method named iseven that accepts an int argument. The method should return true if the argument is even, or false otherwise. The program s main method should use a loop to generate 100 random integers between 1 and 100. It should use the iseven method to determine whether each random number is even, or odd. When the loop is finished, the program should display the number of even numbers that were generated, and the number of odd numbers. 4. ReverseDigits Write a method reverse that takes an integer value and return the number with its digits reversed. The main program reads in the integer and prints the reversed integer. For instance, if the input is 54378 the output integer would be 87345. 5. Craps Simulation When playing craps, a player rolls two dice repeatedly until she wins or loses. The first roll of the dice is called the come-out roll. If the player rolls a 7 or an 11 on the comeout roll, then she wins immediately. A 2, 3, or 12 on the come-out roll results in an immediate loss. If she rolls a 4, 5, 6, 8, 9, or 10 on the come-out roll, then that number becomes her point and she continues rolling until she rolls either her point or a 7. If she rolls her point, she wins, but if she rolls a 7 before rolling her point, she loses. Once a player has established her point, no other numbers (including 2, 3, 11, or 12) affect her winning or losing. Write a method boolean craps() that simulates one game of craps and returns true if and only if the player wins. Test your method by printing the values of each roll of the dice. When you are convinced that your simulation is correct, include this method in a program that executes craps() n times by requesting n from the user, and reports the percentage of wins correct to two decimal places. Note you need the roll of two six-sided die, not one 12-sided die in this simulation.
Here is a sample with 10 runs: Please enter the number of simulations you wish to run: 10 The come-out is 8 It is TRUE that you won craps game 1 The come-out is 3 It is FALSE that you won craps game 2 The come-out is 9 The next roll is 10 The next roll is 12 The next roll is 4 The next roll is 3 The next roll is 7 It is FALSE that you won craps game 3 The come-out is 9 The next roll is 10 The next roll is 7 It is FALSE that you won craps game 4 The come-out is 2 It is FALSE that you won craps game 5 The come-out is 4 The next roll is 3 The next roll is 9 The next roll is 9 The next roll is 3 The next roll is 4 It is TRUE that you won craps game 6 The come-out is 7 It is TRUE that you won craps game 7 The come-out is 5
The next roll is 7 It is FALSE that you won craps game 8 The come-out is 6 It is TRUE that you won craps game 9 The come-out is 8 The next roll is 9 The next roll is 12 It is TRUE that you won craps game 10 The player won 50.00% games out of the 10 games played