Midterm 1 Flashcards
(22 cards)
Algorithm
a set of unambiguous and ordered steps to accomplish a task
3 control structures
Sequence (i.e. one line after the other)
Decision making (e.g. using if/else constructs)
Looping (e.g. using while loops)
Algorithm development should be independent of…
the final computer language used to implement the algorithm as an executable program
Java
- an object-oriented programming language
- portable between machines
- > compiled into Java bytecode. This is a machine code for a “virtual computer” called the Java Virtual Machine (JVM).
- > JVM interpreter reads bytecode and simulates its execution.
Object
The principal entities that are manipulated by a Java program
Class
- A blueprint or template for the structure of an object.
- The structure around which all Java programs are based.
- combination of data units (variables) and computation units (methods)
Method
- Java’s term for a procedure or subroutine. Every method belongs to some class, and a class may have any methods.
- Each method has a name, a list of arguments enclosed in parentheses, and body, enclosed in curly braces.
Main Method
There must be exactly one method called main. This is where execution begins
Statements (4 examples)
Individual instructions. Examples:
- Declarations -> declare (and initialize) variables - Assignment -> compute and assign a new value to a variable - Method invocation -> execute a method (which may return a result) - Control flow -> alter the order in which statements are executed
Variables
Data items that the methods operate on. Variables can be of various types, integers
Expressions/ Operators
Java provides various operators, which allow you to manipulate the variables
x = (2 * y - z) / 32;
Types of errors
Compile time and Run time
Compile time error
- caught by Eclipse / Java compiler
- Syntax errors
- Disobeys the rules of the language; violates language’s grammar
- Type errors: misuse of variables
Run time error
- appear during program execution
- Obeys the rules of the language but does not express them meaning you intended;
- Division by 0
- Crash or hang or wrong outputs (b/c of mistakes in programming)
What is variable? (4)
- name of some location of memory used to hold a data value.
- Different types of data require different amounts of memory. Compiler’s job is to reserve sufficient memory
- Variables need to be declared once
- Variables are assigned values, and these values may be changed later
- Each variable has a type, and operations can only be performed between compatible types.
Variable Name (rule
- Starts with: a letter (a-z or A-Z), dollar sign ($), or underscore (_)
- Followed by: zero or more letters, dollar signs, underscores, or digits (0-9)
- Uppercase and lowercase are different (total =/= Total =/= TOTAL)
- Cannot be any of the reserved names (class, float, int, if, then, else, do, public, private, void,…)
Primitive Data Types
Integer Types: byte, short, int, long
Floating-Point Types (real numbers): float, double
Other types: boolean, char
Escape sequences
Allows us to include single/double quotes and other special characters:
" double quote
' single quote
\ backslash
\n new-line character (starts a new line)
\t tab character
String Comparison
Strings should not be compared using operators (==, <=, returns true if s equals t
s.length() -> returns length
s.compareTo(t) -> compares strings lexicographically
result < 0 if s is less than t
result == 0 if s is equal to t
result > 0 if s is greater than t
Scanner Class operations
nextBoolean() nextByte() nextDouble() nextFloat() nextInt() nextLong() nextShort() next() - returns sequence of characters up to next whitespace (space, carriage return, tab, etc.) nextLine() - returns sequence of characters up to next carriage return
Control flow
The order in which statements are executed
General rule -> top to bottom
Several control structures that change that
Conditional statements
permit control flow to be dependent on (true/false) conditions
- if
- if-else