Download presentation
Presentation is loading. Please wait.
Published byClifford Lucas Modified over 8 years ago
1
The Essentials of a Java Program 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
2
4-2 Classes and Source Files l A class defines a class of objects. Class name:File name: SomeClass Ramblecs FallingCube SomeClass.java Ramblecs.java FallingCube.java Convention: a class name starts with a capital letter Same upper / lower case letters
3
4-3 Components of a Class A class consists of: l Fields l Constructors l Methods
4
4-4 public class SomeClass { l Fields l Constructors l Methods } private: visible only inside this class public: visible in other classes Attributes / variables that define the object’s state. Can hold numbers, characters, strings, other objects. Usually private. Code for constructing a new object and initializing its fields. Usually public. Actions that an object can take. Can be public or private.
5
4-5 Summer Focus Our focus this summer will be on creating basic Java applications that involve methods. For Labs 1-3, you will create applications that have only a main method. For Labs 4-8, you will create applications will involve multiple methods. You will write applications and classes that involve fields and constructors during the school year.
6
4-6 All Java applications have a class containing a main method: public class SomeApp { public static void main(String [ ] args) { // program begins by executing // code in main method } }
7
4-7 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.
8
4-8 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
9
4-9 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 * = 2.2046; // Convert to kilograms
10
4-10 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.
11
4-11 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.
12
4-12 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.
13
4-13 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()
14
4-14 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
15
4-15 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.
16
4-16 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
17
4-17 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
18
4-18 Style l Arrange code on separate lines; insert blank lines between fragments of code. l Use comments. l Use meaningful variable names. l Indent blocks within braces.
19
4-19 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: Both compile fine! In fact, they are identical to the compiler!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.