15a-section-2-solutions

15a-section-2-solutions - Mehran Sahami CS 106A Handout...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Mehran Sahami Handout #15A CS 106A October 7, 2009 Solutions for Section #2 Portions of this 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; } 2. Drawing centered text /* * File: CenteredText.java * ----------------------- * This programs displays a message centered in the graphics window. */ import acm.graphics.*; import acm.program.*; public class CenteredText extends GraphicsProgram { public void run() { GLabel label = new GLabel("CS106A rocks my socks!"); label.setFont("SansSerif-28");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/01/2009 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 2

15a-section-2-solutions - Mehran Sahami CS 106A Handout...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online