ch 5 syntax and stylegreenstein.com/mvhs/apcs/lessons/overheads/ch 5 syntax... · 2020. 8. 10. ·...
TRANSCRIPT
![Page 1: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/1.jpg)
Programming Syntax and Style
David GreensteinMonta Vista High School
![Page 2: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/2.jpg)
![Page 3: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/3.jpg)
Programming Language SyntaxAll have:
• Comments
• Programmer-defined Names
• Reserved Words
• Structure
![Page 4: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/4.jpg)
Assembly Code (Amiga 68K)
1950 1960 1970 1980 1990 2000 2010 2020
![Page 5: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/5.jpg)
FORTRAN (FORmula TRANslation)
COBOL
1950 1960 1970 1980 1990 2000 2010 2020
![Page 6: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/6.jpg)
LISP (LISt Processing)
1950 1960 1970 1980 1990 2000 2010 2020
![Page 7: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/7.jpg)
APL (A Programming Language)
1950 1960 1970 1980 1990 2000 2010 2020
![Page 8: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/8.jpg)
C Language
1950 1960 1970 1980 1990 2000 2010 2020
![Page 9: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/9.jpg)
Pascal ADA
1950 1960 1970 1980 1990 2000 2010 2020
![Page 10: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/10.jpg)
C++ Python
1950 1960 1970 1980 1990 2000 2010 2020
![Page 11: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/11.jpg)
Javascript
1950 1960 1970 1980 1990 2000 2010 2020
![Page 12: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/12.jpg)
Java Syntax and Style
![Page 13: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/13.jpg)
Syntax and Style on the Web
![Page 14: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/14.jpg)
Brace “{ }” Structurefor (int a = 1; a < 5)
if ( count > 0 ) count-- ;
else { reset = true; count = 100;
} {
even += odd; two = ! two;
}
• A pair of braces denote one “compound statement” (even if the braces contain a single statement).
• Statements inside braces are indented.
• Java braces are an example of “block structured” languages
Singlestatements
Eachare one
compoundstatement(braces)
![Page 15: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/15.jpg)
• In Java, a number of words are reserved for a special purpose.
• Reserved words use only lowercase letters.
• Reserved words include:primitive data types: int, double, char, boolean, etc.storage modifiers: public, private, static, final, etc.control statements: if, else, switch, while, for, etc.built-in constants: true, false, null
• There are about 50 reserved words total.
Reserved Words
![Page 16: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/16.jpg)
• The programmer gives names to his or her classes, methods, fields, and variables.
• In addition to reserved words, Java uses standard names for library packages and classes (APIs):
java.lang.String java.io.File java.awt.Graphics java.util.Scanner javax.swing.JFrame
• Careful! Check to be sure your class names do not conflict with Java’s API class names.
Programmer-Defined Names
![Page 17: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/17.jpg)
• Syntax: A name can include: upper- and lowercase letters (e.g. camelCase)digits (e.g. july4th)underscore characters (e.g. CARD_CNT)
• Syntax: A name cannot begin with a digit.4score, 365days
• Style: Names should be descriptive to improve readability.
YES: dealtCards; NO: dcException: names with limited roles, like names used in loops.for (int a = 1; a < 5; a++)
Programmer-Defined Names (cont.)
![Page 18: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/18.jpg)
• Class names The first letter is always uppercase.The name should use camel-case.The name describes the class and are “noun-like”.
• Method names The first letter is always lowercase.The name should use camel-case.The name should be “verb-like” describing the action.
Programmer-Defined Names (cont.)
e.g. MyClass, CardDeck, Yahtzee
e.g. setBackground, getText, moveForward
![Page 19: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/19.jpg)
• Field names The first letter is always lowercase.The name should use camel-case.The name should be “noun-like” describing the object.
• Constant names Use ALL_UPPER_CASE for constants separating words with underline characters.Java constant fields are declared private final.Make constants out of “magic” numbers. These are numbers that have a significant meaning in your code. For example, the number of cards in a hand.
Programmer-Defined Names (cont.)
e.g. TAX_RATE, CARD_HAND
e.g. count, windowWidth, htmlCanvas
![Page 20: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/20.jpg)
• In-line comment “//“ The Java compiler ignores anything on a line to the right of a double-slash “//“.
• Multiple-line comment delimiters “/*” and “*/“ The Java compiler ignores anything starting with “/*” and ending with “*/”.
Comments
/* This comment can last several lines. */
c += d; // comment is end of line // comment is the whole line
Great for commenting out largesegments of code for debugging!!!
![Page 21: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/21.jpg)
• JavaDoc comments for documenting surrounded by“/**“ and “*/“
The javadoc program automatically reads the comments from your source code and generates API-style HTML.javadoc annotations, denoted by a “@“, provide key information.
Comments (cont.)
/** This method calculates the determinant of a matrix. Precondition: matrix A must be square @param A matrix A @return the determinant of A */ public int determinant(Matrix A) { …
![Page 22: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/22.jpg)
• Always provide generous amounts of comments
Comments (cont.)
![Page 23: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/23.jpg)
• Always provide generous amounts of comments Comment important fields and local variables
Comments (cont.)
JPanel first; // First panel shown in the game int count; // Number of cards remaining
double a, b, c;
Important fields indesign needcomments
Helper variablesdo not needcomments
![Page 24: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/24.jpg)
• Always provide generous amounts of comments Comment important fields and local variablesComment each method
Comments (cont.)
/** Determines number of zombies near robot Precondition: robot is on field @param bot the robot @param loc robot’s current location @return number of nearby zombies */ public int zombieCounter(Robot bot, Location loc) {
Describe what isreturned
A concisedescription
List all of theinput parametersList preconditions
and postconditions
![Page 25: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/25.jpg)
• Always provide generous amounts of comments Comment important fields and local variablesComment each methodPut a header on each class
Comments (cont.)
/** Robot class * A friendly robot that walks around a Field * fighting zombies in its path. * @author Mr Greenstein * @since September 29, 2017 */ public class Robot implements Comparable { …
A concisedescription
Author (you)and date created
![Page 26: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/26.jpg)
• Always provide generous amounts of comments Comment important fields and local variablesComment each methodPut a header on each class
• Missing comments? Serious consequences!
Comments (cont.)
LOSE 10% OF YOUR PROJECT GRADE
![Page 27: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/27.jpg)
• Keep your coding under 80 characters per line.
Other Style Issues
if (item.getItemCost() > money) { System.out.printf("\nNO SALE: Not enough money to buy the " + “item\tChange: $%6.2f\n\n", money); return -1; }
if (item.getItemCost() > money) { System.out.printf("\nNO SALE: Not enough money to buy the item\tChange: $%6.2f\n\n", money); return -1; }
YES
NO
![Page 28: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/28.jpg)
• Keep your coding to < 80 characters per line.
• Names are descriptive, but short (< 15 chars).
Other Style Issues
int theLengthOfTheField;NO
YES int fieldLength;
![Page 29: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/29.jpg)
• Keep your coding to < 80 characters per line.
• Names are descriptive, but short (< 15 chars).
• Names of methods that return a boolean valuestart with “is” or “has”. For example:
Other Style Issues
public boolean isOverdrawn()public boolean hasCreditLeft()
![Page 30: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/30.jpg)
• The compiler catches syntax errors and generates error messages.
• Text in comments and literal strings within double quotes are excluded from syntax checking.
• Braces, brackets, and parentheses ({}, [], ()) can be on different lines
• Double quotes (“”) must be on the same line
Syntax Errors
![Page 31: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/31.jpg)
publc static int abs (int x) { fi (x < 0); { x = -x } return x;
public static int sqrt (int x) ...
Common Syntax Errors
Spelling
Extraneoussemicolon
Missingsemicolon
Missingclosingbrace
![Page 32: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/32.jpg)
• Pay attention to and check for:Matching braces { }, parentheses ( ), and brackets [ ].Missing or extraneous semicolons.Symbols used correctly for operators +, -, =, <, <=, ==, ++, &&, etc.Spelling is correct for reserved words, library names and programmer-defined names, with special attention to upper/lower case.
Syntax Errors (cont.)
![Page 33: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/33.jpg)
Which is a better style?public void act() {if(steps< sideLength&& canMove()){move(); steps++;}else{ turn();turn(); steps=0;}}
public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; } }
• Put spaces between lines.• Put spaces between words and operators.• Indent nested code.
Both compile
![Page 34: Ch 5 Syntax and Stylegreenstein.com/mvhs/apcs/Lessons/Overheads/Ch 5 Syntax... · 2020. 8. 10. · •The programmer gives names to his or her classes, methods, fields, and variables](https://reader036.vdocuments.mx/reader036/viewer/2022071414/610d25b8c7acd510507ce442/html5/thumbnails/34.jpg)
Questions?