Tuesday, March 25, 2014

Final Review_27_28 (Message Box with Array)

Ex 27.    Use a loop to add all the negative values in the array above. Display the sum in a message box.
Ex 28.    Use a loop to determine the average value of only the positive values in the array. Display the average value.


//=================Ex 27==============================
        private void btnNegSum_Click(object sender, EventArgs e)
        {

Final Review_24_26 (Message Box)

Ex 24.    Create an array of doubles and  initialize it with the following values:             
-3.5, 2.3, 5.9, -6.9, -12.1, 15.6, 4.7, -3.8, -17.3, -11.4, 12.6, 19.9
{

Final Review_23 (Message Box)

Ex 23.    Use a loop to count the number of values, between 1000 and 2000, that are divisible by 7. Display the count in a Messagebox.

namespace Final_Review_23
{

Final Review_19_22 (Loop)

Ex 19.    Write a program that allows the user 10 tries to answer the question: “Which U.S. President was born on July 4?” After three incorrect guesses the program should give the hint: “He once said, ‘if you don’t say anything, you won’t be called upon to repeat it.’ “. After seven incorrect guesses, the program should give the hint “His nickname was ‘Silent Cal.’”. Note: Calvin Coolidge was born on July 4, 1872.
                {
                        string answer = "Calvin Coolidge";
                        string userGuess;

Final Review_14_18 (For loop, while loop, do while loop)

//==========================Ex 14============================
Ex 14.    Display all the numbers between 29 and 99.
                    {
                        for (int counter = 29; counter <= 99; counter++)
                        {
                            Console.WriteLine("Counter no: {0}", counter);
                        }
                    }

Final Review_8_13(If_else if_else)

namespace Final_Review_8_13
{
    class Program
    {

Final Review_1_7(Boolean Expression)

        static void Main(string[] args)
        {
            int x = 0;
            //Write boolean expression to:
            bool b1, b2, b3, b4, b5, b6, b7;
            //1.    Check if x is greater or equal to 9
                   b1 =  (x >= 9);
                    Console.WriteLine(b1.ToString());
            //===================================================
            //2.    Check if x is not greater than -5
                   b2 = (x <= -5);
                   Console.WriteLine(b2.ToString());
           //===================================================
            //3.    Check if x is between -5 and 5
                   b3 = (x > -5 && x < 5);
                   Console.WriteLine(b3.ToString());
            //===================================================
            //4.    Check if x is either greater or equal -2 or less than -5
                   b4 = (x >= -2 || x < -5);
                   Console.WriteLine(b4.ToString());
            //===================================================
            //5.    Check if x is not equal to 2,4, and 7
                   b5 = (x != 2 || x != 4 || x != 7);
                   Console.WriteLine(b5.ToString());
           //===================================================
            //6.    Check if x is equal to 3 or is between 11 and 19  (both not inclusive)
                   b6 = (x == 3 || x > 11 && x < 19);
                   Console.WriteLine(b6.ToString());
           //===================================================
            //7.    Check if x is not in the range -11 to 11
                   b7 = (x < -11 || x > 11);
                   Console.WriteLine(b7.ToString());

            Console.ReadLine();
        }

//=================1. Example with Method=====================
 static void Main(string[] args)
        {
            Console.Write("Enter x value: ");
            int x = int.Parse(Console.ReadLine());

            bool b1 = BooleanMethod(x);
            Console.WriteLine(b1);

            Console.ReadKey();
        }
        static bool BooleanMethod(int x)
        {
            bool b1 = x >= 9;
            return b1;
        }
    }
}

Monday, March 24, 2014

Question No 40_45(Random, Array_/Method)

Ex 40.    Within a loop, you are to generate random values between 100 and
     200, until the sum of all the random values exceed 2000. Within
    the loop displays the random value as well as the accumulated
    sum.
        static void Main(string[] args)
        {
            Random rand = new Random();
            int rdNo;
            int sum = 0;
            int counter = 1;
            while (sum <= 2000)
            {
                rdNo = rand.Next(100, 201);
                sum += rdNo;
                counter++;
                Console.WriteLine("Radom No: {0  }  Accumulated Sum: {1}", rdNo, sum);
            }
            Console.ReadKey();
        }
//==================code for question no 41 & 42================
Ex 41.    Create a one dim array A and initialize it with the values: 4,
     33, -21,-3, 43, 1, -5
Ex 42.    Create a one dim array B and initialize it with the values: 2.4,
     4.3, 5.6, 6.4, 9.5, 3.6, 7.3
        static void Main(string[] args)
        {
            //==================Ex 41==============================
            int[] arrayA = { 4, 33, -21, -3, 43, 1, -5 };

            //==================Ex 42==============================
            double[] arrayB = { 2.4, 4.3, 5.6, 6.4, 9.5, 3.6, 7.3 };
        }
//==================code for question no 43 & 44================
Ex 43.    Write a method 'getPositiveCount' that takes a one-dimensional
     array, counts the number of values in array that are greater
     than 0 and returns that count.

Ex 44.    Write a method 'getRangeCount' that takes a one-dimensional
     array, counts the number of values in the array that are between
     1 and 6 (both inclusive), then returns the count.
        static void Main(string[] args)
        {
            int[] arrayA = { 4, 33, -21, -3, 43, 1, -5 };

            //Ex 43: call method
            int result43 = GetPositiveCount(arrayA);
            Console.WriteLine("Positive count is: {0}", result43);

            //Ex 44: call method
            int result44 = getRangeCount(arrayA);
            Console.WriteLine("Range count is: {0}", result44);


            Console.ReadLine();
        }
        //=================Ex 43===========================
        static int GetPositiveCount(int [] arrayA)
        {
            int countPositive = 0;
            for (int i = 0; i < arrayA.Length; i++)
            {
                if (arrayA[i] > 0)
                    countPositive++;
            }
            return countPositive;
        }
        //=================Ex 44===========================
        static int getRangeCount(int [] arrayA)
        {
            int countInRange = 0;
            for (int i = 0; i < arrayA.Length; i++)
            {
                if (arrayA[i] >= 1 && arrayA[i] <= 6)
                    countInRange++;
            }
            return countInRange;
        }
    }
}
//==================code for question no 45================
Ex 45.    Write a method that takes 3 integer parameters and returns the
     average of the highest 2 parameters.

        static void Main(string[] args)
        {

            Console.Write("X: ");
            int X = int.Parse(Console.ReadLine());
            Console.Write("Y: ");
            int Y = int.Parse(Console.ReadLine());
            Console.Write("Z: ");
            int Z = int.Parse(Console.ReadLine());

            double result = avgOf2highParameters(X, Y, Z);

            Console.WriteLine("Avg of 2 high parameter: {0}", result);

            Console.ReadLine();

        }
        static double avgOf2highParameters(int X, int Y, int Z)
        {
            double average = 0;
            if (X < Y && X < Z)
            {
                average = (Y + Z) / 2.0;
            } 
            else if (Y < X && Y < Z)
            {
                average = (X + Z) / 2.0;
            }
            else if (Z < X && Z < Y)
            {
                 average = (X + Y) / 2.0;
            }
            else
            {
                Console.WriteLine("Do not repeat the three value");
            }
            return average;
        }

Question No 38(For loop) & 39(Six random dice)

Ex 38.    Write a for loop to compute the sum of all the consecutive values between 10,000 and 20,000.Display the final result sum.
        static void Main(string[] args)
        {
            double sum = 0;
            for (int i = 10000; i < 20000; i++)
            {
                sum += i;
            }
            Console.WriteLine("Sum: {0}", sum);
            Console.ReadLine();
        }
//===================code for Q 39========================
Ex 39.    You are to throw a dice and gain points according to the outcome. The throw of the dice is simulated by generating a random number from 1 to 6.
You gain 2 points when you throw a 1
You gain 4 points when you throw a 2
You lose 3 points when you throw a 3
You gain a point when you throw a 4
You gain 2 points when you throw a 5
You lose 5 points when you throw a 6
Use a for loop to run 12 throws. Within the loop add up all the gains and losses according to the outcomes of the throws. Use a switch to set up the decision tree. Display the final accumulated points.
        static void Main(string[] args)
        {
             int[] dieRoller = { 1, 2, 3, 4, 5, 6 };
             Random rand = new Random();
             int points = 0;
            for (int i = 0; i < 12; i++)
            {
                int dieRoll = rand.Next(1, 7);
                switch (dieRoll)
                {
                    case 1:
                        dieRoller[0]++;
                        points += 2;
                        break;
                    case 2:
                        dieRoller[1]++;
                        points += 4;
                        break;
                    case 3:
                        dieRoller[2]++;
                        points -= 3;
                        break;
                    case 4:
                        dieRoller[3]++;
                        points += 1;
                        break;
                    case 5:
                        dieRoller[4]++;
                        points += 2;
                        break;
                    case 6:
                        dieRoller[5]++;
                        points -= 5;
                        break;
                }
                Console.WriteLine("Dice roll {0} Point {1}", dieRoll, points);
            }
            Console.WriteLine("\nYou have earned {0}", points);
            Console.ReadLine();
        }

Question No. 36(Boolean) & 37(If_else if_else)

Ex 36.    Let X=10 and Y=6, evaluate the following boolean expression to true or false:
 (X > 9 && (Y >= X || Y >= 6)

        static void Main(string[] args)
        {
            int x = 10, y = 6;
            bool b1 = (x > 9 && (y >= x || y >= 6));

            Console.WriteLine("Boolean expression: {0}", b1);
            Console.ReadLine();

        }
//==============code for Question no 37==========================
Ex 37.    Request and read four integer values V1, V2, V3, and V4. Subtract 5 from the highest value and display it.        static void Main(string[] args)
        {
            Console.Write("Enter v1: ");
            int v1 = int.Parse(Console.ReadLine());
            Console.Write("Enter v2: ");
            int v2 = int.Parse(Console.ReadLine());
            Console.Write("Enter v3: ");
            int v3 = int.Parse(Console.ReadLine());
            Console.Write("Enter v4: ");
            int v4 = int.Parse(Console.ReadLine());

            int maxVal = 0;

            if (v1 > v2 && v1 > v3 && v1 > v4)
            {
                maxVal = v1;
            }
            else if (v2 > v1 && v2 > v3 && v2 > v4)
            {
                maxVal = v2;
            }
            else if (v3 > v1 && v3 > v2 && v3 > v4)
            {
                maxVal = v3;
            }
            else if (v4 > v2 && v4 > v3 && v4 > v1)
            {
                maxVal = v4;
            }
            else
            {
                Console.WriteLine("Please enter 1 value for one time");
            }
            int result = maxVal - 5;
            Console.WriteLine("Result: {0}", result);
            Console.ReadLine();
        }
    }
}

Question No. 32 _35(Decimal, Mod, Bool)

//=================Code for Question No 32=============================== 
Ex 32.    Calculate the volume (ONLY) of a cone. Request radius and height from user, calculate the volume and display it, with 2 decimal points.


 
        static void Main(string[] args)
        {
            Console.Write("Enter radius: ");
            double r = double.Parse(Console.ReadLine());
            Console.Write("Enter height: ");
            double h = double.Parse(Console.ReadLine());

            double volume = 1 / 3.0 * Math.PI * Math.Pow(r, 2) * h;

            Console.WriteLine("Volume: {0:f2}", volume);
            Console.ReadLine();
        }
    }
}
//=================Code for Question No 33===============================
Ex 33.    Request two integer values. Divide the larger value by the smaller value. Display quotient and remainder.

        static void Main(string[] args)
        {
            Console.Write("Enter first integer x: ");
            int x = int.Parse(Console.ReadLine());
            Console.Write("Enter second integer y: ");
            int y = int.Parse(Console.ReadLine());
            double remainder, quotient;
            if (x > y)
            {
                quotient = x / y;
                remainder = x % y;
            }
            else
            {
                quotient = y / x;
                remainder = y % x;
            }
            Console.WriteLine("Quotient: {0}", quotient);
            Console.WriteLine("Remainder: {0}", remainder);
            Console.ReadLine();
        }
 //=================Code for Question No 34===============================
