chapter 2 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 2 PowerPoint Presentation
Download Presentation
Chapter 2

Loading in 2 Seconds...

play fullscreen
1 / 16

Chapter 2 - PowerPoint PPT Presentation


  • 83 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 2' - delu


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 2

Chapter 2

Programming by Example

a holistic perspective
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
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
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
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
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
slide7

Out put:

75

Hello, world

100

Hello, world

add two integers
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
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
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
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
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
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
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
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);

}

}

slide16

position: ( 100, 50)

width: 125 height: 60 unit: pixel