160 likes | 256 Views
Chapter 2. Programming by Example. A Holistic Perspective. Three sections separated by blank lines. Program comment File: FileName.java -------------------------- What the program does Imports Library packages Main class. Program Comment Format. /* * File: HelloProgram.java
E N D
Chapter 2 Programming by Example
A Holistic Perspective Three sections separated by blank lines. • Program comment File: FileName.java -------------------------- What the program does • Imports Library packages • Main class
Program Comment Format /* * File: HelloProgram.java * ----------------------------------- * Describe what this program does. Inputs and outputs * if any. The user can run this program without reading * the code. * * Author: E. Roberts, 123456 */
Main Class Format public class HelloProgram extends GraphicsProgram { public void run() { add(new GLabel(“hello, world”, 100, 75)); } } • Indentations (3 spaces each level) • Positions of “{“ and “}” • ClassName and methodName
Main Class public class HelloProgram extends GraphicsProgram { body of the class definition } • Key words: public, class, extends • Main class name must be the same as file name • Main class HelloProgram is a subclass of GraphicsProgram, defined in acm.program package (must be imported).
Body of Main Class public void run() { add(new GLabel(“hello, world”, 100, 75)); } • run: method name • run(): no arguments passed to run • add: method defined in acm.graphics, one argument (an object of GLabel class) • GLabel: a class defined in acm.graphics, requires three arguments
Out put: 75 Hello, world 100 Hello, world
Add Two Integers /* * File: Add2Integers.java * ------------------------------- * This program prompts for two integers, adds them, and prints their sum. * * Author: E. Roberts, 123456 */ Import acm.program.*; public class Add2Integers extends ConsoleProgram { public void run() { println(“This program adds two integers.”); int n1 = readInt(“Enter n1: “); int n2 = readInt(“Enter n2: “); int total = n1 + n2; println(“The total is “ + total + “.”); } }
Questions • Add2Integers is a subclass of what class? • Where is ConsoleProgram (handles console input and output) class defined? • Where are the methods println and readInt defined? • run() is a method in which class?
Console Output • println: displays the argument string then returns to next line println(“This program adds two integers.”) Displays: This program adds two integers. println(“The total is “ + total + “.”) Displays, when the value of total is 42, The total is 42. • +: concatenation operator
Console Input • readInt: displays argument string, prompts an integer entered from keyboard, returns the entered integer. readInt(“Enter n1: “) Displays Enter n1: prompts an integer from keyboard, if 17 is typed, Enter n1: 17 returns 17
HelloProgram Revisited Split add(new GLabel(“hello, world”, 100, 75)); Into GLabel msg = new GLabel(“hello, world”, 100, 75); add(msg); Why? Control font, size, color, etc, in between. How?
Sending Messages Setting font-size and color public void run() { GLabel msg = new GLabel(“hello, world”, 100, 75); msg.setFont(“Helvetica-24”); msg.setColor(Color.RED); add(msg); } hello, world
GRect and GOval /* * File: GRectPlusGOval.java * ------------------------------------ * This program draws a red rectangle and a black * outlined and green filled oval */ import acm.graphics.*; import acm.program.*; import java.awt.*;
GRect and GOval (cont.) public class GRectPlusGOval extends GraphicsProgram { public void run() { GRect rect = new GRect(100, 50, 125, 60); rect.setFilled(true); rect.setColor(Color.RED); add(rect); GOval oval = new GOval(100, 50, 125, 60); oval.setFilled(true); oval.setColor(Color.GREEN); add(oval); } }
position: ( 100, 50) width: 125 height: 60 unit: pixel