Java Programming: Chapter Two - Graphical Programs and Object-Oriented Programming, Study notes of Computer Science

A part of the course materials for a java programming class, specifically focusing on chapter two. The chapter covers programming by example, graphical programs, parts of a program, programming perspectives, and classes and objects. Students are expected to read the textbook, attend lab sessions, and complete assignments. How to create and send messages to graphical objects, the java coordinate system, and the difference between setcolor() and setfillcolor() methods for grects and govals.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-3ea
koofers-user-3ea 🇺🇸

8 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mat 2170
Chapter Two: Programming by Example
Java Programming
Spring 2009
Student Responsibilities
IReading: Textbook, Chapter 2
ILab 1 submissions (printout, electronic) & web publishing
ILab 2, Prelab (due at beginning of Lab 2, Thursday), & Postlab
(due at beginning of Lab 3, next week)
IAttendance
Chapter Two Overview
Programming by Example
2.6 Graphical programs (needed for Lab 2, so moved up)
2.1 Parts of a program
2.2 Programming Perspectives
2.3 Add2Integers
2.4 Programming idioms and patterns
2.5 Classes and objects
2.6 Graphical Programs
IThe GraphicsProgram class makes it possible to create simple
pictures on the screen.
IThe conceptual model is that of a collage composed of objects
on a canvas or a felt board.
IRunning a GraphicsProgram creates a window that serves as
the background canvas for the collage.
IYou create picture by creating graphical objects of various
kinds, and then adding those objects to the canvas.
IWe will be learning how to work with labels (textual graphics),
rectangles, ovals, and lines using the classes GLabel,GRect,
GOval, and GLine.
IThe complete set of graphics classes is discussed in Chapter 9.
GLabel Objects Unnamed vs Named
Create and Send Directly to Graphics Window:
public void run()
{
add(new GLabel("Hello, World!", 100, 75));
}
VS
Declare a Named Object (–MyLabel–) of type GLabel,
which is then sent to Graphics Window:
public void run()
{
GLabel MyLabel = new GLabel("Hello, World!", 100, 75);
add(MyLabel);
}
Sending Messages to Objects
IWe may wish to change the appearance (color) or location
(position) of a graphical object after it’s been created.
IIn object–oriented languages such as Java, these changes are
the responsibility of the object.
IThus, to change the color of an object, you send a message to
it asking it to change color.
IIn order to send a message to an object, it must have been
declared with a name as the GLabel MyLabel was on the
last slide.
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Chapter Two - Graphical Programs and Object-Oriented Programming and more Study notes Computer Science in PDF only on Docsity!

Mat 2170

Chapter Two: Programming by Example

Java Programming

Spring 2009

Student Responsibilities

I (^) Reading: Textbook, Chapter 2

I (^) Lab 1 submissions (printout, electronic) & web publishing

I (^) Lab 2, Prelab (due at beginning of Lab 2, Thursday), & Postlab (due at beginning of Lab 3, next week)

I (^) Attendance

Chapter Two Overview

Programming by Example

2.6 Graphical programs (needed for Lab 2, so moved up)

2.1 Parts of a program

2.2 Programming Perspectives

2.3 Add2Integers

2.4 Programming idioms and patterns

2.5 Classes and objects

2.6 Graphical Programs

I (^) The GraphicsProgram class makes it possible to create simple pictures on the screen.

I (^) The conceptual model is that of a collage composed of objects on a canvas or a felt board. I (^) Running a GraphicsProgram creates a window that serves as the background canvas for the collage.

I (^) You create picture by creating graphical objects of various kinds, and then adding those objects to the canvas. I (^) We will be learning how to work with labels (textual graphics), rectangles, ovals, and lines using the classes GLabel, GRect, GOval, and GLine. I (^) The complete set of graphics classes is discussed in Chapter 9.

GLabel Objects — Unnamed vs Named

Create and Send Directly to Graphics Window:

public void run() { add(new GLabel("Hello, World!", 100, 75)); }

— VS —

Declare a Named Object (–MyLabel–) of type GLabel, which is then sent to Graphics Window:

public void run() { GLabel MyLabel = new GLabel("Hello, World!", 100, 75); add(MyLabel); }

Sending Messages to Objects

I (^) We may wish to change the appearance (color) or location (position) of a graphical object after it’s been created.

I (^) In object–oriented languages such as Java, these changes are the responsibility of the object.

I Thus, to change the color of an object, you send a message to it asking it to change color.

I (^) In order to send a message to an object, it must have been declared with a name — as the GLabel MyLabel was on the last slide.

Sending Messages to Objects

I (^) To send a message to an object, Java uses the following syntax:

receiver.methodName(arguments); where: I (^) receiver is the (named) object to which the message is directed I (^) methodName identifies which message is sent I (^) arguments is a list of values used to specify any other information associated with the message

Sending Messages to a GLabel

This program illustrates sending a message to an object. Note that the label doesn’t appear until it is added to the canvas.

public class HelloProgram extends GraphicsProgram { public void run() { GLabel MyLabel = new GLabel("Hello",100,75); MyLabel.setFont("SansSerif-36"); MyLabel.setColor(Color.RED); add(MyLabel); } }

contents of MyLabel: (^) “Hello”

The Java Coordinate System

I (^) Positions and distances in a graphics program are measured in terms of pixels, which are the individual dots that cover the screen.

I (^) Unlike traditional mathematics, Java defines the origin of the coordinate system to be the upper left corner of the window.

I (^) Values for the x coordinate increase from left to right.

