100% found this document useful (2 votes)
62 views

Introduction to Programming with Greenfoot Object Oriented Programming in Java with Games and Simulations 1st Edition Michael Kolling - The full ebook version is available, download now to explore

The document promotes the ebook 'Introduction to Programming with Greenfoot: Object Oriented Programming in Java with Games and Simulations' by Michael Kolling, available for download at ebookultra.com. It includes links to additional recommended programming-related ebooks and outlines the contents and structure of the Greenfoot programming environment, which is designed to teach object-oriented programming through interactive games and simulations. Supplementary materials and resources for the book are also provided through a companion website.

Uploaded by

hafssividesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
62 views

Introduction to Programming with Greenfoot Object Oriented Programming in Java with Games and Simulations 1st Edition Michael Kolling - The full ebook version is available, download now to explore

The document promotes the ebook 'Introduction to Programming with Greenfoot: Object Oriented Programming in Java with Games and Simulations' by Michael Kolling, available for download at ebookultra.com. It includes links to additional recommended programming-related ebooks and outlines the contents and structure of the Greenfoot programming environment, which is designed to teach object-oriented programming through interactive games and simulations. Supplementary materials and resources for the book are also provided through a companion website.

Uploaded by

hafssividesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Download the full version and explore a variety of ebooks

or textbooks at https://ebookultra.com

Introduction to Programming with Greenfoot Object


Oriented Programming in Java with Games and
Simulations 1st Edition Michael Kolling

_____ Follow the link below to get your download now _____

https://ebookultra.com/download/introduction-to-programming-
with-greenfoot-object-oriented-programming-in-java-with-
games-and-simulations-1st-edition-michael-kolling/

Access ebookultra.com now to download high-quality


ebooks or textbooks
Here are some recommended products for you. Click the link to
download, or explore more at ebookultra.com

An Introduction to Object Oriented Programming with Visual


Basic NET 1st Edition Dan Clark

https://ebookultra.com/download/an-introduction-to-object-oriented-
programming-with-visual-basic-net-1st-edition-dan-clark/

Introduction to Programming with Java 3rd Edition John


Dean

https://ebookultra.com/download/introduction-to-programming-with-
java-3rd-edition-john-dean/

Java the UML way integrating object oriented design and


programming Else Lervik

https://ebookultra.com/download/java-the-uml-way-integrating-object-
oriented-design-and-programming-else-lervik/

Applying UML and Patterns An Introduction To Object


Oriented Programming 2Ed Edition Larmen

https://ebookultra.com/download/applying-uml-and-patterns-an-
introduction-to-object-oriented-programming-2ed-edition-larmen/
Object Oriented Design with UML and Java 1st Edition
Kenneth Barclay

https://ebookultra.com/download/object-oriented-design-with-uml-and-
java-1st-edition-kenneth-barclay/

Object Oriented Programming in C 4th Edition E.


Balagurusamy

https://ebookultra.com/download/object-oriented-programming-in-c-4th-
edition-e-balagurusamy/

Java Methods Object Oriented Programming and Data


Structures Third AP 3rd AP Edition Maria Litvin

https://ebookultra.com/download/java-methods-object-oriented-
programming-and-data-structures-third-ap-3rd-ap-edition-maria-litvin/

Beginning C Object Oriented Programming 2nd Edition Dan


Clark

https://ebookultra.com/download/beginning-c-object-oriented-
programming-2nd-edition-dan-clark/

Practical Database Programming with Java 1st Edition Ying


Bai

https://ebookultra.com/download/practical-database-programming-with-
java-1st-edition-ying-bai/
Introduction to Programming with Greenfoot Object
Oriented Programming in Java with Games and
Simulations 1st Edition Michael Kolling Digital Instant
Download
Author(s): Michael Kolling
ISBN(s): 9780136037538, 0136037534
Edition: 1
File Details: PDF, 5.44 MB
Year: 2009
Language: english
Introduction to Programming with Greenfoot

Companion Website
Additional material and resources for this book can be found at
http://www.greenfoot.org/book/

For students:
G The Greenfoot software

G The scenarios discussed in this book

G The Greenfoot Gallery—a scenario showcase

G Tutorial videos

G A discussion forum

G Technical support

For teachers:
G A teacher discussion forum

G Additional exercises related to the book

G The “Green Room” containing worksheets and other teaching

resources
This page intentionally left blank
Introduction to
Programming with
Greenfoot
Object-Oriented Programming in Java™
with Games and Simulations

Michael Kölling

