Stanford programming method-the art and science of Java-chapter05

Source: Internet
Author: User

 

Chapter 05 Methods
Review questions

-------------------------------------------------------------------------------

1. Explain in your own words the difference between a method and a program.

Answer:
Lies in who or what makes use of it

-------------------------------------------------------------------------------

2. Define the following terms as they apply to Methods: Call, argument,
Return.
Answer:
Call: To apply a mehtodd
Aruments: Pass
Paramter to a method
Return: return a result from a method

-------------------------------------------------------------------------------

3. What is the difference between passing information to a method by using

Arguments and reading input data using methods like readint? When wocould each

Action be appropriate?
Answer:
Input and Output refer
Communication between a program and its user.
Arguments and results
Represetn communication between a Method and Its caller.

-------------------------------------------------------------------------------

4. How do you specify the result of a method in Java?
Answer:

Return expression;

-------------------------------------------------------------------------------

5. Can there be more than one return statement in the body of a method?

Answer:
Yes. For example:
Private double min (Double X,
Double Y)
{
If (x <Y)
Return X;

Else
Return y;
}

-------------------------------------------------------------------------------

6. How do you indicate that you want to apply a method to another object?

Answer:
Reciever. Name (arguments );

-------------------------------------------------------------------------------

7. Why has it been unnecessary to specify receivers in the programs
Presented in
The earlier chapters?
Answer:
For those methods
Have defined in their superclass

-------------------------------------------------------------------------------

8. Why was it unnecessary to include a break statement at the end of each
Case
Clause in the monthname method presented in the chapter?
Answer:

Return statements automatically exit from the entire Method

-------------------------------------------------------------------------------

9. What is a predicate method?
Answer:
Return values of Type
Boolean

-------------------------------------------------------------------------------

10. How do you tell whether two strings contain the same characters?

Answer:
Private Boolean havesamecharacter (string S1, string S2)

{
For (INT I = 0; I <s1.length (); I ++)

For (Int J = 0; j <s2.length (); j ++)
If (S1 [I] = S2 [J])

Return true;

Return false;
}

-------------------------------------------------------------------------------

11. What is the relationship between arguments and formal parameters?

Answer:
Copy

-------------------------------------------------------------------------------

12. variables declared within a method are said to be local variables. What
Is
The significance of the word local in this context?
Answer:

The variable scope

-------------------------------------------------------------------------------

13. What does the term return address mean?
Answer:
The point
Which execution shold continue

-------------------------------------------------------------------------------

14. What is a brute-force algorithm?
Answer:
Int gcd (int x, int
Y)
{
Int guess = math. Min (x, y );
While (X %
Guess! = 0 | Y % guess! = 0)
Guess --;
Return guess;

}

-------------------------------------------------------------------------------

15. Use Euclid's algorithm to compute the greatest common divisor of 7735
And
4185. What values does the local variable r take on during
Calculation?
Answer:
3350
835
10

5
0

-------------------------------------------------------------------------------

16. In the examples of Euclid's algorithm to calculate GCD (x, y) that appear
In
This chapter, X is always larger than Y. Does this condition matter? What

Happens if X is smaller than Y?
Answer:
Doesn' t matter

If x <Y, then R = x % Y = x and X! = 0, so x = y and Y = r
Then y
> X;

-------------------------------------------------------------------------------

Programming exercises

1. Write a program that displays the value of the mathematical constant

This constant Phi is called the golden ratio. Classical mathematicians believed that this
Number represented the most aesthetically pleasing ratio for the dimensions of
Rectangle, but it also turns up in computational mathematics.

 

 

/* <Br/> * file: goldenratio. java <br/> * --------------------- <br/> * This program dispalys the golden ratio. <br/> * fi = (1 + SQRT (5)/2; <br/> */<br/> Import ACM. program. *; <br/> Import Java. lang. math; <br/> public class goldenratio extends consoleprogram {<br/> Public void run () {<br/> double Fi = (1 + math. SQRT (5)/2; <br/> println ("golden ratio: fi =" + FI); <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.