View
216
Download
2
Embed Size (px)
Citation preview
What does a program look like?
• An algorithm is a set of instructions designed to accomplish a specific goal
• For a temperature f in Fahrenheit• Subtract 32 from f.• Divide f by 1.8.• Display the value of f.• [Converts to Celsius]
• Don’t need to understand the goal to follow instructions
Programming Languages
• A language is a tool with which we tell a computer an algorithm
• Compilers translate from one computer language to another
• Each language has own advantages and disadvantages
The Java Language
• Widely Used
• Can be compiled on many computer systems
• Object Oriented (we’ll learn what this means later)
Event Driven Programming
• Old programs would start with all input at once and run until completion
• Event: An action, a mouse click, an item selected
• Using events made easy in Java
A First Program
import objectdraw.*;import java.awt.*;
public class TouchyWindow extends WindowController {public void onMousePress ( Location point ) {
new Text("I’m touched", 40, 50, canvas );}
public void onMouseRelease( Location point ) {canvas.clear();
}}
Some of the words
• Class– A set, collection, group, or configuration
containing members regarded as having certain attributes or traits in common. (American Heritage Dictionary)
– Very similar meaning in Java
Software Libraries
• Reuse code already written
• extends– Builds upon an already written class
• import– Allows the use of instructions written
elsewhere
Parts of a Class
• Body– Enclosed in the curly braces found on the line
“public class…”
• Methods:public void onMousePress ( Location point ) {
new Text( "I’m touched", 40, 50, canvas );
}
The Life of a Program
• Write the Program
• Translate the program into a language the computer understands
• Run the Program
Integrated Development Environments
• Eclipse– Professional Programming Tool
• TouchyWindow in Eclipse
Integrated Development Environments
• Eclipse and BlueJ:
– Available on Windows, MacOS, Unix
– Free and downloadable
Graphical Objects
new Text ( "I’m Touched", 40, 50, canvas );• new an instruction that tells we want to
construct a new object• Text the object we want to construct• (…) comma delineated “parameters” that tell
how to construct the object• ; semicolons are important too!
– every command semicolon terminated
Events
public void onMousePress( Location point )– Will run the code when mouse button is
pressed
• onMouseRelease:– Runs code when the buttons released
• Many more mouse event handling methods
The begin Method
public void begin(){
…
}
• begin method– Executed (run) exactly once for each program– Executed early in program’s life
Graphical Objects
• New FilledRect( 10, 20, 30, 40, canvas );– Creates a 30x40 rectangle– Upper left corner at (10,20)
• new FilledOval( 10, 20, 30, 40, canvas);– Imagine a 30x40 rectangle at (10,20)– Draws the largest oval that can fit inside the
imaginary rectangle