{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

02-ProgrammingByExample - TheArtandScienceof CHAPTER 2...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full Document Right Arrow Icon
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"); }
Background image of page 2
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 ) ); } } 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.program.*; 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 . A class definition in Java typically contains a series of entries . This example has one entry, which is a method called run . A Java method consists of a series of statements . Here, the only statement is a call to add , which adds an object to the display. The object to be added is indicated by supplying an argument to the add method. Here, the argument is a new GLabel object. In Java, objects are created by using a constructor, which consists of the keyword new followed by the class name and arguments.
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}