The document is a handout about methods in Java from a CS 106A class. It discusses what methods are, how they are similar to and different from methods in Karel, and some important aspects for understanding methods in Chapter 5 of the textbook. Methods allow programmers to break programs into reusable components and hide implementation details. They can pass data between each other via arguments and return values.
The document is a handout about methods in Java from a CS 106A class. It discusses what methods are, how they are similar to and different from methods in Karel, and some important aspects for understanding methods in Chapter 5 of the textbook. Methods allow programmers to break programs into reusable components and hide implementation details. They can pass data between each other via arguments and return values.
The document is a handout about methods in Java from a CS 106A class. It discusses what methods are, how they are similar to and different from methods in Karel, and some important aspects for understanding methods in Chapter 5 of the textbook. Methods allow programmers to break programs into reusable components and hide implementation details. They can pass data between each other via arguments and return values.
Chapter 5 introduces the concept of methods in Java, which are analogous to those you created in Karel. In contrast to the topics of expressions and control statements, the material in Chapter 5 involves far fewer details. On the other hand, the ideas introduced in this chapter are far more important. If you plot overall importance on a scale of 1 to 10, the switch statement probably weighs in somewhere around 2 (and I'm being very generous here... poor old switch statement); methods are definitely a 10.
The common idea that links methods in Karel and Java is that both provide a service to other, higher-level parts of the program and therefore act as tools. In both languages, the run method can call subsidiary methods to accomplish parts of the overall task. Those methods in turn call other methods that perform simpler operations, and so on. The caller views the method in terms of the effect it accomplishes. The method supplies all the details about how that operation is done. By hiding the details of complex operations, methods simplify the conceptual structure of a program considerably and allow you as a programmer to view it at varying levels of detail.
The fundamental difference between methods in Karel and their counterparts in Java is that Java makes it possible for data to pass back and forth between the caller and the method. Callers supply information to the method by supplying arguments; methods give information back to their callers by returning results. The entire process of passing this data between the two levels is in many respects the most important issue for you to understand in Chapter 5. In particular, you should take note of the following:
Methods can be applied to other objects. In this case, the syntax of the call is
UHFHLYHU.QDPH(DUJXPHQWV)
Arguments in the calling method are assigned to the corresponding formal parameters in the callee according to their position in the argument list. Thus, the first argument is assigned to the first parameter name, the second to the second, and so on. The names of the variables are completely irrelevant to this process. Arguments are copied rather than shared. If you change the value of a formal parameter, the corresponding actual argument even if it is a variable with the same name is unaffected. The return statement causes a method to return immediately to its caller and also indicates the value to be returned as a result.
Methods can return values of any of the types you have encountered so far. Most of you will have little trouble with methods that return numeric data because you are familiar with this concept from high-school algebra. For some reason, methods that return objects or Boolean data seem harder, although the basic idea is precisely the same. Methods that return Boolean values, which are usually called predicate methods. are extremely important to programming. http://technicalsupportindia.blogspot.com/ id9147734 pdfMachine by Broadgun Software - a great PDF writer! - a great PDF creator! - http://www.pdfmachine.com http://www.broadgun.com Mehran Sahami Handout #15 CS 106A October 8, 2007 Section Handout #2 Simple Java Based on a handout by Eric Roberts
1. The Fibonacci sequence In the 13th century, the Italian mathematician Leonardo Fibonacci as a way to explain the geometic growth of a population of rabbits devised a mathematical sequence that now bears his name. The first two terms in this sequence, Fib(0) and Fib(1), are 0 and 1, and every subsequent term is the sum of the preceding two. Thus, the first several terms in the Fibonacci sequence look like this:
Write a program that displays the terms in the Fibonacci sequence, starting with Fib(0) and continuing as long as the terms are less than 10,000. Thus, your program should produce the following sample run:
This program continues as long as the value of the term is less than the maximum value, so that the loop construct you need is a while, presumably with a header line that looks like this:
while (term < MAX_TERM_VALUE)
Note that the maximum term value is specified using a named constant. http://technicalsupportindia.blogspot.com/ 2 2. Drawing a face Your job is to draw a robot-looking face like the one shown in the following sample run:
This simple face consists of four parts a head, two eyes, and a mouth which are arranged as follows:
7KH KHDG The head is a big rectangle whose dimensions are given by the named constants HEAD_WIDTH and HEAD_HEIGHT. The interior of the head is gray, although it should be framed in black. 7KH H\HV The eyes should be cricles whose radius in pixels is given by the named constant EYE_RADIUS. The centers of the eyes should be set horizontally a quarter of the width of the head in from either edge, and one quarter of the distance down from the top of the head. The eyes are yellow. 7KHPRXWK The mouth should be centered with respect to the head in the [-dimension and one quarter of the distance up from the bottom of the head in the \-dimension. The dimensions of the mouth are given by the named constants MOUTH_WIDTH and MOUTH_HEIGHT. The mouth is white.
Finally, the robot face should be centered in the graphics window. http://technicalsupportindia.blogspot.com/ Mehran Sahami Handout #15A CS 106A October 10, 2007 Solutions for Section #2 Based on a handout by Eric Roberts 1. The Fibonacci sequence
/* * File: Fibonacci.java * -------------------- * This program lists the terms in the Fibonacci sequence up to * a constant MAX_TERM_VALUE, which is the largest Fibonacci term * the program will display. */
import acm.program.*;
public class Fibonacci extends ConsoleProgram {
public void run() { println("This program lists the Fibonacci sequence."); int t1 = 0; int t2 = 1; while (t1 <= MAX_TERM_VALUE) { println(t1); int t3 = t1 + t2; t1 = t2; t2 = t3; } }
/* Defines the largest term to be displayed */ private static final int MAX_TERM_VALUE = 10000;
}
http://technicalsupportindia.blogspot.com/ 2 2. Drawing a robot face /* File: RobotFace.java */ /* This program draws a robot face. */
/* Parameters for the drawing */ private static final int HEAD_WIDTH = 100; private static final int HEAD_HEIGHT = 150; private static final int EYE_RADIUS = 10; private static final int MOUTH_WIDTH = 60; private static final int MOUTH_HEIGHT = 20;
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More