02-ProgrammingByExample - The Art and Science of An...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Programming by Example C H A P T E R 2 Example is always more efficacious than precept. Samuel Johnson, Rasselas, 1759 2.1 The Hello world program 2.2 Perspectives on the programming process 2.3 A program to add two numbers 2.4 Programming idioms and patterns 2.5 Classes and objects 2.6 Graphical programs The Hello World Program One of the important influences on the design of Java was the C programming language, which was developed at Bell Labs in the early 1970s. The primary reference manual for C was written by Brian Kernighan and Dennis Ritchie. On the first page of their book, the authors suggest that the first step in learning any language is to write a simple program that prints the message hello, world on the display. That advice remains sound today. 1.1 Getting Started The only way to learn a new programming language is to write programs in it. The first program to write is the same for all languages: Print the words hello, world This is the big hurdle; to leap over it you have to be able to create the program text somewhere, compile it, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy. In C, the program to print hello, world is #include <stdio.h> main() { printf("hello, world"); } The Hello World Program in Java /* * File: HelloProgram.java * ----------------------- * This program displays "hello, world" on the screen. * It is inspired by the first program in Brian * Kernighan and Dennis Ritchie's classic book, * The C Programming Language. */ import acm.graphics.*; import acm.program.*; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel( "hello, world", 100, 75 ) ); } } /* * File: HelloProgram.java * ----------------------- * This program displays "hello, world" on the screen. * It is inspired by the first program in Brian * Kernighan and Dennis Ritchie's classic book, * The C Programming Language. */ The simple Hello World example illustrates several features that are common to the programs you will see in this book. This first few lines (everything between /* and */ ) are an example of a comment , which is intended for human readers. import acm.graphics.*; import acm.program.*; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel( "hello, world", 100, 75 ) ); } } The next two lines are the imports , which indicate what library packages the program uses. The last few lines in the file define the HelloProgram class, which the extends keyword identifies as a GraphicsProgram ....
View Full Document

This note was uploaded on 10/27/2009 for the course COMP SCI 1MD3 taught by Professor Various during the Winter '07 term at McMaster University.

Page1 / 23

02-ProgrammingByExample - The Art and Science of An...

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

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