Java Chapter 5 Methods

¡Supera tus tareas y exámenes ahora con Quizwiz!

This appears at the beginning of a method definition.

Header

Is the following line of code a method header or a method call? calcTotal();

Method call

Is the following line of code a method header or a method call? public static void calcTotal()

Method header

A method header can contain __________.

Method modifies, method return type, method name, parameter declarations.

Explain what is meant by the phrase "pass by value."

When an argument is passed to a method, only a copy of the argument is passed. The method cannot access the actual argument.

True or False: A parameter variable's scope is the entire program that contains the method in which the parameter is declared.

false

True or False: It is possible for one method to access a local variable that is declared in another method.

false

True or False: No two methods in the same program can have a local variable with the same name.

false

True or False: When code in a method changes the value of a parameter, it also changes the value of the argument that was passed into the parameter.

false

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.

false

True or False: When passing multiple arguments to a method, the order in which the arguments are passed is not important.

false

True or False: You terminate a method header with a semicolon.

false

Write the header for a method named distance. The method should return a double and have two double parameter variables: rate and time.

public static double distance(double rate, double time)

Write a method named timesTen. The method should accept a double argument, and return a double value that is ten times the value of the argument.

public static double timesTen(double num) { return num * 10.0; }

Write the header for a method named days. The method should return an int and have three int parameter variables: years, months, and weeks.

public static int days(int years, int months, int weeks)

Write the header for a method named lightYears. The method should return a long and have one long parameter variable: miles.

public static long lightYears(long miles)

True or False: You must have a return statement in a value-returning method.

true

precedence of all operators

- (unary negation) ! * / % + - < > <= >= == != && || = += -= *= /= %=

Write a method named getName that prompts the user to enter his or her first name, and then returns the user's input.

// Assume java.util.Scanner has been imported. public static String getName() { String name; Scanner keyboard = new Scanner(System.in); System.out.print("Enter your first name: "); name = keyboard.nextLine(); return name; }

Write a void method that displays your full name. The method should be named myName.

// This is how Mary Catherine Jones would write it. public static void myName() { System.out.println("Mary Catherine Jones"); }

What value will be stored in c?

1 will be stored in c.

What value will be stored in b?

2 will be stored in b, and

What will the following program display? public class Checkpoint { public static void main(String[] args) { int num1 = 99; double num2 = 1.5; System.out.println(num1 + " " + num2); myMethod(num1, num2); System.out.println(num1 + " " + num2); } public static void myMethod(int i, double d) { System.out.println(i + " " + d); i = 0; d = 0.0; System.out.println(i + " " + d); } }

99 1.5 99 1.5 0 0.0 99 1.5

This javadoc tag is used to document a parameter variable.

@param

This javadoc tag is used to document a method's return value.

@return

What is the "divide and conquer" approach to problem solving?

A large complex problem is broken down into smaller manageable pieces. Each smaller piece of the problem is then solved.

Why do local variables lose their values between calls to the method in which they are declared?

A method's local variables exist only while the method is executing. This is known as the lifetime of a local variable. When the method begins, its local variables and its parameter variables are created in memory, and when the method ends, the local variables and parameter variables are destroyed. This means that any value stored in a local variable is lost between calls to the method in which the variable is declared

What is the difference between a void method and a value-returning method?

A void method, which does not return a value, uses the key word void as its return type in the method header. A value-returning method will use int, double, boolean, or any other valid data type in its header.

What is the difference between an argument and a parameter variable?

An argument is a value that is passed into a method when the method is called. A parameter variable is a variable that is declared in the method header, and receives the value of an argument when the method is called.

What is the difference between an argument and a parameter?

An argument is a value that is passed into a method. A parameter is a special variable that holds the value being passed into the method.

A value that is passed into a method when it is called is known as a(n) __________.

Argument

The body of a method is enclosed in __________.

Curly Braces

What if the user enters 10?

If the user enters 10 the program will display I saw Elba.

What if the user enters 100?

If the user enters 100 the program will display I saw Elba.

What message will the following program display if the user enters 5?

If the user enters 5 the program will display Able was I.

In Java, method arguments are passed by value. What does this mean?

Only a copy of an argument's value is passed into a parameter variable. A method's parameter variables are separate and distinct from the arguments that are listed inside the parentheses of a method call. If a parameter variable is changed inside a method, it has no effect on the original argument.

Suppose a method named showValues accepts two int arguments. Which of the following method headers is written correctly? a) public static void showValues() b) public static void showValues(int num1, num2) c) public static void showValues(num1, num2) d) public static void showValues(int num1, int num2)

Only d is written correctly.

A variable that receives a value that is passed into a method is known as a(n) __________.

Parameter

This statement causes a method to end and sends a value back to the statement that called the method.

Return

// This method has an error! public static void sayHello(); { System.out.println("Hello"); }

The header should not be terminated with a semicolon.

// This method has an error! public static double timesTwo(double num) { double result = num * 2; }

The method should have a return statement that returns a double value

Look at the following method header: public static void myMethod(int a, int b, int c) Now look at the following call to myMethod: myMethod(3, 2, 1); When this call executes, what value will be stored in a?

The value 3 will be stored in a,

This type of method does not return a value.

Void

Where do you declare a parameter variable?

are declared inside the parentheses in the method header. This is often referred to as a parameter list.

Look at the following method header: public static void myMethod(int num) Which of the following calls to the method will cause a compiler error? a) myMethod(7); b) myMethod(6.2); c) long x = 99; myMethod(x); d) short s = 2; myMethod(s);

b and c will cause a compiler error because the values being sent as arguments (a double and a long) cannot be automatically converted to an int.

Examine the following method header, and then write an example call to the method: public static void doSomething(int x)

doSomething(25);

Look at the following method header. What type of value does the method return? public static double getValue(int a, float b, String c)

double

A program contains the following method definition: public static int cube(int num) { return num * num * num; } Write a statement that passes the value 4 to this method and assigns its return value to a variable named result.

result = cube(4);

True or False: The contents of a String object cannot be changed.

true

True or False: When an object, such as a String, is passed as an argument, it is actually a reference to the object that is passed.

true

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.

true


Conjuntos de estudio relacionados

Anatomy and Physiology Test 2: FINAL

View Set

Lecture 5: Capital Expenditure Decisions

View Set

CITBC102 Hardware Technician: Security

View Set

English Writing Workshop test 2020

View Set

CH 48 Hematological and Oncological Disorders

View Set

Chapter 14: The Organization of International Business

View Set

III. The Practice of Globalization

View Set

Adaptive Immunity (MasteringMicro)

View Set

Chapter 7- Activity Based Costing

View Set