Programming by Example - PowerPoint PPT Presentation

About This Presentation
Title:

Programming by Example

Description:

Main class name must be the same as file name ... msg.setFont('Helvetica-24'); msg.setColor(Color.RED); add(msg); hello, world. GRect and GOval ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 17
Provided by: sanzhe
Category:

less

Transcript and Presenter's Notes

Title: Programming by Example


1
Chapter 2
  • Programming by Example

2
A Holistic Perspective
  • Three sections separated by blank lines.
  • Program comment
  • File FileName.java
  • --------------------------
  • What the program does
  • Imports
  • Library packages
  • Main class

3
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
  • /

4
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

5
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).

6
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

7
Out put
75
Hello, world
100
Hello, world
8
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
    .)

9
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?

10
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

11
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

12
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?

13
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

14
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.

15
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)

16
  • position ( 100, 50)
  • width 125 height 60 unit pixel
Write a Comment
User Comments (0)
About PowerShow.com