Ex 34.    Write a boolean expression that evaluates to true, when X is greater or equal to Y and Y is different from 12.        static void Main(string[] args)
        {
            Console.Write("Enter value x: ");
            int x = int.Parse(Console.ReadLine());
            Console.Write("Enter value y: ");
            int y = int.Parse(Console.ReadLine());

            bool b1;
            b1 = (x >= y && y != 12);
            Console.WriteLine("b1: {0}", b1);
           
//=================Code for Question No 35===============================
Ex 35.    Write a boolean expression that evaluates to true, when X is either less than half Y, or greater than 10.
            bool b2 = ((x < y/2) || (x > 10));

            Console.WriteLine("b2: {0}", b2);
            Console.ReadLine();
        }

Question No 31(MessageBox)

Ex 31.    Read an integer value from a textbox (txtValue). Use the TryParse to read the value. Display error message when the value is not valid.


        private void btbCheck_Click(object sender, EventArgs e)
        {
            int value;
            if (int.TryParse(textBox.Text, out value))
                MessageBox.Show("Valid Input");
            else
                MessageBox.Show("Invalid Input");
        }
    }
}

Question 30(Weekly wage with array)

Ex 30.    In a certain city, police officers receive extra pay for weekend duties. Their hourly wage is increased by 50% for Saturday work and doubled for Sunday.
Write a program that requests the name  of the officer, the hourly wage, and the number of hours worked each day, and return the weekly salary
 Use a loop to request the hours worked each day.
