


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Chapter 2âProgramming by Example The Art and Science of An Introduction
C H A P T E R 2 Example is always more efficacious than precept. âSamuel Johnson, Rasselas, 1759
(adapted from slides by Eric Roberts)
1.1 Getting Started language is to write programs in it.^ The only way to learn a new programming 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"); }
import acm.graphics.; import acm.program.; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel( "hello, world", 100, 75 ) ); } }
import acm.graphics.; import acm.program.; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel("hello, world", 100, 75) ); } } HelloProgram hello, world import acm.graphics.; import acm.program.; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel("hello, world", 100, 75) ); } }
I will be glad to indulge both of you, if you will first oblige me, by telling me the meaning of these strange expressions, âholismâ and âreductionismâ. Achilles: Crab: Holism is the most natural thing in the world to grasp. Itâs simply the belief that âthe whole is greater than the sum of its partsâ. No one in his right mind could reject holism. Anteater: Reductionism is the most natural thing in the world to grasp. Itâs simply the belief that âa whole can be understood completely if you understand its parts, and the nature of their âsumââ. No one in her left brain could reject reductionism.
import acm.program.*; public class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); } }
import acm.program.*; public class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); } }
Add2Integers class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); } } n1 n2 total This program adds two numbers. Enter n2: The total is 42.
25 class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); } } n1 n2 total 17 25 42 Enter n1: 17
The structure of Javaâs class hierarchy resembles the biological classification scheme introduced by Scandinavian botanist Carl Linnaeus in the 18th century. Linnaeusâs contribution was to recognize that organisms fit into a hierarchical classification scheme in which the placement of individual species reflects anatomical similarities. Carl Linnaeus (1707â1778)
Living Things Plants Animals Fungi Annelida Brachiopoda Arthropoda Mollusca Chordata Crustacea Insecta Arachnida Hymenoptera Formicidae Iridomyrmex purpureus Kingdom Phylum Order Class Family Genus Species Classification of the red ant Iridomyrmex purpureus Every red ant is also an animal, an arthropod, and an insect, as well as the other superclasses in the chain. Note that there can be many individual red ants, each of which is an instance of the same basic class.
Applet JApplet Program ConsoleProgram DialogProgram GraphicsProgram Java class hierarchies are similar to the biological class hierarchy from the previous slide. This diagram, for example, shows the hierarchy formed by the classes in the acm.program package. Every ConsoleProgram is also a Program, a JApplet, and an Applet. That means that every ConsoleProgram can run as an applet on the web.
object .setColor( color ) Sets the color of the object to the specified color constant. object .setLocation( x , y ) Changes the location of the object to the point ( x , y ). object .move( dx , dy ) Moves the object on the screen by adding dx and dy to its current coordinates.
new GLabel( text , x , y ) Creates a label containing the specified text that begins at the point ( x , y ).
label .setFont( font ) Sets the font used to display the label as specified by the font string.
" family - style - size "
new GRect( x , y , width , height ) Creates a rectangle whose upper left corner is at ( x , y ) of the specified size. new GOval( x , y , width , height ) Creates an oval that fits inside the rectangle with the same dimensions.
object .setFilled( fill ) If fill is true, fills in the interior of the object; if false, shows only the outline. object .setFillColor( color ) Sets the color used to fill the interior, which can be different from the border. new GLine( x 0 , y 0 , x 1 , y 1 ) Creates a line extending from ( x 0 , y 0 ) to ( x 1 , y 1 ).
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.setFillColor(Color.GREEN); add(oval); } } GRectPlusGOval rect oval 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.setFillColor(Color.GREEN); add(oval); } } rect oval skip simulation