Chapter 4

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

(4.4 - Introducing Strings) - what type of quotes do you use to initialize a string?

" "

(4.2 - Variables) - what is the message the compiler will show if you try to variable cast when it's not necessarily safe?

"Cannot implicitly convert type "double" to "int". An explicit conversion exists (missing a cast?)"

(4.4 - Introducing Strings) - Quiz #5 - what operator is used to concatenate or join 2 or more string values?

+ (plus sign)

(4.2 - Variables) - case sensitivity

-C# is case-sensitive lang. -MEANS, a variable named myInteger is DIFFERENT from a variable named MyInteger

(4.2 - Variables) - how to make a variable constant?

-add the const keyword in front of the data type ex: const double PI = 3.14159;

(4.4 - Introducing Strings) - blank and null strings

-blank string - written as 2 double quotes back to back with no spaces in between -null string - means no string of any size -difference btwn holding an empty bag with nothing in it and not having any bag in hand at all

(4.2 - Variables) - what do you do after you declare your variable?

-can initialize it with value -use an assignment statement with the ='s sign left of statement = variable name right of statement = any expression that results in a single data value

(4.2 - Variables) - choosing good variable names and choosing a style

-choose variable names long enough to usefully represent what the variable contains -also chose a consistent style (slide 17)

(4.2 - Variables) - DQ #6 - review the best practices for naming variables

-consistent style -be specific enough but not too long -meaningful name (pertains to what the variable does)

(4.2 - Variables) - DQ #8 - what should you watch for when casting variables?

-decimal to integer, (for ex), it will lose the decimal part of it -watch or mismatching

(4.2 - Variables) - How do you create a new variable?

-declare it -each declaration starts with a data type followed by a name ex: char myChar;

(4.4 - Introducing Strings) - how to create a string variable?

-declare it -start with "string" followed by the variable name ex: string myString;

(4.3 - Reference Data Types) - how to declare a ref. variable?

-declare it - write the data type (class name) 1st then the variable name

(4.2 - Variables) - variable - Double

-fractional #'s are by default double values -to make it a decimal must add the "f" suffix to make it a float or the 'M' suffix to make it a DECIMAL

(4.2 - Variables) - when will the compiler not allow you to variable cast?

-if the conversion is not necessarily safe (slide 19) -compiler will be worried that the integer data type can't hold the fractional part of the double... etc.

(4.2 - Variables) - Quiz #3 - consider the following code: public class MyClass{ int myInt1 = 1; public void function1(){ int myInt2 = 2;} } Which variable has a class-level and local scope?

-myInt1 has a class-level scope =myInt2 has a local scope

(4.2 - Variables) - what is the purpose of a variable?

