100 likes | 141 Views
Dive into the basics of Java coding with examples and exercises. Learn about objects, classes, variables, conditional code, and more in this comprehensive guide.
E N D
Java Coding Introduction Scott McElfresh SAMS 2004
Software Creation Cycle • Edit • Compile • Run • Test • Debug
Software Creation Cycle • Edit • word processor, text editor, Project Builder • Compile • javac programname.java • sh compile.sh • Run • java programname • sh run.sh • Test • make sure everything works as desired • Debug
Objects, Classes, and Behavior • In Java programming, items of interest are called objects • Objects that share common behavior are grouped into classes. • Once a class is defined in Java, objects of that class can be created. These are called instances. • Java has some classes pre-defined for us. • In this course, we will also use classes created by other programmers.
Messages • Requesting that an object do something (execute one of its behaviors) is called a sending a message, or calling a method. • To send a message, we must specify the object and the desired behavior. EG: • bob.goToStore(); • jane.goToStore(butter); // butter is a “parameter” • bill.howOldAreYou()
Variables • Variables in Java refer to specific objects. • Java needs a way to refer to the object, so requires an identifier. • Identifiers can have letters and numbers, and must start with a letter.
Conditional code • Some actions are only desirable under certain conditions. if (out of butter) mary.goToStore(butter) • If statements allow us to check the state of the world and make a decision about what to do. if (mary.outOfButter()) mary.goToStore(butter); else mary.goToFriendsHouse();
Comments andInternal Documentation • Comments are code for HUMANS to read, not the computer. • /* …. */ indicates comments between. • // indicates comments on the rest of the line • Project builder typically makes these green.
Items on screen in this project • For each item on your screen, you need to do 3 or 4 things: • Choose a name for the item. Declare this to Java. • Create the item and fill in details (eg. location size) • Add it to your display and tell Java how this item relates to other items on screen. • Tell Java what to do when this item is in focus and phone keys are pressed.