Exam 1
Negating a boolean expression syntax
!(Boolean_Expression)
The 'mod' Operator
- (%) should be used with operators of integer type to obtain the remainder after the integer division ex: 14 / 4 is 3 with a remainder of 2 > Hence, 14 % 4 is equal to 2 - The mod operator has many uses, including: > determining if an integer is odd or even > clock arithmetic: 3hrs after 10 pm = 1 am / (3+10)%12=1
Primitive types
- 4 integer types, whole numbers --> byte = 8 bits --> short = 16 bits --> int = 32 bits (most common) --> long = 64 bits - 2 floating types, decimal numbers --> float = 32 bits --> double = 64 bits - 1 character type, characters on keyboard --> char - 1 boolean type, control loops and decision statements --> boolean
Negating a boolean expression
- A boolean expression can be negated using the "not" (!) Operator
named constant
- A memory location whose value cannot be changed while a program is running - Java provides mechanism to... define a variable/ initialize it/ fix the value so it cannot be changed - public static final Type Variable = Constant;
Naming Convention
- A set of rules used in the naming of files and folders: - Class types begin with uppercase - Primitive types begin with lowercase - Variable names start with lowercase then uppercase and are attached
Comments
- A single comment begins with // - Everything after these symbols & to the end of the line is treated as a comment & ignored by the compiler - A multi-line comment begins with /* and ends with */ - All significant block of source should be commented for good programming style
Compound statement
- A statement formed by joining two or more statements - To include multiple statements in one branch, enclose the statements in curly braces and enclosed statements should be properly indented
The Empty String
- A string can have any number of characters, including zero - The string with zero characters is called the empty string - The empty string is useful and can be created in many ways
The class String
- A value of type String is a sequence of characters and treated as a single object - Two strings are concatenated using the + operator
Meaningful Variable Names
- A variable's name should suggest it's use - Observe conventions in choosing names for variables > Use only letters and digits > "Punctuate" using uppercase letters at word boundaries - Start variables with lowercase letters - Start class names with uppercase letters
e Notation
- Also called scientific notation or floating-point notation
ASCII Encoding
- American Standard Code for Information Interchange - 7-bit & 8-bit ASCII is one of the most widely used text encoding schemes - 7-bit ASCII contains 2^7= 128 symbols - 8-bit ASCII contains 2^8= 256 symbols
Compound Boolean Expression
- An expression that includes more than one Boolean expression - Boolean expression can be combined using the "and"(&&) operator and the "or"(||) operator - The || operator is inclusive and allows both or either expression to be true - The ^ (exclusive or) is exclusive and allows one or the other expression to be true, but not both
String methods
- An object of the String class stores data consisting of a sequence of characters - Objects have methods as well as data - The length() method returns the number of characters in a particular String object
The division operator
- Behaves as expected if one of the operands is a floating type - When both operands are integer types, the result is a truncated integer > Hence, 99/100 has a value of 0
The Processor
- CPU: central processing unit or the chip - processes/run a program's instructions (needs to be simple) - comes from speed & program intricacy - Has the ALU: arithmetic logic unit (does mathematics and logical operations)
Arithmetic Operators
- Can be formed using the +, -, *, /, % operators together with variables on #s referred as operands - When both operands are the same type, the result is also the same type - When one is a floating-point type and the other is an integer, the result is a floating-point type - Expressions with 2 or more operators can be viewed as a series of steps with 1 or 2 operands
Char
- Characters ('A', 'b', '?', '*'...) - 8 bits
Class loader
- Classes: Several pieces of a Java program. May have a seperate author & each is compiled separately - Class loader automatically connects the classes together
Encoding/Decoding
- Computers can only understand binary (0 or 1) - Hardware & Software must translate into finite binary sequence
Storing Data
- Data of all kinds (numbers, letters, audio, video...) encoded & stored using binary numbers (0 and 1) - The address of the first byte is the address of the unit of bytes
Double
- Decimal & fraction numbers - 64 bits
Initializing values
- Declared variables that are not initialized are called uninitialized - Uninitialized values have the value of "null" - Initializing can be done at the same time as declarations
Converting a Base-10 integer to binary
- Do the same thing as before, but we write the remainder numbers from bottom to top
‼️Programs
- Finite sequence of instructions for a computer to follow written in a programming language - Used daily for emails, words - Following the instructions is called running or executing the program. - CPU executes the instructions - Computers and programs are considered one unit
code indention
- Good program style - Obligatory everytime there is a new body
Hardware & Software
- Hardware includes tangible (physical) parts of computer systems - Software includes programs and data
Programming languages
- High-level programming languages - Ex: Java, C, C++, Python, JSP, HTML... - Computer hardware needs high-level languages to be translated to low -level languages (binary)
Encoding with n bits
- If we have n bits, where n >= 0 then the maximum number of different symbols that can be encoded is 2^n
Encoding with 1 or 2 bits
- If we use 1 bit to encode a set of symbols, the maximum number of different symbols that can be encoded is 0 or 1 or 2^1 = 2 - If we use 2 bits to encode a set of symbols, the maximum number of different symbols that can be encoded is 00, 11, 01, 10 or 2^2=4
Encoding n Symbols
- In general, if we need to encode n symbols, the minimum number of bits needed is: ceiling(log2(n)) = number of times divided by 2 until we reach a quotient of 0 integer
Indentation
- Indentation should communicate nesting clearly - A good choice is 3 or 4 spaces or a tab for each level of indention - Indentation should be consistent - Eclipse can automatically indent code
int
- Integer variable types - 32 bits for each integer - Whole numbers
Java bytecode
- Java compiler translates a Java program into assembly language or machine language for a computer - It translates a Java program to byte-code - Byte-code: machine language for interpreter "Java Virtual Machine" - Easy to translate into machine language for any computer
Portability
- Java is platform independent: can be used on any computer after being compiled once
Assignment Compatibilities
- Java is strongly typed - A value can be assigned to a variable whose type allows more precision byte < short < int < long int < float < double - type 'char' can be assigned to a value of an 'int'
Files
- Large groups of bytes in aux are called files - Files have names - Organized into groups called directories or folders (sub-folders) - Java programs are stored in files when they are not executed - Program files are copied from aux into main memory
Constant
- Literal expressions like 2, 3.7 or 'y' are called constant values or literals - Can be preceded by a + or - sign; no commas
Java Identifiers
- Name of a variable - Only letters, digits from 0 to 9, '_' character - First character can not be a number (digit)
Naming and declaring variables
- Names should count - Provide name and type when declaring variables - include int, double, char, string, and boolean
Auxiliary memory
- Non-volatile - Secondary memory - Includes hard drives (SSDs and HDDs), USB, Flash Drives, etc... - Measured in Gigabytes, Terabytes ...
The Operating System
- OS: supervisory program that oversees the operation of the computer - Main job is to manage processes & hardware resources - Ex: Windows 10 OS, Mac OS, Microsoft OS, Linux OS
Parentheses & Precedence
- Parentheses can communicate the order in which arithmetic operations are performed - Without parentheses, an expression is evaluated according to the operator precedence
‼️String indices
- Positions start with 0, not 1 ex: The 'J' in "Java is fun" is in position 0; the 'i' is in position 4
‼️Operator Precedence Covered in this Class
- Postfix: expr++ (increment by 1), expr-- (decrement by 1) - Unary: ++expr (increment by 1), --decr (decrement by 1), +expr (positive number), -expr (negative number), !(type) (not known type {int...}) - Multiplicative: * (multiplication), / (division), % (mod) - Additive: + (addition), - (subtraction) - Relational: < (less than), > (more than), <= (less or equal to), >= (more or equal to) - Equality: == (equal to), != (not equal to) - Logical "AND": && - Logical "OR": || - Ternary: ?, : (not yet learnt but is basically a simpler if-else statement) - Assignment: =, +=, -=, *=, /=, %=
Data Types
- Primitive types: simple, non-decomposable values such as individuals or characters Ex: int, double, char [float, char, boolean] - Class/Object type: used for a class or object and has both data & symbols Ex: String (sequence of characters)
Applications
- Regular programs, meant to run on laptop or desktop computer
Keynotes or reserved words
- Saved by the compiler with pre-defined meanings
Type Casting
- Temporarily changes the value of a variable from the declared type to some other type
Unicode
- The ASCII character set is too small to encode text found in different spoken throughout the world - 16-bit Unicode can encode 2^16 = 65,536 symbols > Java uses unicode for char - 32-bit Unicode can encode 2^32 = 4,294,967,267 symbols - The first 256 characters of Unicode character are the same as ASCII character set > Thus, ASCII character set is a subset of the Unicode character set
Simple input
- The data needed for a computation are obtained from the user at runtime - Keyboard input requires "import java.util.Scanner" at the beginning of file - Data can be entered from keyboard using Scanner keyboard = new Scanner(System.in); - followed by for example eggsPerBasket = keyboard.nextInt();
Introduction to boolean expression
- The value is either true or false - Very important to control decision statements and loops
assignment statement
- Used to assign a value to a variable during runtime - Value = expression
increment and decrement operators
- Used to increase or decrease the value of a variable. by 1 - Easy to use, important to recognize - Increment operator: count++ or ++count or count=+ or count=count+1 - Decrement operator: count-- or --count or count-= or count=count-1
Precedence Rules
- Used to indicate the order of operations for all expressions (including boolean expressions) - When parentheses are omitted, the order of operation is determined by precedence rules
Main memory
- Working memory used to store current program, data used, results of calculations - Measured in Gigabytes - Volatile memory: loses memory when the power is gone
Common Windows Commands
- dir: directory - cd: change directory - mkdir: make directory - tree: graphic structure of drive - exit: quits directory
Memory
- holds programs, data, results of immediate processing - Has main memory and auxiliary memory
Encoding with 3 or 4 bits
- if we use 3 bits to encode a set of symbols, the maximum number of different symbols that can be encoded is 000, 001, 011, 111, 110, 100, 101, 010 or 2^3= 8 - If we use 4 bits to encode a set of symbols, the maximum number of different symbols that can be encoded is 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000, 0010, 0100, 0101, 1001, 1101, 0110, 1010, 1011 or 2^4= 16
public static void main(String[] args)
- main method - first method called when executing a compiled file - needed to run
Compilers
- translates a program from high-level to low-level language - Uses a source program to compile program by running the compiler on the high-level language version of the program - Produce machine-or-assembly-language programs called object programs - 20% = parxing the program - 80% = devoted to optimization - Each high-level language needs a different compiler for each OS and computer
Escape character
-\; how you would print - The compiler needs to be told that the quotation marks do not signal the start or end of a string, but instead are to be printed - Each escape sequence is a single character even though it is written with 2 symbols
Example: Convert (1001101)2 to base-10 integer
1) 1 0 0 1 1 0 1 x 64 32 16 8 4 2 1 <-- double the numbers 2) (1*64)+(0*32)+(0*16)+(1*8)+(1*4)+(0*2)+(1*1) = 64+8+4+1= 77 <-- multiply then add everything > Hence, (1001101)2 = (77)10
Converting a Binary number to Base-10 integer
1) Start by writing one on the rightmost corner then doubling it until we reach the leftmost side (n^2) 2) Multiply the binary and the double numbers together 3) Add everything together
Compiling, Interpreting, & Running
1) Use the compiler to translate the Java program into byte-code (compile command) ex: JavaC className.java 2) Use the bytecode interpreter to translate into machine language & run the resulting machine-language instructions ("run command") ex: Java className
Inputs
2 kinds of inputs: - Program's instructions - data needed by the program
Ex: ceiling(log2(30) = 5
30/2 = 15 r 0 15/2 = 7 r 1 7/2 = 3 r 1 3/2 = 1 r 1 1/2 = 0 r 1 > Hence, we need to divide by 2 a total of 5 times to reach a quotient of 0
Ex: Convert ceiling(log2(41)) from base-10 integer to binary
41/2 = 20 r 1 20/2 = 10 r 0 10/2 = 5 r 0 5/2 = 2 r 1 2/2 = 1 r 0 1/2 = 0 r 1 > Hence the binary (or Base-2) of 41 is 101001 from a Base-10 integer or (41)10 = (101001)2
Contigency
A boolean expression is a contingency when it is neither a tautology and a contradiction
Contradiction
A boolean expression is a contradiction if and only if it is always false regardless of the values of its variables
Tautology
A boolean expression is a tautology if and only if it is always true regardless of the values of its variables
The if-else statement
A branching statement that chooses between 2 possible actions
Addresses
A numbered location where each byte is in main memory
Programmer
A person who writes and tests computer programs
Process
A program in execution
Specialized Assignment Operators
Assignment operators can be combined with arithmetic operators (+,-,*,/,%) ex: amount = amount + 5 is the same as amount += 5
0's and 1's
Binary (base-z) is the mathematical number system consisting only of 0s and 1s
Branching /Decision/Selection Statement
Choose between 2 or more possible actions
Windows Commands
Command Prompt: Place where windows commands are programs that allow users to interact with the Windows OS Current/Present Working Directory: directory the command prompt is currently referencing
Bytes
Consists of 8 bits
Value
Data stored by a variable in a memory location
{
Denotes the body of code; needs to be closed
Syntax
Grammar rules for programming language
Compound Boolean Expression Syntax
If ((sub_Expression_1) &&(sub_Expression_2)) If ((sub_Expression_1) ||(sub_Expression_2))
The if-else syntax
If (boolean_expression) Statement_1 else Statement_2 - If the boolean expression is true then the statement 1 is applied. If the boolean expression is false then the statement 2 is applied.
Statement
Instruction to the computer, ends with a semi-colon (;)
Argument
Items inside parenthesis that provide the informations needed by methods
Package
Library of classes that have been defined already
Hardware & Memory
Most computers have: - Input devices (keyboard, mouse, etc.) - Output devices (display screen, printer, etc.) - One or two processors - 2 types of memory (main memory & aux memory)
Compiling & Running a Program
Process One: Java program < Java Compiler < Bytecode program < Bytecode Interpreter (JVM) < Machine Language Instructions < Computer Execution of Machine Language Instructions < Output of Java Program Process Two: Data for Java program < Computer Execution of Machine Language Instructions < Output of Java Program
Variable
Program component that can store or represent data
Outputs
Result produced by following the instructions in the program
User
Someone who interacts with the program
Encoding
Translating from 1 set of symbols to another set of symbols ex: ASCII to binary; integer to binary
Decoding
Translating from the encoded symbol back to the o.g. symbol set ex: Binary to ASCII; binary to integer
Logically equivalent
Two boolean expressions are logically equivalent if and only if and only if their resulting truth tables are identical for each variation of their variables
Bits
binary digits of 0s and 1s
Flow of Control
order in which a program performs actions (is sequential)
Loop Statement
repeats an action until a stopping condition occurs