-places where you store data in your program (can change the variable's contents while running the program)

(4.3 - Reference Data Types) - how to assign a valid object to ref. variable ?

-to create a new instance or copy of object, use the NEW keyword ex: Contact myContact = new Contact( ); // create a new contact object

(4.2 - Variables) - variable - char

-use single quotes around individual characters

(4.4 - Introducing Strings) - string equality - how to compare 2 strings to see if they are equal?

-use the Equals() method on the string object -Equals() will return a Boolean true if the 2 strings are identical, or FALSE otherwise

(4.2 - Variables) - rules for naming variables

-valid characters are: -letters -numbers -underscore characters ( _ ) -the name must start with a letter or underscore -a variable name can NEVER start with a number (but can be used in the name) -variable names can't have spaces -all declarations need to have unique names -can't use reserved words for variable names (c# compiler has list of words with specific meaning to the compiler)

(4.2 - Variables) - Variable

-when you create an instance of a data type and give it name, (that instance is called a variable)

(4.3 - Reference Data Types) - primitive data types

-when you declare a primitive data variable, you automatically get a spot in a computer memory reserved for that value

(4.2 - Variables) - DQ #7 - what is a variable casting and why would you need to use it?

-when you need to convert variables from 1 data type to another -1.Would need to use it when you need to convert variables of 1 data type to another 2.Would need to use it only when you are sure the conversion is safe 1.Ex: you have a real number you are certain actually holds an integer, can cast with confidence

(4.2 - Variables) - variable casting

-when you need to convert variables of 1 data type to another

(4.3 - Reference Data Types) - Quiz #1 - what is the diff. between a ref. data type and a value data type?

. A reference data type does not automatically create space for the data in memory when a variable is declared.

(4.2 - Variables) - variable in scope / out of scope

. A variable is "in scope" at a particular spot in your code if it exists and can be seen by code at that location. Otherwise, it is "out of scope". (slides 23-24)

(4.2 - Variables) - DQ #1 - what is a variable? how can you create a variable in code?

...

(4.2 - Variables) - Quiz #6 - see quiz

...

(4.4 - Introducing Strings) - DQ #1 - what is the purpose of the string data type?

...

(4.4 - Introducing Strings) - DQ #3 - why can't you use the "=" to compare 2 string variables? how should you compare 2 string variables/

...

(4.4 - Introducing Strings) - DQ #5 - What is concatenation? What operator is used to perform this functionality in C#? What is the short-cut operator to quickly add a value to the end of a String variable?

...

(4.4 - Introducing Strings) - DQ #2 - how is a string variable created? why do you not need to use the new keyword

1.Create: declare it -by staring with "string" and then the variable name 2.You don't need to use the new keyword because the string is a special case among the ref. data types since it is heavily used

(4.2 - Variables) - DQ #5 - review the rules for naming c# variables

1.Name must start with a letter or underscore (NEVER a number) 2.Has to be 1 line (no spaces) 3.All declarations have to have unique names (can't have the same 1 for diff. variables) 4.Can't used a reserved keyword (ex: "Float" or "return") 5. no special characters (only letters, #'s and underscores)

(4.4 - Introducing Strings) - DQ #8 - what is the diff. between a null string and an empty string?

1.Null string - means no string of any size 2.Empty string - does not contain any characters 3.Like holding an empty bag with nothing in it vs. not having a bag at all (double check)

(4.3 - Reference Data Types) - DQ #1 - what is the diff. between a reference data type and a primitive data type

1.Primative - automatically get a spot in computer memory reserved for that value 2.Reference - declaring them as variables does not create space for the data in memory

(4.3 - Reference Data Types) - DQ #4 - how do you create a new instance of a ref. variable?

1.To create a new instance or copy of your object, you need to use the new keyword

(4.4 - Introducing Strings) - DQ #6 - how can you access individual characters in a string variable? why is this possible?

1.Use an index value in square brackets

(4.4 - Introducing Strings) - DQ #4 - how can you compare 2 strings without worrying about case-sensitivity?

1.Use the optional StringComparison.OrdinalIgnreCase

(4.4 - Introducing Strings) -accessing string characters - important note

1st character in string has an index value of [0]

(4.3 - Reference Data Types) - Quiz #5 - what is the result of the code: ReferenceDataType myValue1 = new ReferenceDataType();ReferenceDataType myValue2 = myValue1;

Both myValue1 and myValue2 refer to the same data in memory (review in textbook / ppt)

(4.4 - Introducing Strings) - when to use the plus-equal operator (+=)?

If you have a variable with some string contents already, and you want to add more to the end, you can use the plus-equals operator (+=) as shown below.

(4.4 - Introducing Strings) - what function shows if your string is empty or null?

IsNullorEmpty( )

(4.3 - Reference Data Types) - multiple references to the same object

It is possible to create more than one reference variable that points to the same object! You can imagine creating a Contact named "Joseph" and sharing that object with two of your friends. You have not copied Joseph or his contact information, but are sharing that same object between two different references.

(4.2 - Variables) -mixing data types

It is possible to write a mathematical expression with a mixture of data types. Ex: integer and floating-point number ("3 + 4.2"). C# will automatically cast the integer to a floating point number ("3.0 + 4.2") in order to perform the math. If you are storing the result in a floating point variable such as double, you don't need to explicitly cast the results, because they will always fit without losing any data. If you are storing the results as an int, you will need to cast the result to that data type because you are losing the fractional part when storing in the variable.

(4.3 - Reference Data Types) - more info on string variables

It may not be obvious, but C# string variables are actually reference variables! String values like "Amanda" are objects in memory, so string variables can be set to null or point to the same shared string values in memory. We'll take a closer look at strings in the next lesson.

(4.3 - Reference Data Types) - DQ #2 - what is the initial value for a ref. data object?

Null

(4.2 - Variables) - how do you cast?

To cast, use the target data type in parentheses right before the source data, and the compiler will force it to the target data type. Only cast when you are sure the conversion is safe even if the compiler is unsure.

(4.4 - Introducing Strings) - getting the string length

You can find the number of characters in a string by reading the Length property (see textbook)

(4.4 - Introducing Strings) - what type of bracket is used for accessing string characters?

[]

(4.4 - Introducing Strings) - string

a series of characters that form a line of text (can be as short as a name or long as an entire book)

(4.2 - Variables) - Quiz #1 - what is a variable?

a specific, named instance of a data type

(4.3 - Reference Data Types) -in C# where is a line of text stored in?

a string data type

(4.2 - Variables) - constant variable

a variable whose value NEVER CHANGES during the course of a program ex: Pi = 3.14159

(4.2 - Variables) - Quiz #2 - what is a constant variable?

a variable whose value remains the same throughout the course of the program

(4.2 - Variables) - Quiz #4 - which of the following variable assignment statements is NOT valid? a. char MyChar = "A"; b. long thisIsMyNumber = 56; c. int myNumber = 5; d. bool isTrue = false;

a. because it needs to be in SINGLE quotations

(4.4 - Introducing Strings) - Quiz #3 - given the following string String myString = "Hello"; which code will allow you to access the 3rd letter char c3 = myString[2]; b. char c3 = myString[3]; c. char c3 = myString(3); d. char c3 = myString[4];

a. charc3 = myString[2];

(4.2 - Variables) - DQ #3 - what must you do to a decimal variable to make the compiler understand the value property?

add the "M" suffix

(4.3 - Reference Data Types) - DQ #3 - what happens if you use a ref. variable that has not been initialized?

an error message will occur

(4.3 - Reference Data Types) - what happens if you use a ref. variable that contains null

an error will occur in the program

(4.3 - Reference Data Types) - what to do if you want to change a ref. variable so it doesn't point at any object

assign the value "null" to the variable

(4.4 - Introducing Strings) - Quiz #1 - what is the result of the following code: String myString1 = "Hello";String myString2 = "HELLO";bool isEqual = myString1.Equals(myString2); a. The value of isEqual is true b. The value of isEqual will be false

b - false

(4.4 - Introducing Strings) - why don't you have to use the new keyword?

because the string is a special case among the ref. data types bc it is heavily used

(4.3 - Reference Data Types) - reference data type

called "reference" types bc declaring them as variables does NOT create space for the data in memory -the variable name is merely a "reference" to an object or class that resides somewhere else in memory

(4.2 - Variables) - how can you override the compiler's natural caution and force the conversion?

casting -means you tell the compiler the source data type to the target data type, doing whatever is necessary to make the data fit

(4.2 - Variables) - variable - boolean values

either true or false

(4.4 - Introducing Strings) - Quiz #2 - you must use the "new" keyword when creating a string variable

false

(4.4 - Introducing Strings) - Quiz #4 - the isEqual( ) method will perform a case-insensitive comparison of 2 string values

false

(4.2 - Variables) - Quiz #5 - the compiler will always allow you to assign data from 1 variable to a variable of another data type (true or false)

false (bc it wont if the compiler thinks it is not "safe") - see txbx and ppt

(4.2 - Variables) - DQ #10 - which scope level is NOT available in C#?

global scope

(4.2 - Variables) - when will the compiler allow you to assign data from 1 variable to another data type?

if the assignment is "safe" (no chance you will lose or truncate data while making the conversion) (see slide 18)

(4.4 - Introducing Strings) - in order to use a string variable in a program you have to ________ it

initialize

(4.2 - Variables) - a constant variable must be ________ when it is declared

initialized

(4.2 - Variables) - what is initializing a variable

initializing it with a value (would use an assignment statement with an = sign)

(4.2 - Variables) - variable - int

interger type

(4.2 - Variables) - DQ #9 - what is the difference between local, class-level and global scope?

local scope - only lives / works in function defined in class level - lives for everything in the class (not just function global scope - not present in c# - in other lang. like C++ it is possible to declare a variable outside of any class or function definition

(4.4 - Introducing Strings) - example of initializing a string

myString = "hello" -did NOT have to use the NEW keyword to create a new string

(4.3 - Reference Data Types) - Quiz #3 - what keyword needs to be used to create an instance of a reference variable?

new

(4.3 - Reference Data Types) - Quiz #2 - what is the initial value for a ref. data object?

null

(4.3 - Reference Data Types) - what is the initial value of ref. data type

null means "nothing" - variable refers to nothing and has no data at all associated with it

(4.3 - Reference Data Types) - what can you add when you create a new instance of an object?

parameters (data values) helps initialize the object

(4.3 - Reference Data Types) - add a set of ______ after the data type when using the ____ keyword

parentheses new

(4.3 - Reference Data Types) - string variables are ______

references

(4.2 - Variables) - Variable Scope

scope - concept of variable lifespan and visibility Where you declare your variables will determine which parts of your program can see and access those variables as well as how long they "live" before being discarded by the computer program. The concept of variable lifespan and visibility is called scope. A variable is "in scope" at a particular spot in your code if it exists and can be seen by code at that location. Otherwise, it is "out of scope". All variables must be declared within some set of opening and closing curly braces. These braces will mark the beginning and ending of the variable's scope. If you declare the variable just inside a "class" definition, then the variable will be visible to all of the functions within that class, and will live for as long as the class itself does.

(4.4 - Introducing Strings) - DQ #7 - what is a zero-based index?

the 1st character in the string is index 0 / has a 0 index

(4.4 - Introducing Strings) - string concatentaion - what is concatenation

the process of putting together 2 pieces -can put 2 smaller strings to make a larger string by concatenation

(4.3 - Reference Data Types) - Quiz #4 - what happens if you use a reference variable that has not been initialized?

this will cause an error in your program

(4.2 - Variables) - DQ #2 - how can you assign a value to a variable? how can you create a variable and assign a value at the same time?

to assign value - type variable name then = sign the value then semi colon @ same time, type variable type and then name and then = sign value, semi colon

(4.2 - Variables) - DQ #4 - when would you use a constant variable in program?

use it when you don't want value of variable to change throughout the program

(4.2 - Variables) - variable - Float

use the "F" suffix to specify a float value ex: myFloatnumber = 1.02 F

(4.4 - Introducing Strings) - how to check for string equality but IGNORE case differences?

use the parameter StringComparison.OrdinalIgnoreCase at the end (+ see textbook)

(4.4 - Introducing Strings) - how to put 2 smaller strings into a larger string (concatenation)

use the plus sign (+)

(4.3 - Reference Data Types) - new keyowrd

will create a new object in memory and return a ref. to that object

(4.2 - Variables) - DQ #11 - what happens if you try to access a variable that is out-of-scope?

will show an error message (see txbx)

(4.4 - Introducing Strings) - can you declare and initialize a string variable at the same time

yes ex: string myString = "Greetings";

(4.2 - Variables) - can you initialize the variable the same time you declare it?

yes ex: BOOL isAccepted = FALSE;


Kaugnay na mga set ng pag-aaral

Ch 15 Triumph of Imperialism in Asia

View Set

World History Unit 5: Age of Encounter & Explorer

View Set

finance & Closing- ga state 15 questions

View Set

V. CEMETERIES, NYS LAW EXAM REVIEW #2, VI: COMMISIONER OF HEALTH (NYS DOH), VII: COMMUNICABLE DISEASES, VIII: CREMATORY OPERATIONS, IX: DEATH CERTIFICATES AND BURIAL PERMITS, PRE-NEED COMPLIANCE 2020, NYS Funeral Law, NYS Funeral Pre-Plan

View Set

Practice Questions: Seizures & Epilepsy

View Set

Nursing Fundamentals Practice Final Exam EXTRA STUDY

View Set

Media Law Final: Access and Protecting Sources

View Set

ATP Chapter 2 Branch Circuits & Feeders

View Set