202 test 2
Test information may change! Come to class to stay on top of details.
Test may NOT be "made up" without prior arrangements or VERIFIED EMERGENCY!

Make a CAP!
This guide is not a "promise" of what will be on the test, but is simply an outline of topics that MAY be on the test that I provide to be helpful to you. Other topics not listed may also sneak onto a test from time to time.
Understand that a test is a SAMPLING and can not possibly cover all information from class. The idea, however, is that you must study all the information since you don't know for certain what questions will show up. This is for your benefit as it motivates you to learn more!


-Algorithms finite # of steps taken to solve a problem often IPO may involve repeating actions (loops) may involve selecting actions (if) basis for a program -Computer and Programming Environment HW, SW Compiler converts C# to binary code Visual Studio IDE Big 5: Designer, Code Editor, Toolbox, Properties, Solution Explorer -Windows programs GUI Classes and objects Properties Methods Events -Event-driven code click, text_changed, hover, tick -Controls Form Button TextBox ListBox Label PictureBox RadioButton CheckBox GroupBox Tab TextBox Methods: AppendText() Focus() SelectAll() Properties: Text CheckBox, RadioButton Checked Property CheckedChanged Event (check if checked) use with "if" Form Load Event this Visible Property vs. Enabled Property -Variables Data types int, double, string, bool Naming meaningful Declare, Initialize int i; double x = 789.88; string s=""; Class level variables (fields) Scope Lifetime local variables declared WITHIN a method scope is only within that method lifetime id life of method class-level variables (fields) declared in the class but outside of any method scope is that they are visible to any method in the class lifetime is that they hold their value for the life of the program -Assignment = "gets" val <-- expression variable: x = 99; property: txtOut.Text = "Hello"; -Strings strings represented in "double quotes" Strings NO conversion needed to input or output string s = txtIn.Text; MessageBox.Show(s); comparison if (s == "hello") -Arithmetic & Number Types + - * / % PEMDAS x = y + z * 3; x = 7 % 3; - Accumulation counters initialize (usually to 0) ++, -- int x=99; MessageBox.Show(++x); MessageBox.Show(x++); +=, *=, -=, /= txtOut.Text = "Words"; txtOut.Text += " and more words"; int x = 3; x += 2; string s = "This is"; s += " the end."; txtOut.Text += "xxx"; - adds to what is already in TextBox listOut.Items.Add("xxx"); - adds to what is already in ListBox -Conversion Conversion of Strings to Numbers explicit int.Parse( ), double.Parse( ) Conversion of Numbers to Strings explicit .ToString() .ToString("C") .ToString("P") .ToString("N3") .ToString("N0") implicit MessageBox.Show("The answer is " + x); careful! MessageBox.Show("The sum is " + x + y); MessageBox.Show("The sum is " + (x + y); Conversion of Numbers to Numbers implicit int to double // no problem int i = 99; double d = i; // small container into big cast needed double to int double d = 99.99; int i = (int)d; // truncates -Input TextBox.Text string s = txtIn.Text; Conversion of Numbers int.Parse( ), double.Parse( ) int x = int.Parse(txtIn.Text); -Handling Exceptions user input generally try { code } catch { error message } -Output Must be a string or converted to a string TextBox.Text = Label.Text = ListBox.Items.Add( ) MessageBox.Show( ) Text Property of any control = btnDoIt.Text = "Do It"; -Conditions (comparison) - always true or false relational operators < > <= >= != == logical operators && || ! compound conditions -Branching (selection): if(condition) if(condition) if(condition) { { { } } } else if(condition) else { { } } -Looping (iteration): while(condtition) for(i=0; i<10; i++) { { } } -Random Numbers Random rnd = new Random(); // create a random object from the Random class rnd.Next(0,101); -Files as input using System.IO; StreamReader reader; reader = new StreamReader( "File.txt" ); OR reader = File.OpenText( "File.txt" ); string s = reader.ReadLine(); -Methods, Parameters void DoIt() { MessageBox.Show("Hello"); } void DoIt(string herName) { MessageBox.Show("Hello " + herName); } -------------------------------------------- -Methods, returning values int Twice(int num) { return num * 2; } OR int Twice(int num) { int result; result = num * 2; return result; } int answer = Twice(9); MessageBox.Show(answer.ToString()); // displays 18 - OBJECTS CREATED FROM CLASSES: "instantiate", constructor method properties vs methods Button class => btnDoIt object (created through designer) Random class => rnd object ( created with new Random()) // declares a reference to the object & creates a new object via constructor Random rnd = new Random(); // declares a reference to the object & creates a new random object via constructor // declares a reference to the object & creates a new object via constructor StreamReader reader = new StreamReader("data.dat");
Multiple Choice (30) Write a SINGLE statement (one semicolon) Read / Write code Given: int i = 0; while (i < 5) { i++; } MessageBox.Show(i.ToString()); What is the output? a. 2 b. 3 c. 4 d. 5 e. 6 What would the output be if i was initialized to 5 rather than 0? a. 2 b. 3 c. 4 d. 5 e. 6 --------------------------------------------------------------- A variable needs to be at the class level (a field) if: i. it will be shared by more than one method in the class ii. it needs to maintain its value between uses by a method a.) i only b.) ii only c.) both i and ii d.) neither i nor ii --------------------------------------------------------------- Which is the proper way to use int.TryParse assuming x is an int vartiable and txtIn has the number to be parsed? i. bool ok = int.TryParse(txtIn.Text, out x); ii. x = int.TryParse(txtIn.Text); a.) i only b.) ii only c.) both i and ii d.) neither i nor ii ---------------------------------------------------------- Given the following method, void doIt(int x) { MessageBox.Show( (x * 2).ToString() ); } What is the output of the following? doIt(100); a. 200 b. 100 c. 102 d. 50 What is the output of the following? int x = 50; x++; doIt(x); a. 200 b. 100 c. 102 d. 50 WRITE A SINGLE C# STATEMENT TO... ------------------------------ increase the int variable, X, by 12 change the title of a form to "Math" if the int variabe X is bigger than 100, assign 999 to X show the value of the variable, cost, in txtOut as currency say "hello" in a messageBox if the integer in txtIn is divisible by 2 declare 5 int variables a,b,c,d and e and set them all to zero assign the average of 2 int variables, x and y to double z preserving any fractional result
WRITE CODE ---------- Get a number from a TextBox called txtIn and put it into an int variable called Num. Triple the int variable Num and write it to txtOut saying "The number is" followed by Num.
Report which radioButtons (radOne, radTwo) are checked in a MessageBox Report which checkBoxes (chkOne, chkTwo) are checked in a MessageBox
Report if the int value in txtIn is even or odd by displaying a message ('2 is even' or '99 is odd') in lblOut.
** Use TryParse to read a number and show it in a listBox if it is valid
Generate a random number between 1 and 100 and report its value using a MessageBox
Report which of the int variables a or b are the biggest
READ CODE --------- int x=0, y=0; while(y<10) { x+=2; y+=x; } Show variables in a grid to keep track of the values. x? y? AFTER loop?
-More Code Questions read code and answer questions read code and determine output read code and determine values of variables write a code snippet write a user created method write an event method Write code that will place the input in txtIn into an int variable called Num. Multiply Num by 3 and display it in a label called lblOut. Loop 100 times and output every EVEN number in a MessageBox. (harder) Create a method that will accept an int argument, i. Loop i times, printing out the square of i in a MessageBox each time only if i is divisible by 5.