(Hint use an array of size 7 to hold the hours worked each day, you may also use another array of 7 to hold the names of each day of the week (Sun, Sat, Mon, Tues, Wed, Thur, Fri)

        static void Main(string[] args)
        {
                double [] hr = { 0, 0, 0, 0, 0, 0, 0 } ;
                string [] day = { "sun", "mon", "tue", "wed", "thur", "fri", "sat" };
                double weeklysalary = 0;
                    Console.Write("Enter your name: ");
                    string name = Console.ReadLine();
                    Console.Write("Enter your hourly wage: ");
                    double wage = double.Parse(Console.ReadLine());

                for (int i = 0; i < hr.Length; i++)
                {
                    Console.Write("Enter amount of hour on {0} you work: ", day[i]);
                    int wkhour = int.Parse(Console.ReadLine());
                    hr[i] = wkhour;
                }
                for (int i = 0; i < day.Length; i++)
                {
                    if (day[i] == "sat")
                    {
                        weeklysalary += hr[i] * (wage * 1.5);
                    }
                    else if (day[i] == "sun")
                    {
                        weeklysalary += hr[i] * (wage * 2);
                    }
                    else
                    {
                    weeklysalary += hr[i] * wage;
                    }
                }
                Console.WriteLine("Office name: {0}", name);
                Console.WriteLine("Weekly Salary is: {0:C}", weeklysalary);
                Console.ReadLine();
        }
    }
}

