Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

1 CS1001 Lecture Overview Java Programming Java Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
The Java Programming Language
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Outline Java program structure Basic program elements
Chapter 2: Introduction to C++.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Java Language and SW Dev’t
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Classes CS 21a: Introduction to Computing I First Semester,
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Style Guidelines. Why do we need style?  Good programming style helps promote the readability, clarity and comprehensibility of your code.
Chapter 2: Java Fundamentals
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
Data Types, Variables, and Arithmetic JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
More Sophisticated Behavior
Working with Java.
Java Methods /** * Chapter 5 */ Java Syntax and Style
Java Programming: From Problem Analysis to Program Design, 4e
Statements, Comments & Simple Arithmetic
IDENTIFIERS CSC 111.
Programming Vocabulary.
2.1 Parts of a C++ Program.
An overview of Java, Data types and variables
Chapter 1: Computer Systems
MSIS 655 Advanced Business Applications Programming
Java Methods /** * Chapter 5 */ Java Syntax and Style A & AB
Anatomy of a Java Program
Focus of the Course Object-Oriented Software Development
Chapter 2: Introduction to C++.
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM /** * Chapter 5 */

5-2 Objectives: l Learn to distinguish the required syntax from the conventional style l Learn when to use comments and how to mark them l Review reserved words and standard names l Learn the proper style for naming classes, methods, and variables l Learn to space and indent blocks of code

5-3 Comments l Comments are notes in plain English inserted in the source code. l Comments are used to: –document the program’s purpose, author, revision history, copyright notices, etc. –describe fields, constructors, and methods –explain obscure or unusual places in the code –temporarily “comment out” fragments of code

5-4 Formats for Comments l A “block” comment is placed between /* and */ marks: /* Exercise 5-2 for Java Methods Author: Miss Brace Date: 3/5/2010 Rev. 1.0 */ l A single-line comment goes from // to the end of the line: wt * = ; // Convert to kilograms

5-5 Documentation Comments l Used by the special utility program javadoc to automatically generate documentation in HTML format from the source code l Should precede a class, a method, or a field l Can use special javadoc – describes a parameter of a - describes the method’s return value

5-6 javadoc Comments (cont’d) /** * Returns total sales from all vendors; * sets totalSales * to 0. * total amount of sales from all vendors */ /** indicates a javadoc comment Can use HTML tags Common style

5-7 Reserved Words l In Java a number of words are reserved for a special purpose. l Reserved words use only lowercase letters. l 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 l There are about 50 reserved words total.

5-8 Programmer-Defined Names l In addition to reserved words, Java uses standard names for library packages and classes: String, Graphics, javax.swing, JApplet, JButton, ActionListener, java.awt l The programmer gives names to his or her classes, methods, fields, and variables.

5-9 Names (cont’d) l Syntax: A name can include: –upper- and lowercase letters –digits –underscore characters l Syntax: A name cannot begin with a digit. l Style: Names should be descriptive to improve readability.

5-10 Names (cont’d) l Programmers follow strict style conventions. l Style: Names of classes begin with an uppercase letter, subsequent words are capitalized: public class FallingCube l Style: Names of methods, fields, and variables begin with a lowercase letter, subsequent words are capitalized. private final int delay = 30; public void dropCube()

5-11 Names (cont’d) l Method names often sound like verbs: setBackground, getText, dropCube, start l Field names often sound like nouns: cube, delay, button, whiteboard l Constants sometimes use all caps: PI, CUBESIZE l It is OK to use standard short names for temporary “throwaway” variables: i, k, x, y, str

5-12 Syntax vs. Style l Syntax is part of the language. The compiler checks it. l Style is a convention widely adopted by software professionals. l The main purpose of style is to improve the readability of programs.

5-13 Syntax l The compiler catches syntax errors and generates error messages. l Text in comments and literal strings within double quotes are excluded from syntax checking. l Before compiling, carefully read your code a couple of times to check for syntax and logic errors.

5-14 Syntax (cont’d) l Pay attention to and check for: –matching braces { }, parentheses ( ), and brackets [ ] –missing and extraneous semicolons –correct symbols for operators +, -, =, <, <=, ==, ++, &&, etc. –correct spelling of reserved words, library names and programmer-defined names, including case

5-15 Syntax (cont’d) l Common syntax errors: Missing closing brace Public static int abs (int x) { If (x < 0); { x = - x } return x; public static int sign (int x)... Extraneous semicolon Spelling (p  P, if  If) Missing semicolon

5-16 Style l Arrange code on separate lines; insert blank lines between fragments of code. l Use comments. l Indent blocks within braces.

5-17 Style (cont’d) public boolean moveDown(){if (cubeY<6*cubeX) {cubeY+=yStep; return true;}else return false;} public boolean moveDown() { if (cubeY < 6 * cubeX) { cubeY += yStep; return true; } else { return false; } Before:After: Compiles fine!

5-18 Style (cont’d) public void fill (char ch) { int rows = grid.length, cols = grid[0].length; int r, c; for (r = 0; r < rows; r++) { for (c = 0; c < cols; c++) { grid[r][c] = ch; } Add blank lines for readability Add spaces around operators and after semicolons

5-19 Blocks, Indentation l Java code consists mainly of declarations and control statements. l Declarations describe objects and methods. l Control statement describe actions. l Declarations and control statements end with a semicolon. l No semicolon is used after a closing brace (except certain array declarations).

5-20 l Braces divide code into nested blocks. l A block in braces indicates a number of statements that form one compound statement. l Statements inside a block are indented, usually by two spaces or one tab. Blocks, Indentation (cont’d)

5-21 Blocks, Indentation (cont’d) public void fill (char ch) { int rows = grid.length, cols = grid[0].length; int r, c; for (r = 0; r < rows; r++) { for (c = 0; c < cols; c++) { grid[r][c] = ch; }

5-22 Review: l Name as many uses of comments as you can. l What does javadoc do? l Explain the difference between syntax and style. l Why is style important? l Roughly how many reserved words does Java have?

5-23 Review (cont’d): l Explain the convention for naming classes, methods and variables. Which of the following are syntactically valid names for variables: C, _denom_, my. num, AvgScore? Which of them are in good style? l What can happen if you put an extra semicolon in your program? l What are braces used for in Java? l Is indentation required by Java syntax or style?