Java Chapter 5

Ace your homework & exams now with Quizwiz!

The Math class provides a static method, max, that accepts two int arguments and returns the value of the larger one. Four int variables, population1, population2, population3, and population4 have already been declared and initialized. Write an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max.

Math.max(Math.max(population1,population2),Math.max(population3,population4))

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

Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max.

if (x > y) max = x; else max = y;

Given that a method receives three parameters a, b, c, of type double, write some code, to be included as part of the method, that determines whether the value of "b squared" - 4ac is negative. If negative, the code prints out the message "no real solutions" and returns from the method

if((Math.pow(b,2) - 4 *a*c) < 0) {System.out.print("no real solutions");}

Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer variable min. Assume that all the variables have already been declared and that x, y, and z have been assigned values.

if(x < y){ if(x<z){ min = x; } else{ min = z; } }else{ if(y<z){ min = y; }else{ min = z;} }

A showChar MethodWrite a method named showChar. The method should accept two arguments: a reference toa String object and an integer. The integer argument is a character position within theString, with the first character being at position 0. When the method executes, it shoulddisplay the character at that character position. The method does not return anything.Here is an example of a call to the method:showChar("New York", 2);In this call, the method will display the character w because it is in position 2. Demonstratethe method in a complete program-- use the class name Method_showChar

import java.util.Scanner; public class Method_showChar { public static void main(String[] args) { String lineOfText; int index; Scanner stdin = new Scanner(System.in); System.out.print("Enter a line of text:"); lineOfText = stdin.nextLine(); System.out.print(" Enter your index: "); index =stdin.nextInt(); showChar(lineOfText,index); } public static void showChar(String str, int index){ System.out.print(str.charAt(index)); } }

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)

printLarger is a method that accepts two int arguments and returns no value. Two int variables, sales1 and sales2, have already been declared and initialized. Write a statement that calls printLarger, passing it sales1 and sales2. Assume that printLarger is defined in the same class that calls it.

printLarger ( sales1 , sales2 );

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 a method max that has two string parameters and returns the larger of the two.

public String max (String aString, String bString){ if (aString.compareTo(bString) > 0){ return aString; } else{ return bString; } }

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 definition of a method printDottedLine, which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a newline) consisting of five periods.

public static void printDottedLine (){ System.out.println("....."); }

Write the definition of a method dashedLine, with one parameter, an int. If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes. The method returns nothing.

public void dashedLine (int a){ if (a>0){ int i; for (i=1;i<=a;i=i+1){ System.out.print("-"); }System.out.println(""); } }

Write the code for invoking a method named sendNumber . There is one int argument for this method. Send the number 5 as an argument to this method. Assume that sendNumber is defined in the same class that calls it.

sendNumber(5);

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 code for invoking a method named sendSignal. There are no arguments for this method. Assume that sendSignal is defined in the same class that calls it.

sendSignal();

Write the code for invoking a method named sendTwo. There are two arguments for this method: a double and an int. Invoke the method with the double value of 15.955 and the int value of 133. Assume that sendTwo is defined in the same class that calls it.

sendTwo(15.955,133) ;

Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a method (in the same class as the caller) that receives a single integer parameter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14.Write an expression whose value is eight times that of x without using the standard Java arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this.In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

twice(twice(twice(x)))

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


Related study sets

Chapter 6 Section 3 Colonial Latin America

View Set

Chapter 8 Trademarks, related property and Patents

View Set