Java Chapter 5

7 September 2022
4.7 (114 reviews)
11 test answers

Unlock all answers in this set

Unlock answers (7)
question
printTodaysDate is a method that accepts no arguments and returns no value . Write a statement that calls printTodaysDate. Assume that printTodaysDate is defined in the same class that calls it.
answer
printTodaysDate();
Explanation: The printTodaysDate method is called without any arguments and returns no value.
question
Write the code for invoking a method named sendObject. There is one argument for this method which is of type Customer. Assume that there is a reference to an object of type Customer, in a variable called John_Doe. Use this reference as your argument . Assume that sendObject is defined in the same class that calls it.
answer
sendObject(John_Doe);
question
Write the definition of a method printAttitude, which has an int parameter and returns nothing. The method prints a message to standard output depending on the value of its parameter . If the parameter equals 1, the method prints disagree If the parameter equals 2, the method prints no opinion If the parameter equals 3, the method prints agree In the case of other values , the method does nothing. Each message is printed on a line by itself.
answer
void printAttitude (int pint){ if (pint == 1){ System.out.println("disagree"); } if (pint == 2){ System.out.println("no opinion"); } if (pint == 3){ System.out.println("agree"); } }
question
Write the definition of a method twice, which receives an integer parameter and returns an integer that is twice the value of the parameter .
answer
public static int twice(int a) { int b = 2 * a; return b; }
question
Write the definition of a method add, which receives two integer parameters and returns their sum .
answer
public static int add(int a, int b) { int c = a + b; return c; }
question
toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized . Another int variable , cubeVolume, has already been declared . Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and that stores this value in cubeVolume. Assume that toThePowerOf is defined in the same class that calls it.
answer
cubeVolume = toThePowerOf(cubeSide, 3);
Explanation: The statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and stores this value in cubeVolume is as follows:cubeVolume = toThePowerOf(cubeSide, 3);This statement calls the toThePowerOf method, passing in the value of cubeSide as the first argument and 3 as the second argument. toThePowerOf computes the value of cubeSide raised to the power of 3 and returns this value, which is then stored in the cubeVolume variable.
question
add is a method that accepts two int arguments and returns their sum . Two int variables , euroSales and asiaSales, have already been declared and initialized . Another int variable , eurasiaSales, has already been declared . Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales. Assume that add is defined in the same class that calls it.
answer
eurasiaSales = add(euroSales, asiaSales);
Explanation: The add method is a method that accepts two int arguments and returns their sum. In this case, the two int variables euroSales and asiaSales have already been declared and initialized. Another int variable, eurasiaSales, has already been declared. The statement that calls add to compute the sum of euroSales and asiaSales and store this value in eurasiaSales would look like this:eurasiaSales = add(euroSales, asiaSales);
question
Write a method , makeSubjectLine, that gets a String argument and returns the same String but with "Subject: " in front of it. So if the argument is "Are you going to the show?" the method returns "Subject: Are you going to the show?".
answer
public String makeSubjectLine(String text) { return "Subject: " + text; }
question
Write the definition of a method min that has two int parameters and returns the smaller.
answer
public int min (int aint, int bint){ return Math.min(aint, bint); }
question
max is a method that accepts two int arguments and returns the value of the larger one. Two int variables , population1 and population2, have already been declared and initialized . Write an expression (not a statement !) whose value is the larger of population1 and population2 by calling max. Assume that max is defined in the same class that calls it.
answer
max(population1, population2)
Explanation: The max method is a way of finding the largest value of two integers. In this case, the two integers are population1 and population2. The max method will take the two integers and compare them, returning the larger integer.
question
Assume that x is a variable that has been declared as a double and been given a value . Write an expression to compute the quartic root of x. The quartic root of a number is the square root of its square root. EXAMPLES: For example, the quartic root of 16.0 is 2.0 because: the square root of 16.0 is 4.0 and the square root of 4.0 is 2.0. Another example: the quartic root of 81.0 is 3.0 because the square root of 81.0 is 9.0 and the square root of 9.0 is 3.0. Thus, to find the quartic root of a number you take the square root of the number and then take the square root of that. In this exercise you must find the quartic root of x in a single expression -- you must not write any statements . Also, you may only use the sqrt() static method defined in the Math class -- no other methods . (HINT: you will need to call the Math.sqrt() method twice-- and you will need to pass the return value of one of those calls as argument to the other call. AND REMEMBER: write an expression , not a statement .)
answer
Math.sqrt(Math.sqrt(x))