Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
61 Cards in this Set
- Front
- Back
Converting from decimal to binary
|
1. Find the largest power of 2 that is smaller than or equal to the decimal number
2. Subtract that number from the decimal number 3. Insert 1 in binary number for position equivalent to that power of 2 4. Repeat 1 - 3, until you reach 0 |
|
Hexadecimal
|
Base-16 number system
Uses digits 0 - 9 and letters A - F One hexadecimal digit can express values from 0 to 15 Thus, one hexadecimal digit can represent 4 bits |
|
Convert Hexadecimal to Decimal
Vice Versa |
Ans: 1715004 (10)
1. Write out the hexadecimal in its expanded form. For example, 1A2B3C16 becomes : 1*165+10*164+2*163+11*162+3*161+12*160. 2. Evaluate the sum in decimal Reverse: Subtract highest powers of 16 http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html |
|
Convert Decimal to binary(division)
Vice Versa |
Decimal to Binary:
Divide by two and write remainder. Binary to Decimal: Raise 2 to the power of each 1's position and sum http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html |
|
Convert Decimal to binary(powers)
Vice Versa |
Decimal to Binary:
Subtract highest power of two, do the same with the remainder until you reach 0. Binary to Decimal: Raise 2 to the power of each 1's position and sum http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html |
|
Convert Hex to binary
Vice Versa |
Example #1
Convert (4E)16 to binary: (4)16 = (0100)2 (E)16 = (1110)2 So (4E)16 = (01001110)2 Binary to Hex: Convert (01001110)2 to hex: (0100)2 = (4)16 (1110)2 = (E)16 So (01001110)2 = (4E)16 |
|
Convert from Decimal to Hexadecimal
|
Ans: 4D86B
Change each remainder to Hex(1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) Decimal to Hex: A # in base 16 is equal to each digit multiplied with its corresponding power of 16: Example: E7A9 E7A9 (16) = 14×163+7×162+10×161+9×160 = 57344+1792+160+9 = 59305 |
|
A program can have three types of errors
|
The compiler will find syntax errors and other basic problems (compile-time errors)
If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors) |
|
Compile-Time Errors
|
Compiler errors
-Found by the compiler. -Usually caused by incorrect syntax or spelling The compiler will find syntax errors and other basic problems (compile-time errors) If compile-time errors exist, an executable version of the program is not created |
|
Run-Time Errors
|
-Reported by the system
-Usually caused by incorrect use of prewritten classes or invalid data A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) |
|
Logical Errors
|
-Found by testing the program
-Incorrect program design or incorrect execution of the design A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors) |
|
Class
|
-tool for encapsulating data and operations (METHODS) into one package
-defines a template or model for creating and manipulating OBJECTS --An object is defined by a class --A class is the blueprint of an object --The class uses methods to define the behaviors of the object --The class that contains the main method of a C# program represents the entire program --A class represents a concept, and an object represents the embodiment of that concept --Multiple objects can be created from the same class |
|
Objects
|
-data created using the class and its methods
-an object is an instance of the class -creating an object is INSTANTIATION An object has: --state - descriptive characteristics --behaviors - what it can do (or what can be done to it) --The state of a bank account includes its current balance --The behaviors associated with a bank account include the ability to make deposits and withdrawals Note that the behavior of an object might change its state |
|
INSTANTIATION
|
creating an object
|
|
Four Types of Flow of Control
|
1. Sequential Processing - Execute instructions in order
2. Method Call - Jump to code in method, then return 3. Selection - Choose code to execute based on data value 4. Looping or Iteration - Repeat operations for multiple data values |
|
Flow of control
|
The order of execution of instructions
|
|
C# reserved words
|
|
|
In the C# programming language:
|
-A program is made up of one or more CLASSES
-A class contains one or more METHODS -A method contains program STATEMENTS |
|
Comments
|
Block comments
Can span several lines Begin with /* End with */ Line comments Start with // Compiler ignores text from // to end of line |
|
Identifiers
|
-used to name classes, variables, and methods
Identifier Rules: --Must start with a letter --Can contain essentially any number of letters and digits, but no spaces --Case sensitive!! (Number1 and number1 are different!) --Cannot be keywords or reserved words |
|
Strings
|
-OBJECT(not primitive) in C#, defined by string class
-String literal is 0 or more characters enclosed with double quotes --“The quick brown fox jumped.” --“x” --“” Can contain any valid character |
|
The "+" operator
|
-String concatenation
-addition -- string + number = string -- number + number = number |
|
escape seq
|
|
|
Variables
|
A variable is a name for a location in memory used to hold a data value.
A variable must be declared by specifying the variable's name and the type of information that it will hold |
|
expression:
|
operators and operands that evaluate to a single value
--value is then assigned to target --target must be a variable (or constant) --value must be compatible with target's data type Syntax: target = expression; -Declare a variable only once -Once a variable is declared, its data type cannot be changed. These statements: double twoCents; double twoCents = .02; and double cashInHand; int cashInHand; generate compiler errors |
|
Constants
|
Value cannot change during program execution
Syntax: const dataType constantIdentifier = assignedValue; Note: assigning a value when the constant is declared is optional. Constants are useful for three important reasons: 1. Give meaning to otherwise unclear literal values --For example, MAX_LOAD means more than the literal 250 2. Facilitate program maintenance If a constant is used in multiple places, its value need only be updated in one place 3. Formally establish that a value should not change, avoiding inadvertent errors by other programmers Conventions: 1. Use all caps and separate words with underscore: const double TAX_RATE = .05; 2. Declare constants at the top so values can easily be seen 3. Declare as a constant any data that should not change during program execution |
|
Data Types
|
-For all data, assign a name (identifier) and a data type
-Data type tells compiler: ---How much memory to allocate ---Format in which to store data ---Types of operations you will perform on data -Compiler monitors use of data ---C# is a "strongly typed" language |
|
Primitive data types
|
13 simple data types:
---8 subsets of integers ---2 subsets of floating point numbers ---Character(NOT string) ---Boolean ---Decimal data type Everything else is an object |
|
Literals
|
All numeric values without a decimal point are considered int
All numeric values with decimal point are considered double int testGrade = 100; long cityPopulation = 425612340L; byte ageInYears = 19; float salesTax = .05F; double interestRate = 0.725; double avogadroNumber = +6.022E23; |
|
Expression
|
RHS of "="(assignment operator)
Combination of one or more operators and operands Has both data type & value Operands may be ---literals ---constants ---variables |
|
Operators & Precedence
|
25 / 3 8
25 % 3 1 3 / 25 0 3 % 25 3 25.0 / 5 5.0 10 / 3.0 3.3333333 |
|
Increment/Decrement
|
Operators can be applied in postfix form:
count++ (use value, then incr/decr) or prefix form: ++count (incr/decr, then use value) When used as part of a larger expression, the two forms can have different effects |
|
Shortcut Operators
|
No spaces are allowed between the arithmetic operator and the equals sign
Note that the correct sequence is +=, not =+ |
|
Mixed type arithmatic
|
When performing calculations with operands of different data types:
-Lower-precision operands are promoted to higher-precision data types, then the operation is performed -Promotion is effective only for expression evaluation; not a permanent change Called "implicit type casting" Bottom line: any expression involving a floating-point operand will have a floating-point result. |
|
Data conversion types
|
Widening conversion (promotion)
-safe, do not lose data -goes to a “wider” (more bits) data type -may lose precision (long to float) Narrowing conversion (demotion) -may lose data -may lose precision -should be avoided -compiler error unless specific cast done |
|
Conversion Techniques
|
assignment conversion
-assign an int to a long promotion -divide an int by a double casting Explicit Type Casting Syntax: (dataType)( expression ) Note: parentheses around expression are optional if expression consists of 1 variable Useful for calculating averages double result = (double) 25 / 3; double result = (double) total / count |
|
Encapsulation
|
Instance variables are usually declared to be private, which means users of the class must reference the data of an object by calling methods of the class.
Thus the methods provide a protective shell around the data. We call this encapsulation. Benefit: the class methods can ensure that the object data is always valid. |
|
Naming conventions
Classes and Objects |
Class names: start with a capital letter
Object references: start with a lowercase letter In both cases, internal words start with a capital letter Example: class: Student objects: student1, student2 |
|
Steps using objects
|
1. Declare an object reference
Syntax: ClassName objectReference; or ClassName objectRef1, objectRef2…; Object reference holds address of object 2. Instantiate an object Objects MUST be instantiated before they can be used Call a constructor using *new* keyword Constructor has same name as class. Syntax: objectReference = new ClassName( arg list ); Arg list (argument list) is comma-separated list of initial values to assign to object data, and may be empty Example: Date independenceDay; independenceDay = new Date(7,4, 1776 ); Date graduationDate = new Date(5,15,2008); Date defaultDate = new Date( ); |
|
objReferencces vs objData
|
Object references point to the location of object data.
An object can have multiple object references pointing to it. Or an object can have no object references pointing to it. If so, the garbage collector will free the object's memory Example: Date hireDate = new Date( 2, 15, 2003 ); Date promotionDate = new Date( 9, 28, 2004 ); promotionDate = hireDate; int x = 5, y = 3; x = y; |
|
Null reference
|
Date aDate;
aDate.setMonth( 5 ); Date independenceDay = new Date( 7, 4, 1776 ); // set object reference to null independenceDay = null; // attempt to use object reference independenceDay.setMonth(5); Gives runtime error. |
|
Method Classifications
|
Accessor methods
Gets the values of object data Mutator methods Writes/changes values of object data |
|
Dot Notation
|
Use when calling method to specify which object's data to use in the method
Syntax:objectReference.methodName( arg1, arg2, … ) Note: no data types are specified in the method call; arguments are values only! When calling a method, include only expressions in your argument list. Including data types in your argument list will cause a compiler error. If the method takes no arguments, remember to include the empty parentheses after the method's name. The parentheses are required even if there are no arguments. |
|
Using declaration
|
Must have using statement to use values in library:
using System.Text; Or you can fully qualify: System.Text.StringBuilder phrase = new System.Text.StringBuilder (“Change is inevitable”); |
|
*Math Class*
|
Basic mathematical functions
All methods are static methods (class methods) invoked through the name of the class no need to instantiate object Two static constants PI = the value of pi E = the base of the natural logarithm CALLING: -Use dot syntax with class name instead of object reference -Syntax:ClassName.methodName( args ) -Example: int absValue = Math.Abs( -9 ); -abs is a static method of the Math class that returns the absolute value of its argument (here, -9). |
|
Public
|
When we use the modifier public in front of some element, we are telling the compiler, that this element can be accessed from every class, no matter from the current project (assembly), from the current namespace.
- Methods that will be called by the client of the class are usually declared to be public - APIs of methods are published (made known) so that clients will know how to instantiate objects and call the methods of the class |
|
Private
|
The modifier private is used to indicate, that the element, to which is issued, cannot be accessed from any other class (except the class, in which it is defined), even if this class exists in the same namespace. This is the default access level, i.e. it is used when there is no access level modifier in front of the respective element of a class (this is true only for elements inside a class).
- Instance variables are usually declared to be private - Methods that will be called only by other methods of the class are usually declared to be private |
|
Defining Instance Variables
|
Syntax:
accessModifier dataType identifierList; dataType can be primitive date type or a class type identifierList can contain: -one or more variable names of the same data type -multiple variable names separated by commas -initial values Optionally, instance variables can be declared as const |
|
The return Statement
|
-The return type of a method indicates the type of value that the method sends back to the calling location
-A method that does not return a value has a void return type A return statement specifies the value that will be returned --- ' return expression; ' -Its expression must conform to the return type |
|
Method
|
-A method is defined by its
---Name ---Return type ---PARAMETERS (the stuff in the parenthesis) -When we call a method, we need to pass the correct number and type of parameters -“Speak the language” of the method |
|
Overloading
|
-Overloading involves using the same method/function name
---Vary the number of parameters ---Vary the type of parameters ---Cannot just change return type (ambiguity in invocation) -Useful to keep things simple ---Squaring a number… |
|
Short-Circuit Evaluation
|
If the result of the logical operation can be determined after evaluating the first operand, the second operand is not evaluated.
|
|
Comparing Object Data
|
With d1 and d2 Date object references:
d1.equals(d2); Returns true if the month, day, and year of d1 equals the month, day, and year of d2. |
|
The Conditional Operator (?:)
|
-The conditional operator ( ?: ) contributes one of two values to an expression based on the value of the condition.
-Some uses are ---handling invalid input ---outputting similar messages. -Syntax: ( condition ? trueExp : falseExp ) If condition is true, trueExp is used in the expression If condition is false, falseExp is used in the expression |
|
else
|
an else is always paired
with the closest preceding if that doesn’t already have an else paired with it. |
|
switch
|
- we must be comparing the value of a character (char) or integer (byte, short, or int) expression to constants of the same types
-The expression is evaluated, then its value is compared to the case constants in order. -When a match is found, the statements under that case constant are executed in sequence until either a break statement or the end of the switch block is reached. -Once a match is found, if other case constants are encountered before a break statement, then the statements for these case constants are also executed. |
|
How many kinds of loops?
|
4 kinds of loops
-for -while -do while -foreach |
|
The for Loop
|
Ideal when you know the number of iterations to perform before the loop begins
Has format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again } |
|
The while Loop
|
1. The while loop is designed for repeating a set of operations on data items when we don't know how many data items there will be.
2. We will get some signal when we have reached the end of the items to process. 3. The end of data items could be indicated by a special input value called a sentinel value or by reaching the end of a file 4. Receiving the signal is an event; we call this event-controlled looping |
|
The do-while loop
|
Similar to while loop
Must execute at least one time (test is at bottom) Has format: do { }while (<boolean value>); |
|
The foreach loop
|
-Often we have a collection of data
-We’d like to do something to each one of the items in the collection Certainly we can do this with a for loop -But C# provides a new loop – foreach ---References an item in the collection ---Provides access to it ---Loops through all elements in the collection foreach (<TYPE> <NAME> in <COLLECTION>) { WORK TO BE DONE ON EACH ITEM } |