10-Basic-Examples-1

10-Basic-Examples-1 - CS106A Handout 10 April 6th, 2011...

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

View Full Document Right Arrow Icon
CS106A Handout 10 Spring 2011 April 6 th , 2011 Introductory Java: Crash Course Examples Welcome to Stanford! /** * File: WelcomeToStanford.java * ---------------------------- * Simple program that paints a glorious banner * (color? red! what else?) welcoming all of you * to The Farm. */ import acm.program.*; import acm.graphics.*; import java.awt.*; public class WelcomeToStanford extends GraphicsProgram { /** * The primary method where the program begins. GLabel is * an example of a 'class', and welcomeText is an example * of an 'object'. If objects are statues, then classes are the * molds which dictate what the statues look like. * * Lines 2 and 3 invoke member functions on behalf of welcomeText which * are taken to be instructions on how welcomeText should modify itself. * The add function is an instruction to add the text, with its new color * and font, to the graphics window. */ public void run() { GLabel welcomeText = new GLabel("Welcome To Stanford, Everyone!!", 50, 250); welcomeText.setFont("Palatino-Bold-36"); welcomeText.setColor(Color.RED); add(welcomeText); } } Program comment that provides an English overview of exactly what the Java program does. Library inclusions grant us access to code that’s already been written. As with all of our Karel programs, all of our Java programs have a run method (the phrases method , function , and member function are used interchangeably, so get used to all three). The run method is the place where you place your code, which comes in the form of a sequence of Java statements. welcomeText is an example of a variable, which is a programming unit used to keep track of a single piece of information. In the above example, we declare welcomeText and initialize it to be a new GLabel , we invoke methods on behalf of the welcomeText variable to set its font and color, and then add the text label to the graphics window. For the most part, everything outside the dashed box here is boilerplate—a necessary evil of programming in Java—and will be given to you to start.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Five People In My Family: One Two Three Four Five /** * File: FivePeopleInMyFamily.java * ------------------------------- * Introductory program that prints out the first verse of * Sesame Street's "Five People In My Family" as performed * by the Anything Muppets. * * Modern Rendition: http://www.youtube.com/watch?v=8Wb7LotxOWw */ import acm.program.*; public class FivePeopleInMyFamily extends ConsoleProgram { /** * Prints the first two verses of the Sesame Street * classic song, "Five People In My Family". */ public void run() { println("Oh, I've got five people in my family"); println("And there's not one of them I'd swap"); println("There is a sister, and two brothers"); println("And a mother, and a pop (that's me)."); println(); // no arguments? print a blank line! println("Oh, five is such a pretty number!");
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

10-Basic-Examples-1 - CS106A Handout 10 April 6th, 2011...

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

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