Java Chapter 5

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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 .)

Math.sqrt(Math.sqrt(x))

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.

cubeVolume = toThePowerOf(cubeSide, 3);

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.

eurasiaSales = add(euroSales, asiaSales);

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.

max(population1, population2)

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.

printTodaysDate();

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?".

public String makeSubjectLine(String text) { return "Subject: " + text; }

Write the definition of a method min that has two int parameters and returns the smaller.

public int min (int aint, int bint){ return Math.min(aint, bint); }

Write the definition of a method add, which receives two integer parameters and returns their sum .

public static int add(int a, int b) { int c = a + b; return c; }

Write the definition of a method twice, which receives an integer parameter and returns an integer that is twice the value of the parameter .

public static int twice(int a) { int b = 2 * a; return b; }

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.

sendObject(John_Doe);

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.

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"); } }


Set pelajaran terkait

Intro to Business- Exam One ( Set One )

View Set

3000 most common words in written english - second 1000 words

View Set

Elapsed Time to the hour, half hour, and 5 minute

View Set

ACCT C101 Financial Accounting Chapter 10

View Set