Welcome to class! - Northeastern Illinois University

Download Report

Transcript Welcome to class! - Northeastern Illinois University

A Review
a 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.
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).
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
You 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)
Compiler Gotcha: Mismatched braces, quotation marks, or parenthesis
Possible Error Message: Errors.java:13: reached end of file while parsing
public class Errors
{
public static void main(String args[ ])
{
//body of method
} //closing main method
} //closing class header
Enter them in pairs
& label closing
Compiler Gotcha: misspelling a keyword
Error Message: Errors.java:6: cannot find symbol
Type Accurately.
Remember keywords are l.c.
Compiler 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)
Compiler 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)
Compiler Gotcha: putting blank space in a multi-word variable name
Error Messages:
Errors.java:6: ';' expected
Errors.java:6: not a statement
Just Don’t Do It!
To reinforce this habit, never use blank spaces when you
name any computer file.
Compiler Gotcha: forgetting to end the statement?
Error Message: Errors.java:6: ';' expected
Use it!
;
(semi-colon)
Compiler 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
Compiler 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;
unintentionally 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
}
forgetting the rules for operator precedence
Error Message:
NONE! Watch out for this one!
Highest
Precedence
Lowest
Precedence
- (unary)
* / %
+ -
x = 2+5*4;
y = 7-2+3;
// x = 22
// y = 8;
x=
(2+5)*4;
y=7–
(2+3);
// x = 28
// y = 2;
When operators within an expression have the same precedence,
sharing an operand, they work according to their associativity. When
in doubt, use (parenthesis)!
Compiler Gotcha: placing a space between combined operators
Error Message: Errors.java:9: illegal start of expression
z + = y; //Don’t
z += y; //Do
Compiler Gotcha: incompatible data types
public class Errors
{
public class Errors
{
public static void main(String args[ ])
{
float y =2.75F;
int z =1;
public static void main(String args[ ])
{
float y =2.75F;
z+= y;
//Don’t (z:3)
} //closing main method
}
//Do
float z =1.0F;
z+= y;
//(z:3.75)
} //closing main method
}
Compiler Gotcha: forgetting to terminate a multi-line comment
Error Message: Errors.java:3: unclosed comment
/*
start multi-line
comment
blah, blah, blah…
*/
Compiler 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