A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software...

19
JAVA FUNDAMENTALS A Review

Transcript of A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software...

Page 1: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

JAVA FUNDAMENTALS

A Review

Page 2: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Programming Constructsa review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document

Large Problems are broken down into smaller modules to be solved using flowcharting or pseudo-code, then coded, & re-assembled to solve the complete problem.

High-level languages require a compiler to translate source code into machine code ( or byte code in the case of Java).

Byte code is interpreted by the Java Virtual Machine and run.

Page 3: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

a review of lessons learned so far… ( 2 steps forward - 1 step back)Program command execution is linear, thus, programming statements must be written in order (linear) as well.Programming languages contain keywords/reserved words that can only be used as their intended purpose as determined by the language.Languages utilize a specific syntax & punctuation.Primitive data types are “built in” to the language, and allow for data storage & retrieval.Storage space must be allocated for data, before it can be used to store data.Programming languages allow for user defined “identifiers” of storage space (variables & constants), methods, and classes (in object-oriented languages like Java).

Page 4: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

a review of lessons learned so far… ( 2 steps forward - 1 step back)

The API extends/expands the language with a library of classes/methods for common programming tasks.

Common Mathematical Operators are: +, -, *, /, %, ++ (increment), -- (decrement) & - (unary minus)= (The Assignment Operater, assigns the rval to the the lval )

Mathematical Expressions are processed with operator precedence & associativity.

Programmers use comments to note explanation of coding methods & identifier purposes for future reference.

Good Programming Practice is to write “readable by humans” code (indent, ws, comments).

Page 5: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Types of errorsYou mean there are more than one?

Compile-Time: syntax errors caught by the compiler

Run-Time: errors caused by exceptions resulting in the program terminating abnormally (ex. attempting to divide by zero (0) - undefined command - program crashes)

Logic: program compiles & runs, but, produces inaccurate results(always test program with data to verify accurate results)

Page 6: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: Mismatched braces, quotation marks, or parenthesis

Possible Error Message: Errors.java:13: reached end of file while parsing

public class Errors {

Enter them in pairs

& label closing

} //closing class header

} //closing main method

//body of method

public static void main(String args[ ]) {

Page 7: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: misspelling a keyword

Error Message: Errors.java:6: cannot find symbol

Type Accurately.Remember keywords

are l.c.

Page 8: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: Using a keyword as a variable name

Errors.java:6: not a statement

Errors.java:6: ';' expected

Errors.java:6: not a statement

Sampling of most commonly accidently used keywords (and replacement choice):

class (use group)final ( use endResult)

return (use returnValue)false ( use negative)true (use affirmative)new (use newValue)

null ( use emptyValue)

Page 9: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: misspelling variable names & switching use of l.c. & u.c.

Errors.java:7: cannot find symbol

Be consistent.Follow convention when naming variables.

List all variable declarations at the beginning of a method - FIRST!

(then you can refer to them as a list when you need to use them,

and…you will not improperly use or accidently reuse a

variable name)

Page 10: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: putting blank space in a multi-word variable name

Error Messages:Errors.java:6: ';' expectedErrors.java:6: not a statement

Just Don’t Do It!

To reinforce this habit, never use blank spaces when you name any computer file.

Page 11: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: forgetting to end the statement?

Error Message: Errors.java:6: ';' expected

Use it!;

(semi-colon)

Page 12: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: mis-matched data types & literals

Error Message: Errors.java:6: possible loss of precision

Remember float literals are treated as double values.

float total = 5.0; // wrong

float total = 5.0F; // correct

Page 13: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: using commas (,) or currency ($) symbols in numeric data types

Error Message: Errors.java:6: ';' expected

DON’T USE $ OR , IN DATA!

float totalExpense = $5,000.00F;

Correct:float totalExpense =

5000.00F;

Page 14: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding Errorsunintentionally performing integer division (loss of remainder)

Error Message: NONE! Watch out for this one!

public class Errors {

public static void main(String args[ ]) { int x =5; int y =2; int z; z= x/y; System.out.println(z); //output: 2 } //closing main method }

Page 15: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding Errorsforgetting the rules for operator precedence

Error Message: NONE! Watch out for this one!

When operators within an expression have the same precedence, sharing an operand, they work according to their associativity. When in doubt, use (parenthesis)!

HighestPrecedence - (unary)

* / %Lowest Precedence + -

x = 2+5*4;

// x = 22

x = (2+5)*4;

// x = 28

y = 7-2+3;

// y = 8;

y = 7 – (2+3);

// y = 2;

Page 16: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: placing a space between combined operators

Error Message: Errors.java:9: illegal start of expression

z + = y; //Don’t

z += y; //Do

Page 17: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: incompatible data types

public class Errors {

public static void main(String args[ ]) { float y =2.75F; int z =1;

z+= y; //Don’t (z:3) } //closing main method }

public class Errors {

public static void main(String args[ ]) { float y =2.75F;

float z =1.0F; //Do z+= y; //(z:3.75) } //closing main method }

Page 18: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: forgetting to terminate a multi-line comment

Error Message: Errors.java:3: unclosed comment

/* start multi-line commentblah, blah, blah…*/

Page 19: A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.

Common Coding ErrorsCompiler Gotcha: forgetting to use the import statement

Errors.java:11: cannot find symbol

import java.util.Scanner;

public class Errors { public static void main(String args[ ]) { Scanner keyboard = new Scanner(System.in); } //closing main method } //closing class definition

For this

statemnt to

work…

You must i

mport the Scanner

Object!