Problem 1
Fill in the blanks in each of the following statements: a) A method is invoked with a(n)_______ b) \(A\) variable known only within the method in which it is declared is called a(n) ________ c) The _____ statement in a called method can be used to pass the value of an expression back to the calling method. The keyword _____ indicates that a method does not return a value. e) Data can be added or removed only from the ______ of a stack. f) Stacks are known as ______ data structures - the last item pushed (inserted) on the stack is the first item popped (removed) from the stack. g) The three ways to return control from a calfed method to a caller are ______ . _____ and _____ h) An object of class_____ produces random numbers. 190 i) The program execution stack contains the memory for local variables on each invocation of a method during a program's execution. This data, stored as a portion of the prosogram execution stack, is known as the _____ or _____ of the method call. j) If there are more method calls than can be stored on the program execution stack, an error known as a(n)_____ Occurs. k) The ______ of a declaration is the portion of a program that can refer to the entity in the declaration by name. l) In Java, it is possible to have several methods with the same name that each operate on different types or numbers of arguments. This feature is called method ._______ m) The program execution stack is also referred to as the ________ stack.
Problem 4
Give the method header for each of the following methods: a) Method hypotenuse, which takes two double-precision, floating-point arguments sidel and side 2 and returns a double-precision, floating-point result. b) Method smallest, which takes three integers \(x, y\) and \(z\) and returns an integer. c) Method instructions, which does not take any arguments and does not return a value. \([\)Note: Such methods are commonly used to display instructions to a user. method intToF 7 oat, which takes an integer argument number and returns a floatingpoint result.
Problem 5
Find the error in each of the following program segments. Explain how to correct the error. a) int g() { System.out.println( "Inside method g" ); int h() { System.out.println( "Inside method h" ); } } b) int sum( int x, int y ) { int result; result = x + y; } c) void f( float a ); { float a; System.out.println( a ); } d) void product() { int a = 6, b = 5, c = 4, result; result = a * b * c; System.out.printf( "Result is %d\n", result ); return result;
Problem 6
Write a complete Java application to prompt the user for the double radius of a sphere, and call method sphereVolume to calculate and display the volume of the sphere. Use the following statement to calculate the volume: double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 )
Problem 7
What is the value of x after each of the following statements is executed? a) x = Math.abs( 7.5 ); b) x = Math.floor( 7.5 ); c) x = Math.abs( 0.0 ); d) x = Math.ceil( 0.0 ); e) x = Math.abs( -6.4 ); f) x = Math.ceil( -6.4 ); g) x = Math.ceil( -Math.abs( -8 + Math.floor( -5.5 ) ) );
Problem 8
A parking garage charges a \(\$ 2.00\) minimum fee to park for up to three hours. The garage charges an additional \(\$ 0.50\) per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24 -hour period is \(\$ 10.00 .\) Assume that no car parks for longer than 24 hours at a time. Write an application that calculates and displays the parking charges for each customer who parked in the garage yesterday. You should enter the hours parked for each customer. The program should display the charge for the current customer and should calculate and display the running total of yesterday's receipts. The program should use the method calculateCharges to determine the charge for each customer.
Problem 9
An application of method Math.floor is rounding a value to the nearest integer. The statement y = Math.floor( x + 0.5 ); will round the number x to the nearest integer and assign the result to y. Write an application that reads double values and uses the preceding statement to round each of the numbers to the nearest integer. For each number processed, display both the original number and the rounded number.
Problem 10
Math.floor may be used to round a number to a specific decimal place. The statement y = Math.floor( x * 10 + 0.5 ) / 10; rounds x to the tenths position (i.e., the first position to the right of the decimal point). The statement y = Math.floor( x * 100 + 0.5 ) / 100; rounds x to the hundredths position (i.e., the second position to the right of the decimal point). Write an application that defines four methods for rounding a number x in various ways: a) roundToInteger( number ) b) roundToTenths( number ) c) roundToHundredths( number ) d) roundToThousandths( number ) For each value read, your program should display the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hun- dredth and the number rounded to the nearest thousandth.
Problem 11
Answer each of the following questions: a) What does it mean to choose numbers "at random?" b) Why is the nextInt method of class Random useful for simulating games of chance? c) Why is it often necessary to scale or shift the values produced by a Random object? d) Why is computerized simulation of real-world situations a useful technique?
Problem 13
For each of the following sets of integers, write a single statement that will display a number stat random from the set: a) 2,4,6,8,10 b) 3,5,7,9,11 c) $6,10,14,18,22