Question No. 29(Parallel Array with Form)

Ex 29.    The salespeople at a health club keep track of the members who have joined in the last month. Their names and types of membership, Bronze, Silver, or Gold are stored in 2 parallel array. One array holds member names and the second array holds their membership types (“Silver”, “Bronze”, or “Gold”). Create two arrays of size 20 and initialize these arrays directly during ‘array creation’. Load 20 names in one and membership types in the other one. Write a program that provide a menu to perform the following functions:
  1. Display all the members’ name only
  2. Request a name from user and display his/her membership type 
  3. Request a name and change  his/her membership type
  4. Request a membership type (either Silver, Bronze, or Gold), then display all the names with the given membership type
  5. Exit

namespace Final_Review_Ex_29
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] namesA = { "Joe", "Bob", "John", "Jack", "Jill", "Homer", "Sally", "Wilma", "Jimmy", "Jason", "Michal", "Kellan", "Sara", "Silvia", "Janelle",

"Yulka", "Yevette", "Elizabeth", "Silvester", "Alice" };
        string[] mbtypeA = { "Gold", "Silver", "Silver", "Bronze", "Bronze", "Gold", "Silver", "Silver", "Bronze", "Bronze", "Gold", "Silver", "Silver", "Bronze",

"Bronze", "Gold", "Gold", "Silver", "Gold", "Gold" };

        private void btnMemberOnly_Click(object sender, EventArgs e)
        {
            txtMainBox.Clear();
            for (int i = 0; i < namesA.Length; i++)
            {
                txtMainBox.Text += i.ToString() +" "+ namesA[i] + "\n";
            }
        }

        private void btnMtypes_Click(object sender, EventArgs e)
        {
            txtMainBox.Clear();
            string getname = txtNforM.Text;
            for (int i = 0; i < namesA.Length; i++)
            {
                if (getname.ToLower() == namesA[i].ToLower())
                {
                    txtMainBox.Text = mbtypeA[i];
                    break;
                }
                else
                {
                    txtMainBox.Text = "Name doesn't exist";
                }
            }

        }

        private void btnChangeMT_Click(object sender, EventArgs e)
        {

            string getname = txtN2change.Text;
            for (int i = 0; i < namesA.Length; i++)
            {
                if (getname.ToLower() == namesA[i].ToLower())
                {
                    if (txtmbtype.Text.ToLower() == "gold" || txtmbtype.Text.ToLower() == "silver" || txtmbtype.Text.ToLower() == "bronze")
                    {
                        mbtypeA[i] = txtmbtype.Text;
                        txtMainBox.Text = String.Format("{0}'s membership type has changed to {1}", namesA[i], mbtypeA[i]);
                        break;
                    }
                    else
                    {
                        MessageBox.Show("Please enter a valid service level");
                        break;
                    }
                }
                else
                    txtMainBox.Text = "Name doesn't exist";
            }

        }

        private void btnMtoN_Click(object sender, EventArgs e)
        {
            string membershiptype = txtReMbt.Text;
            for (int i = 0; i < mbtypeA.Length; i++)
            {
                if (membershiptype.ToLower() == mbtypeA[i].ToLower())
                {
                    txtMainBox.Text += namesA[i] + "\n";
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

Sunday, March 23, 2014

Queries Key Terms

Queries

Criteria
When you want to limit the results of a query based on the values in a field, you use query criteria. A query criterion is an expression that Access compares to query field values to determine whether to include the record that contains each value.
Sort
Sorting means organizing records in a meaningful way so that you can retrieve data faster and in an order of your choice. For example, if you want to view records in the ascending order of the last name of volunteers, you can sort the records based on the values in the last name field.
[Field Name]
In query, Field Name is written inside bracket [ ]
Arithmetic operators

You use the arithmetic operators to calculate a value from two or more numbers or to change the sign of a number from positive to negative or vice versa.
Logical Operator
You use the logical operators to combine two Boolean values and return a true, false, or null result. Logical operators are also referred to as Boolean operators.
Value
the numerical amount denoted by an algebraic term; a magnitude, quantity, or number.
Function
functions must be expressed as function calls (e.g. sum(a,b) instead of a+b)
See more on: http://wiki.apache.org/solr/FunctionQuery#What_is_a_Function.3F

Access Data Types and Relationship



Data Type
Use for
Text
Use for text or combinations of text and numbers, such as addresses, or for numbers that do not require calculations, such as phone numbers, part numbers, or postal codes. Stores up to 255 characters. The Field Size property controls the maximum number of characters that can be entered.
Number
Use for data to be included in mathematical calculations, except calculations involving money (use Currency type).
Date/Time
Use for dates and times. Stores 8 bytes.
AutoNumber
Use for unique sequential (incrementing by 1) or random numbers that are automatically inserted when a record is added. (Note: the underlying data type for AutoNumber is Number, Long Integer.)
Memo
Use for lengthy text and numbers, such as notes or descriptions. Stores up to 65,536 characters.
Currency
Use for currency values and to prevent rounding off during calculations. Stores 8 bytes.
Yes/No
Use for data that can be only one of two possible values, such as Yes/No, True/False, On/Off. Null values are not allowed. (Stores 1 bit.)
OLE Object (Object Linking and Embedding)
Use for OLE objects (such as Microsoft Word documents, Microsoft Excel spreadsheets, pictures, sounds, or other binary data) that were created in other programs using the OLE protocol. {Stores up to 1 gigabyte (limited by disk space)}.
Hyperlink
Use for hyperlinks. A hyperlink can be a UNC path or a URL.
Attachment
Files, such as digital photo, Multiple files can be attached per record. This data type is not available in earlier versions of Access.



Relationships
One to many
In a one-to-many relationship, a record in Table A can have many matching records in Table B, but a record in Table B has only one matching record in Table A.
Primary Key
The Unique Identifier for each Record in a Table.
Foreign Key
Foreign key is used to link a primary key of another table. (A foreign key is a field in a relational table that matches the primary key column of another table. The foreign key can be used to cross-reference tables.)
Naming Conventions
Prefix for 3 major objects in access
(tbl, frm, qry)