Prentice Hall
Upper Saddle River • Boston • Columbus • San Francisco • New York
Indianapolis • London • Toronto • Sydney • Singapore • Tokyo • Montreal • Dubai
Madrid • Hong Kong • Mexico City • Munich • Paris • Amsterdam • Cape Town
Vice President and Editorial Director, ECS: Marcia J. Horton
Editor in Chief, CS: Michael Hirsch
Executive Editor: Tracy Dunkelberger
Assistant Editor: Melinda Haggerty
Editorial Assistant: Allison Michael
Director of Team-Based Project Management: Vince O’Brien
Senior Managing Editor: Scott Disanno
Production Liaison: Irwin Zucker
Production Editor: Shiny Rajesh, Integra
Senior Operations Specialist: Alan Fischer
Operation Specialist: Lisa McDowell
Marketing Manager: Erin Davis
Marketing Coordinator: Kathryn Ferranti
Art Director: Jayne Conte
Cover Designer: Bruce Kenselaar
Art Editor: Greg Dulles
Media Editor: Daniel Sandin
Composition/Full-Service Project Management: Integra

Copyright © 2010 by Pearson Higher Education. Upper Saddle River, New Jersey, 07458. All rights
reserved. Manufactured in the United States of America. This publication is protected by Copyright and
permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or
likewise. To obtain permission(s) to use materials from this work, please submit a written request to Pearson
Higher Education, Permissions Department, 1 Lake Street, Upper Saddle River, NJ 07458.

The author and publisher of this book have used their best efforts in preparing this book. These efforts
include the development, research, and testing of the theories and programs to determine their effectiveness.
The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs
or the documentation contained in this book. The author and publisher shall not be liable in any event for
incidental or consequential damages in connection with, or arising out of, the furnishing, performance,
or use of these programs.

Library of Congress Cataloging-in-Publication Data on File

10 9 8 7 6 5 4 3 2 1

ISBN-13: 978-0-13-603753-8
ISBN-10: 0-13-603753-4
To Krümel and Cracker—may their imagination never fade.
—mk

Education is not the filling of a pail, but the lighting of a fire.


—William Butler Yeats
This page intentionally left blank
Contents

List of scenarios discussed in this book xi


Acknowledgments xv

Introduction 1

Chapter 1 Getting to know Greenfoot 3


1.1 Getting started 3
1.2 Objects and classes 4
1.3 Interacting with objects 6
1.4 Return types 7
1.5 Parameters 8
1.6 Greenfoot execution 9
1.7 A second example 10
1.8 Understanding the class diagram 10
1.9 Playing with Asteroids 12
1.10 Source code 13
1.11 Summary 15

Chapter 2 The first program: Little Crab 16


2.1 The Little Crab scenario 16
2.2 Making the crab move 18
2.3 Turning 19
2.4 Dealing with screen edges 21
2.5 Summary of programming techniques 25

Chapter 3 Improving the Crab—more sophisticated programming 27


3.1 Adding random behavior 27
3.2 Adding worms 30
3.3 Eating worms 32
viii | Contents

3.4 Creating new methods 33


3.5 Adding a Lobster 36
3.6 Keyboard control 36
3.7 Ending the game 38
3.8 Adding sound 40
3.9 Summary of programming techniques 42

Chapter 4 Finishing the crab game 43


4.1 Adding objects automatically 43
4.2 Creating new objects 45
4.3 Animating images 46
4.4 Greenfoot images 47
4.5 Instance variables (fields) 48
4.6 Assignment 49
4.7 Using actor constructors 50
4.8 Alternating the images 52
4.9 The if/else statement 53
4.10 Counting worms 53
4.11 More ideas 55
4.12 Summary of programming techniques 56

Interlude 1 Sharing your scenarios 57


I1.1 Exporting your scenario 57
I1.2 Export to application 57
I1.3 Export to a web page 58
I1.4 Publishing on the Greenfoot Gallery 59

Chapter 5 Making music: An on-screen piano 61


5.1 Animating the key 62
5.2 Producing the sound 65
5.3 Abstraction: Creating multiple keys 66
5.4 Building the piano 68
5.5 Using loops: The while loop 69
5.6 Using arrays 72
5.7 Summary of programming techniques 76

Chapter 6 Interacting objects: Newton’s Lab 78


6.1 The starting point: Newton’s Lab 79
6.2 Helper classes: SmoothMover and Vector 80
Contents | ix

6.3 The existing Body class 83


