Docsity

Simple Java - Section Handout - Programming Methodology- 15, Exercises of Programming Methodologies

Toggle sidebar
bg1
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:
Fib(0) = 0
Fib(1) = 1
Fib(2) = 1 (0 + 1)
Fib(3) = 2 (1 + 1)
Fib(4) = 3 (1 + 2)
Fib(5) = 5 (2 + 3)
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.
pf3
pf4

Partial preview of the text

Download Simple Java - Section Handout - Programming Methodology- 15 and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout

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:

Fib (0) = 0

Fib (1) = 1

Fib (2) = 1 (0 + 1)

Fib (3) = 2 (1 + 1)

Fib (4) = 3 (1 + 2)

Fib (5) = 5 (2 + 3)

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.

  • 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:

  • The head. 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.

  • The eyes. 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.

  • The mouth. The mouth should be centered with respect to the head in the x -dimension

and one quarter of the distance up from the bottom of the head in the y -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.

  • 2 –

2. Drawing a robot face

/* File: RobotFace.java / / This program draws a robot face. / import acm.graphics.; import acm.program.; import java.awt.; public class RobotFace extends GraphicsProgram { /* 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; public void run() { addFace(getWidth() / 2, getHeight() / 2); } / Adds the entire face centered at (cx, cy) / private void addFace(double cx, double cy) { addHead(cx, cy); addEye(cx - HEAD_WIDTH / 4, cy - HEAD_HEIGHT / 4); addEye(cx + HEAD_WIDTH / 4, cy - HEAD_HEIGHT / 4); addMouth(cx, cy + HEAD_HEIGHT / 4); } / Adds the head centered at (cx, cy) / private void addHead(double cx, double cy) { double x = cx - HEAD_WIDTH / 2; double y = cy - HEAD_HEIGHT / 2; GRect head = new GRect(x, y, HEAD_WIDTH, HEAD_HEIGHT); head.setFilled(true); head.setFillColor(Color.GRAY); add(head); } / Adds an eye centered at (cx, cy) / private void addEye(double cx, double cy) { double x = cx - EYE_RADIUS; double y = cy - EYE_RADIUS; GOval eye = new GOval(x, y, 2 * EYE_RADIUS, 2 * EYE_RADIUS); eye.setFilled(true); eye.setColor(Color.YELLOW); add(eye); } / Adds a mouth centered at (cx, cy) */ private void addMouth(double cx, double cy) { double x = cx - MOUTH_WIDTH / 2; double y = cy - MOUTH_HEIGHT / 2; GRect mouth = new GRect(x, y, MOUTH_WIDTH, MOUTH_HEIGHT); mouth.setFilled(true); mouth.setColor(Color.WHITE); add(mouth); } }