Copyright © 2005-2007 William C. BentonChapter 21Copyright © 2005-2007 William C. BentonWe have some idea of what a program is and how we go about the activity of programming.2Copyright © 2005-2007 William C. Benton3image credit: Google MapsLet’s take a step back and look at the big picture.Copyright © 2005-2007 William C. BentonHow do we write programs to solve problems and accomplish tasks?4Copyright © 2005-2007 William C. BentonJava is an “object-oriented” language.5Copyright © 2005-2007 William C. Benton(This means that we define programs in terms of “objects” and their interactions.)6123456Copyright © 2005-2007 William C. Benton7•Idea: identify which “objects” are involved in a problem domain and how they might interact•Example: if your problem is “I need to manage a bank,” the objects involved are accounts, customers, currency, etc.•Then, write a program by defining the objects, defining their interactions, and letting the objects do the workCopyright © 2005-2007 William C. BentonObjects correspond to problem-domain entities and interact with one another.8Copyright © 2005-2007 William C. BentonA class is a template for a kind of object.9(We say that objects are instances of classes.You can also think of classes as concepts.)Copyright © 2005-2007 William C. BentonExamples10Class (or concept)Object (or instance)checking accountmy checking accountpositive integer42buildingthe computer sciences buildingrectangleCopyright © 2005-2007 William C. Benton11-earnMoney(int)-spendMoney(int)-getBalance()-name-bankBalanceCartoonPlutocrat(all trademarks are property of their respective owners)Objects are collections of data and sets of operationsCopyright © 2005-2007 William C. Benton12-earnMoney(int)-spendMoney(int)-getBalance()-name-bankBalanceCartoonPlutocratMutator methods modify the data of an objectAccessor methods inspect the data of an object789101112Copyright © 2005-2007 William C. BentonYour programs will create and manipulate objects to model problems13Copyright © 2005-2007 William C. Benton14Let’s look at our simple program in greater detailCopyright © 2005-2007 William C. Benton15public class Hello { public static void main(String [] args) { String greeting = "Hello, world!"; String yellingGreeting = greeting.toUpperCase(); System.out.println(greeting); System.out.println(yellingGreeting); }}Classes, methods, and statements comprise the anatomy of a programCopyright © 2005-2007 William C. Benton16public class Hello {} public static void main(String [] args) { } String greeting = "Hello, world!"; String yellingGreeting = greeting.toUpperCase(); System.out.println(greeting); System.out.println(yellingGreeting);Classes are “units” of code containing methodsMethods are sequences of simple instructionsStatements are like sentences: single, imperative instructionsCopyright © 2005-2007 William C. BentonLet’s talk about statements a bit.17Copyright © 2005-2007 William C. BentonStatements are made up of identifiers and operators.18String greeting = "Hello, world!";131415161718Copyright © 2005-2007 William C. Benton(We’ll talk more about identifiers later)19Copyright © 2005-2007 William C. BentonIdentifiers provide names for “things in your program”20Copyright © 2005-2007 William C. BentonIdentifiers are case-sensitive, must follow rules and should follow conventions21Copyright © 2005-2007 William C. BentonIdentifier rules make it possible for the Java compiler to understand your program22Naming conventions make it possible for future programmers to understand your program!Copyright © 2005-2007 William C. BentonRule #1 of maintenance programming: At some point, you will be the “future programmer” for some code you wrote. Make it easy on yourself.23Copyright © 2005-2007 William C. BentonIdentifiers must follow rules•Can be made up strictly of letters, digits, and the underscore character (_). May not contain spaces or other characters.•The first letter must not be a digit•Identifiers are case-sensitive•That is, Fred is not the same as fred•Finally, reserved words may not be used as identifiers24192021222324Copyright © 2005-2007 William C. BentonReserved words•Some names have been reserved by the Java language•examples: public, static, void, class•Eclipse will highlight these by making them purple•You can’t use these names for identifiers25Copyright © 2005-2007 William C. BentonExamples•Legal•fred, barney, FRED, f123, fredFlintstone, __why_s0_many_underscores•Not legal•class, 2legit2quit, blah!, dot.com, #$!@26Copyright © 2005-2007 William C. BentonIdentifiers provide names for “things in your program,” like variables, classes, and methods.27(We’ve talked about classes a bit already, and we’ll talk about methods soon.)Copyright © 2005-2007 William C. BentonNaming conventions give other programmers a good idea of what sort of thing an identifier is naming!28Copyright © 2005-2007 William C. Benton•Classes: nouns. The first letter of each word is capitalized. •examples: String, PrintStream•Variables: nouns. The first letter of the first word is lowercase; the first letter of subsequent words is capitalized. •examples: greeting, highScore•Methods: verbs, capitalized as variables.•examples: reset(), crashMyComputer()29Naming conventionsCopyright © 2005-2007 William C. BentonClasses, methods, and statements comprise the anatomy of a program30public class Hello { public static void main(String [] args) { String greeting = "Hello, world!"; String yellingGreeting = greeting.toUpperCase(); System.out.println(greeting); System.out.println(yellingGreeting); }}252627282930Copyright © 2005-2007 William C. BentonStatements31•We know that a statement consists of identifiers, operators and literals.•We also know that a statement is terminated by a semicolon, just like a sentence in English ends with a period, exclamation point, or question mark.•But what do some of the statements we’ve seen actually do?Copyright © 2005-2007 William C. BentonThis statement sends a message to an object called System.out32System.out.println(greeting);(Objects can communicate with other objects by sending messages.)Copyright © 2005-2007 William C. BentonThis statement declares a variable, or a named place to store a value33String greeting = "Hello, world!";Copyright © 2005-2007 William C. BentonRead this as ‘greeting
View Full Document