6.4 First extension: Creating movement 85
6.5 Using Java library classes 86
6.6 Adding gravitational force 87
6.7 The List type 90
6.8 The for-each loop 91
6.9 Applying gravity 93
6.10 Trying it out 95
6.11 Gravity and music 97
6.12 Summary of programming techniques 99

Chapter 7 Collision detection: Asteroids 101


7.1 Investigation: What is there? 102
7.2 Painting stars 103
7.3 Turning 106
7.4 Flying forward 107
7.5 Colliding with asteroids 109
7.6 Casting 112
7.7 Adding fire power: The proton wave 115
7.8 Growing the wave 115
7.9 Interacting with objects in range 119
7.10 Further development 121
7.11 Summary of programming techniques 122

Interlude 2 The Greeps competition 123


I2.1 How to get started 124
I2.2 Programming your Greeps 125
I2.3 Running the competition 126
I2.4 Technicalities 126

Chapter 8 Creating images and sound 127


8.1 Preparation 127
8.2 Working with sound 129
8.3 Sound recording and editing 130
8.4 Sound file formats and file sizes 131
8.5 Working with images 133
8.6 Image files and file formats 133
8.7 Drawing images 135
8.8 Combining images files and dynamic drawing 137
8.9 Summary 139
x | Contents

Chapter 9 Simulations 141


9.1 Foxes and rabbits 142
9.2 Ants 145
9.3 Collecting food 146
9.4 Setting up the world 149
9.5 Adding pheromones 149
9.6 Path forming 152
9.7 Summary 152

Chapter 10 Additional scenario ideas 154


10.1 Marbles 154
10.2 Lifts 155
10.3 Boids 156
10.4 Circles 157
10.5 Explosion 158
10.6 Breakout 159
10.7 Platform jumper 160
10.8 Wave 161
10.9 Summary 162

Appendix
A Installing Greenfoot 163
B Greenfoot API 165
C Collision detection 169
D Some Java details 175

Index 185
List of scenarios discussed
in this book

Leaves and wombats (Chapter 1)


This is a simple example showing wombats moving around on screen, occasionally eating leaves.
The scenario has no specific purpose other than illustrating some important object-oriented
concepts and Greenfoot interactions.

Asteroids 1 (Chapter 1)
This is a simple version of a classic arcade game. You fly a spaceship through space and try to
avoid being hit by asteroids. At this stage, we only use the scenario to make some small changes
and illustrate some basic concepts.

Little Crab (Chapter 2)


This is our first full development. Starting from almost nothing, we develop a simple game
slowly, adding may things such as movement, keyboard control, sound, and many other elements
of typical games.

Piano (Chapter 5)
An on-screen piano that you can really play.

Newton’s Lab (Chapter 6)


Newton’s Lab is a simulation of the motion of stars and planets in space. Gravity plays a central
role here. We also make a variant of this that combines gravity with making music, ending up
with musical output triggered by objects under gravitational movement.

Asteroids 2 (Chapter 7)
We come back to the asteroids example from Chapter 2. This time, we investigate more fully
how to implement it.

Ants (Chapter 9)
A simulation of ant colonies searching for food, communicating via drops of pheromones left on
the ground.

The following scenarios are presented in Chapter 10 and selected aspects of them briefly
discussed. They are intended as inspiration for further projects.
Marbles
A simulation of a marble board game. Marbles have to be cleared of the board within a limited
number of moves. Contains simple physics.
xii | List of scenarios discussed in this book

Lifts
A start of a lift simulation. Incomplete at this stage—can be used as a start of a project.

Boids
A demo showing flocking behavior: A flock of birds flies across the screen, aiming to stick
together while avoiding obstacles.

Circles
Make patterns in different colors on the screen with moving circles.

Explosion
A demo of a more sophisticated explosion effect.

Breakout
This is the start of an implementation of the classic Breakout game. Very incomplete, but with
an interesting visual effect.

Platform jumper
A demo of a partial implementation of an ever-popular genre of games: platform jumpers.

Wave
This scenario is a simple demonstration of a physical effect: the propagation of a wave on
a string.
Preface | xiii

Preface
Greenfoot is a programming environment that can be used by individuals, in schools or in
introductory university courses to learn and teach the principles of programming. It is flexible
enough to be suitable for teenagers as well as older students.
Greenfoot supports the Java Programming Language, so students learn standard object-oriented
programming in Java. The environment is designed specifically to convey object-oriented con-
cepts and principles in a clean, easily accessible manner.
The Greenfoot environment makes creation of graphics and interaction easy. Students can
concentrate on modifying the application logic, and engage and experiment with objects.
Developing simulations and interactive games becomes easy, and feedback is immediate.
The environment is designed to quickly engage students who may have no prior interest or experi-
ence in programming. Achieving simple animation results is quick, sophisticated, professional
looking scenarios are possible.

