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!

TOPICS

   

        -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   TabControl
                 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
                }

            int x;
            bool ok = int.TryParse(txtIn.Text, out x);
            if(ok == false)
               return; // goes no further if bad input

        -Output
            Must be a string or converted to a string
            Text Property of any control
               TextBox.Text =
               Label.Text =
               btnDoIt.Text = 
               lblOut.Text = 
            ListBoxes and MessageBoxes   
               ListBox.Items.Add( )
               MessageBox.Show( )

        -Conditions (comparison) - always true or false
            relational operators  <     >     <=     >=     !=     ==
            logical operators     &&    ||     !
            compound conditions
                // if x greater than 0 AND x is less than 10 (1-9)
                if(x > 0 && x < 10)

                // if x is divisible by 2 OR 3
                if(x%2 == 0 || x%3 ==0)

        -Branching (selection):
           if(condition)              if(condition)           if(condition)
           {                          {                       {

           }                          }                       }
           else if(condition)         else
           {                          {

           }                          }

        -Looping (iteration):
           int i = 0;
           while( i < 10 )         
           {
              loop 10 times
              i++;
           }                          

           for( i=0; i<10; i++ )
           {                          
             loop 10 times
           }                          

        -Random Numbers
            // create a random object from the Random class
            Random rnd = new Random();
            x = rnd.Next( 1, 101 ); // get number  1-100

        -Files as input
             using System.IO;

             StreamReader reader;
             reader = new StreamReader( "File.txt" );
                 OR
             reader = File.OpenText( "File.txt" );

             string s = reader.ReadLine(); // reads 1 line of file as a string
             OR
             int x = int.Parse(reader.ReadLine()); // reads 1 line & converts to int

        -Methods, Parameters
             private void doIt()
             {
                MessageBox.Show("Hello");
             }
             
             doIt();

             private void doIt2( string herName )
             {
               MessageBox.Show("Hello " + herName);
             }

             doIt2("Barb");

             --------------------------------------------
        -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 object via constructor
               StreamReader reader = new StreamReader("data.dat");


EXAMPLES
QUESTION TYPES: Multiple Choice (50 questions) Write a SINGLE statement (one semicolon) (10 questions) Read / Write code (5 questions) Given: int i = 1, sum = 0; while (i <= 5) { sum += i; i++; } MessageBox.Show( sum.ToString() ); What is the output? a. 5 b. 10 c. 14 d. 15 e. 16 What would the output be if i was initialized to 5 rather than 1? a. 5 b. 10 c. 14 d. 15 e. 16 --------------------------------------------------------------- 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, ok); 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.
Generate a random number between 1 and 100 and report its value using a MessageBox if it is divisble by 7
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.