Intro to C# Programming Lesson 7 Chapter 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

Why is the second reason it not good programming to use the the *!=* operator to write the *Equals(* *)* *function* when comparing the value of STRINGS?

Another reason that you may not want to use these operators is because other programming languages such as C++ and Java do not allow the use of them.

What is another *function* PROVIDED BY CLASS, which will be introduced in Lesson 7 IN THE COMPARISON OF STRINGS' VALUES?

Another useful function provided by class String is the Equals() function.

Why would it be easier to test for EQUALS rather than using the Compare( ) function TO COMPARE STRINGS' VALUES?

Because the Equals( ) function works very naturally.

Why is the first reason that using *!=* as an operator important to obey?

C# has provided this ability as a convenience, but really, it is making for dangerous programming.

Intro to C# Programming Lesson 7 Chapter 2

Comparing Strings

When comparing Unicode values of two strings with *s1* comes AFTER *s2*, what will be the Value- Returned Comparison Function's value?

Comparison of s1 and s2: s1 comes after s2 Value Returned from String.Compare(s1, s2): AN *int* GREATER THAN ZERO

When comparing Unicode values of two strings with *s1* comes BEFORE *s2*, what will be the Value- Returned Comparison Function's value?

Comparison of s1 and s2: s1 comes before s2 Value Returned from String.Compare(s1, s2): AN *int* LESS THAN ZERO

When comparing Unicode values of two strings with *s1* IS SAME AS *s2*, what will be the Value- Returned Comparison Function's value?

Comparison of s1 and s2: s1 is the same as s2 Value Returned from String.Compare(s1, s2): THE *int* ZERO

Why is the first reason it not good programming to use the the *!=* operator to write the *Equals(* *)* *function* when comparing the value of STRINGS?

First of all, In general, you cannot use these operators on objects, and therefore, you would be wise not to get used to using them.

What role does ignoring case play out in programming WHEN COMPARING STRINGS' VALUES ?

For example, that means that *A* would be treated as equal to *a*

Are Simple Variables and Variables that are created from classes the same component of programming?

For now, just realize that they are much different than simple variables.

What does it mean if you use the following IF Statement IN THE COMPARISON OF THE *VALUE* OF STRINGS ? if (String.Compare("A", "a", false) == 0) Console.Out.WriteLine("Equal");

If you put false in the ignore case, then the case will work the same as this: String s1 = "Adam"; String s2 = "Barbara"; if (String.Compare(s1, s2) < 0) Console.Out.WriteLine(s1 + " comes before " + s2);

What does it mean if you use the following IF Statement in the COMPARISON OF STRINGS' VALUES? if (String.Compare("A", "a", true) == 0) Console.Out.WriteLine("Equal");

If you put true in for ignore case, then the case is ignored. This program segment would display the word *Equal* because the true argument tells the function to ignore the case of the characters.

Describe the organization of the Unicode.

In Unicode, as you learned earlier in the course, the letters are in alphabetical order with all of the capital letters coming before all the lowercase letters.

Demo two reasons why it is easier to test for EQUALS rather than using the Compare( ) function WHEN COMPARING THE *VALUE* OF STRINGS?

It would be better to use Equals() for two reasons. The first reason is because using Equals() makes your code easier to read. Second, you will later find that most classes have an Equals() function defined,

With this example of coding, why wouldn't the program compile when you tried to compared these two strings with an OUT WriteLine Command? String s1 = "Adam"; String s2 = "Barbara"; if (s1 < s2) Console.Out.WriteLine(s1 + " comes before " + s2);

Let me show you with an example: If you wrote this code in a program, it would not compile. THIS IS BECAUSE C# DOES NOT ALLOW YOU TO MAKE COMPARISONS BETWEEN OBJECTS UNLESS YOU USE THE *Value-Returning* *Function* named *Compare(* *)*

What is special about OBJECTS?

Objects are variables that are created from classes. These variables are things that can hold more than one value, and they can have methods associated with them.

Define the term, OBJECTS

Objects are variables that are created from classes. These variables are things that can hold more than one value, and they can have methods associated with them. You will learn a lot more about classes and objects later in the course. For now, just realize that they are much different than simple variables.