Accessing Supplementary Materials


The learning aids and supplementary materials that are noted in the book can be accessed
through the Greenfoot Companion Web site or through the publisher’s Web site:
Companion Website: http://www.greenfoot.org/book/
Publisher’s Website: http://www.prenhall.com/kolling

Supplementary Materials Available for Students


The following supplements are available for students:
I The Greenfoot software
I The scenarios discussed in this book
I The Greenfoot Gallery—a scenario showcase
I Tutorial videos
I A discussion forum
I Technical support

Supplementary Materials Available for Instructors


The following supplements are available for instructors:
I A teacher discussion forum
I Additional exercises related to the book
I The “Green Room” containing worksheets and other teaching resources
For more details about Greenfoot and this book, please also see the Introduction following the
Acknowledgments.
This page intentionally left blank
Exploring the Variety of Random
Documents with Different Content
The Project Gutenberg eBook of My Rubaiyat
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: My Rubaiyat

Author: Sadakichi Hartmann

Release date: January 9, 2019 [eBook #58664]

Language: English

Credits: Produced by hekula03, Barry Abrahamsen, and the


Online
Distributed Proofreading Team at http://www.pgdp.net
(This
file was produced from images generously made
available
by The Internet Archive)

*** START OF THE PROJECT GUTENBERG EBOOK MY RUBAIYAT


***
SADAKICHI HARTMANN
MY RUBAIYAT

THIRD REVISED EDITION


SAN FRANCISCO
1916
To Dunbar Wright, a traveler among Men, who
“in his own way courts the sun and fashions Arcadia
of passing winds and flying clouds.”

Copyright, 1916, by Sadakichi Hartmann


INSTEAD OF A PREFACE:

William Marion Reedy,


St. Louis Mirror:

I will drop the mask and tell you the secret of my verses. You say
they impress you as being uneven and unfinished. I heartily agree
with you. As I have stated in my announcement to the public, a
poem of the scope and range of “My Rubaiyat” is never complete.
No doubt, it will undergo many changes within the next ten years. I
say ten years deliberately. You see, I possess the arrogance of
conviction. I believe it will survive, simply because it strikes a
popular chord, and attempts, no matter how vaguely, to reproduce a
broken melody that hums in every mind. Somebody else may
venture forth on similar paths and succeed to please even the
fastidious in rhyme. “My Rubaiyat” may be put on the back shelves.
Well, we will see. I look at my work with objective eyes. It is a mere
youngster now. It will grow and nobody will watch its growth with
keener appreciation than I myself. The number of verses will not
increase, but I sincerely hope that they will gain in clarity and
strength as well as in musical and pictorial wealth of expression.
As for versification, let me make this explanation. I chose the
eight syllable stanza on account of its terseness of expression. It is
least pliable to any rush and swing of rhythm, but most conducive to
the conveyance of fragmentary moods and thoughts. The omission
of rhyme I essayed for no other reason than its technical difficulty.
To make rhymeless lines read like a poem is the most laborious task
a songsmith can set himself. It is the vanity of the alien to show his
mastery over a language that was neither his father’s nor his
mother’s tongue. But I object to your statement that I disdain
rhythm. I have a vague suspicion that you really mean meter. My
meter is rough and wilful and subject to impurities, as for instance
counting the last two syllables in words like “happier” and “sunnier”
either as one or two, just as my fancy, or rather my appreciation of
rhythm, dictates. My rhythm changes constantly but it is palpable,
underneath as it were, at all times. I have some experience as a
reader (though elocutionists may shrug their shoulders at my style of
interpretation—let them shrug) and I have, whenever I write, the
habit of reading aloud the words as I put them down. Reading
means to get a certain sense and swing, color and sound in the
words as one utters them. If my verses contain this possibility of
aural gratification they cannot be utterly devoid of rhythm. No doubt
my sense of sound alliteration is foreign, unconsciously Oriental. I
feel a sound relation, no, even a rhyme suggestion in words like
“chance” and “spring,” “herd” and “feet” at the end of succeeding
stanzas. The alliteration of Japanese poets is much subtler (due to
the peculiarities of the language) than the word music of our Laniers
and Whitmans, although it is never conducted with the elaborate
precision of a Poe or Swinburne. It always remains fragmentary, it
rarely resembles full orchestration. Also my lines lack the merit of
contrapuntal structure. Yet they have one quality which is generally
overlooked. They possess pictorial harmony. My long and persistent
association with art makes me not only see but think things in
pictures. Pictures abound throughout “My Rubaiyat” for all who have
the mental pictorial vision to see them. Lines like “turn phantoms
with the colder morn” and “in a hilltown among roses” are as
concentrated as any image that can be found in a tanka (i.e.
Japanese short poem).
Critics may contend that pictorial suggestion per se, as the main
characteristic of a poem, does not conform to the accepted forms of
poetry. This objection is meaningless to me. Without the spirit of
innovation there would have been no incentive to write the poem.
Like the composers of the day I believe in the old ideals but in new
methods of expression.
My ambition was to write a simple poem which would appeal to
all; to chambermaids as well as cognoscenti, ordinary business men
as well as solitary artistic souls. Who will decide whether I have
succeeded or failed? Only the public at large. The poem, no doubt, is
too didactic for fragile aesthetics who glorify naught but evanescent
words, but it is surely no shortcoming to try to express thought.
Even exponents of the modern schools attempt this—occasionally.
The way of expression is a different matter. It is open to criticism.
But excuses that a critic knows nothing about a certain subject, and
yet at the same time deliberate pricks at this very thorn in the flesh
of his ignorance are sad to contemplate. Rhyme is surely out of
date. And the supposed lack of rhythm is merely imaginary. Would
you enjoy Japanese or Chinese music? Very likely not and yet they
contain as fine a rhythm and as musical a quality as any modern
composition. Only they are vaguer, subtle, different.
And on this difference hinges all logical and evasive argument.
The practical philosophy contained in “My Rubaiyat,” of course, can
be attacked for being non-moral or non-religious, but the technique
of the poem can be discussed only from one viewpoint.
Sincerely yours,
Sadakichi Hartmann.
MY RUBAIYAT
I.
What should we dream, what should we say,
On this drear day, in this sad clime!
In the garden the asters fade,
Smoke of weed-fires blurs the plain,
The hours pass with a sullen grace—
Can we be gay when skies are grey!
II.
Would joy prove a more steady guest,
In palm-girt, sunnier Southern lands,
Some lambient world of green and gold
Fanned by the charm of Orient lay!
’Tis vain delusion thus to think
That life will change with change of scene.
III.
Man cannot get away from facts—
Alas, stern duty looms supreme,
For certain things we must perform,
Obey the inward voices’ call.
Calm joyous days cannot be wooed
Unless our conscience is at peace.
IV.
Life is to most a weary task,
A ceaseless strife for daily bread,
We cannot act as we would like,
We cannot gain for what we strive.
To bear the burden cheerfully
Is all this earth allows to us.
V.
Our tired soul with faint forced smile
But rarely scales the loftier themes,
Fair Hafiz and Anacreon
Have they drunk, laughed and sung in vain!
Do grove and grange no longer yield
The idyls of Theocritus!
VI.
Was man once happier than now?
Who is there to tell the story
Of slaves or Cesars of the past?
Still our blood is stirred each spring,
Still books and music make us dream,
Why mourn the “snows of yesteryear?”
VII.
There were ever some more favored
Who care-free basked in fortune’s sun.
The rest did toil. And you and I?
We hear the same recurrent rhymes,
Like changing seasons, night and day,
We simply come, sojourn, and go.
VIII.
We enter the world unbidden,
Plod along roads as we know best.
One is born rich, the other poor,
Who knows what helps a mortal most.
Ere sleep we rub from our eyes
We are forever what we are.
IX.
The laughter of childhood is gone,
The toy castles we built are lost—
Can we redeem in future days
The disappointments of the past!
Our nursery songs will they change
Into jubilant songs of love!
X.
Light-headed youth, all smiles around
In dew-drenched gardens of spring morns
No heed takes of the dial’s stealth.
Youth wants to conquer—rule the spheres,
While the sun runs his ruthless course
And shadows begin to lengthen.
XI.
In open woods some summer night,
The sound of the wind in the leaves—
Two vagrant lovers hand in hand—
O’er treetops the errant moon.
Oh, this mad desire to possess!
To waste the soul on blood-red lips.
XII.
Sex is a power all cherish,
We worship it on bended knees,
Like old wine it yields the magic
Of oblivion and ecstasies,
The moments drift on golden clouds
To regions of the white beyond.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like