stanford編程方法——習題答案(the art and science of java)——chapter05

來源:互聯網
上載者: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 mehtod
    aruments: pass the
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 would each

action be appropriate?
Answer:
    Input and output refer to
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 at
which execution should 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 the
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 φ is called the golden ratio. Classical mathematicians believed that this
number represented the most aesthetically pleasing ratio for the dimensions of a
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 />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.