Why would the fact that MOST CLASSES have an Equals() function defined, be a good reason to test for EQUALS rather than using the Compare( ) function WHEN COMPARING THE VALUE OF STRINGS ?

Second, you will later find that most classes have an Equals() function defined, which allows for comparison between two objects.

Why does the fact that *strings* are OBJECTS make them different from any other variable?

Since strings are objects, they behave differently than other variables.

Why is the second reason that using *!=* as an operator important to obey?

So, if you get used to using them in C# and decide later to program in another language, you will be setting yourself up for problems.

What would be the proper Compare( ) function to rewrite this code segment when trying to COMPARE STRINGS? String s1 = "Adam"; String s2 = "Barbara"; if (s1 < s2) Console.Out.WriteLine(s1 + " comes before " + s2);

String s1 = "Adam"; String s2 = "Barbara"; if (String.Compare(s1, s2) < 0) Console.Out.WriteLine(s1 + " comes before " + s2);

How would the Compare( ) function differ for the example below if you wanted to ignore case WHEN COMPARING STRINGS' VALUES ? String.Compare(string s1 , string s2

String.Compare(string s1 , string s2 , boolean ignoreCase) You will notice that the syntax is almost the same as the previously discussed version of Compare() except that this function also takes a Boolean value.

For this example, what would be the syntax for CALLING the *Value-Returning* *Function* named *Compare(* *)*

String.Compare(string s1 , string s2)

What are *strings* considered in programming?

Strings are OBJECTS

WHEN DOES USE OF THE *Equals(* *)* *function* work naturally WHEN COMPARING THE VALUE OF STRINGS?

The Equals() function works very naturally. That is, if the two objects contain the same value, then the function returns true. Otherwise, the function will return false.

Why would it making code easier to read be a good reason to test for EQUALS rather than using the Compare( ) function WHEN COMPARING THE VALUE OF STRINGS?

The first reason is because using Equals() makes your code easier to read. And remember that the easier your code is to read, the easier it will be to debug.

Memory Limits allowed for simple data is limited in comparison to the limits allowed for string values. Why?

The reason that strings can have an arbitrary size and hold an arbitrary number of characters is because strings are objects

At first, the values of the Compare( ) function may be hard to remember. What is an easy technique to use to remember comparisons of strings?

The way I always remember it is that if the two values are the same, then zero is returned. If the first value comes before, or is less than the second value, then you get a number that is less than zero. However, if the first value comes after, or is greater than the second value, then you get a number that is greater than zero.

Is it good programming to use the *!=* operator to write the *Equals(* *)* *function* when comparing the value of STRINGS?

There is one last point I want to make about testing for equality between strings. You may find out by accident that C# allows you to test for equality and inequality using the == or != operators. While this may seem like a better way to code, because there is less to type or remember, it is not good programming style.

What is the role of the *Value-Returning* *Function* named *Compare(* *)*

This function will compare the values of the strings, character by character, using the character's Unicode value.

If you do a little research into *class* *String*, what will you find about using the Compare( ) function?

You will find that there are many different ways to use Compare. One very useful version of Compare() will compare the characters, ignoring the case of the letters. there are many other versions of the Compare() function, but for simplicity, they will not be addressed here.

How would you resort to use of the *Equals(* *)* *function* in an attempt to correct the following code which resulted in an ERROR CODE DURING COMPILING? String s1 = "Adam"; String s2 = "Barbara"; if (String.Compare(s1, s2) < 0) Console.Out.WriteLine(s1 + " comes before " + s2);

if (String.Equals(s1, s2))

Define the term, Simple Type Data

numbers, and characters

What must you use to COMPARE STRINGS?

*Value-Returning* *Function* named *Compare(* *)* in order for the comparison to compile the program successfully

What are the three types of comparisons which can be made of Unicode Values?

*s1* comes BEFORE *s2* *s1* IS THE SAME AS *s2* *s1* comes AFTER *s2*


Conjuntos de estudio relacionados

Texas Principles of Real Estate I - Ch 5

View Set

Class 3: Molecular Biology (see notes)

View Set

Arab-Israeli Conflict: The Six Day War 1967

View Set