I (^) Values for the y coordinate increase from top to bottom.

I (^) Creating a GLabel at a particular x and y position means that the baseline of the first character in the label appears at that point — i.e., the (x, y) coordinate is for the lower left corner of the label.

The GObject Hierarchy

The classes that represent graphical objects form a hierarchy, part of which looks like this:

GObject

GLabel GRect GOval GLine

I (^) Operations are defined at each level of the hierarchy. I (^) Operations that apply to all graphical objects are specified at the GObject level — where they are inherited by each subclass. I (^) Operations that apply to a particular subclass are specified as part of the definition of that class.

Operations on the GObject Class

The following operations apply to all GObjects

object.setColor(color)

Sets the color of the object to the specified color constant

object.setLocation(x, y)

Changes the location of the ob- ject to the point (x, y)

object.move(dx, dy)

Moves the object on the screen by adding the displacements dx and dy to its current coordinates

The java.awt Package Standard Color Names

Color.BLACK Color.RED Color.DARK GRAY Color.YELLOW Color.GRAY Color.GREEN Color.LIGHT GRAY Color.CYAN

Color.WHITE Color.BLUE Color.MAGENTA Color.ORANGE Color.PINK

In order to use these colors, you will need to add: import java.awt.*; to your program.

I (^) What would we need to change in the last program to have the rectangle be drawn after the oval? What would happen in that case?

I (^) What would we need to change if we wanted a cyan oval with a blue border?

I (^) What is the difference between the setColor() and the setFillColor() methods for GRects and GOvals?

I (^) What GraphicsProgram method displays a GObject in the graphics window?

I (^) Suppose we’d like to draw a stick figure in a graphics window. How could we go about determining the coordinates of the parts?

The Stick Figure

// body add(new GLine(100, 100, 100, 150)); // legs add(new GLine(100, 150, 75, 175)); add(new GLine(100, 150, 125,175)); // arms add(new GLine(75,120,125,120)); // head add(new GOval(90, 80, 20, 20)); // eyes add(new GOval(93,86,3,3)); add(new GOval(103,86,3,3)); //nose add(new GLine(99,90,99,94)); add(new GLine(100,90,100,94)); add(new GLine(101,90,101,94)); add(new GLine(101,90,101,94)); // mouth add(new GLine(94,96,106,96));

What if you would like to make a Blue Man?

2.1 Parts of a Java Program

/* file: HelloProgram.java / import acm.graphics.; import acm.program.*;

public class HelloProgram extends GraphicsProgram { public void run() { // create and display a greeting add(new GLabel("hello, world", 100, 75)); } }

I Header Comments, line comments (for humans) I (^) Imports: Allows use of shorter names for library classes

I (^) The main class: HelloProgram

Comments

I Comments are for humans; computer ignores them

I (^) Block comments: use /* and */ around text

I (^) Block comments can extend over several lines

I (^) Line comment: starts with //

I (^) Line comment extends only to end of line

A Program to Add Two Numbers — Dialog

/* file: Add2Integers.java / import acm.program.;

public class Add2Integers extends DialogProgram {

public void run() { println("This program adds two integers"); int n1 = readInt("Enter first number: "); int n2 = readInt("Enter second number: "); int total = n1 + n2; println("The total is " + total + "."); } }

A Program to Add Two Numbers — Console

/* file: Add2Integers.java / import acm.program.;

public class Add2Integers extends ConsoleProgram {

public void run() { println("This program adds two integers"); int n1 = readInt("Enter first number: "); int n2 = readInt("Enter second number: "); int total = n1 + n2; println("The total is " + total + "."); } }

A Program to Add Two Numbers — Floating Point

/* file: Add2Doubles.java / import acm.program.;

public class Add2Doubles extends ConsoleProgram {

public void run() { println("This program adds two floating " + "point values"); double n1 = readDouble("Enter first number: "); double n2 = readDouble("Enter second number: "); double total = n1 + n2; println("The total is " + total + "."); } }

Two Perspectives on the Programming Process

I (^) Reductionism: a whole can be understood completely if you understand its parts, and the nature of their ’sum’.

When programming, you need to understand the language (such as Java) well before you can write correct, efficient programs.

I (^) Holism: the whole is greater than the sum of its parts.

When programming, you need to be able to see the logical “big picture” and create the underlying logic (algorithm) before you can write correct, efficient programs.

2.4 Programming Idioms and Patterns

— A Holistic Approach to Programming — I (^) There are a variety of common operations I (^) A standard solution strategy

I (^) The code that implements such a solution strategy is called a programming idiom or programming pattern I (^) Learning to use these patterns saves time I (^) For example, it is helpful to think of a statement like:

int n1 = readInt("Enter first number: ");

as a holistic pattern to read an integer from the user:

int variable = readInt(‘‘prompt’’);

I (^) Then, switching to floating point values is much easier:

double variable = readDouble(‘‘prompt’’);

I (^) The concept or pattern is the same

I (^) The pattern serves as a template

I (^) Don’t have to remember details

I (^) Recognize pattern and apply standard solution strategy

I (^) This approach is scalable

2.5 Classes and Objects

I (^) Java programs are written as collections of classes, which serve as templates for individual objects.

I Each object is an instance of a particular class.

I (^) Classes can serve as a pattern for many different objects.

I (^) Java classes form hierarchies — like a family tree.

I (^) Except for the class named Object at the top of the hierarchy, every Java class is a subclass (derived) of some other superclass (parent class).

I (^) A class can have many subclasses, but only one superclass.