Wednesday, August 25, 2010

Hi LO Guessing Game

Study the code below;

Public Class Form1
Dim secretnum As Integer = Int (10 * Rnd() ) + 1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text <>Then TextBox2.Text = "higher"
If TextBox1.Text > secretnum Then TextBox2.Text = "lower"
If TextBox1.Text = secretnum Then TextBox2.Text = "correct"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Randomize ()
Secretnum = Int (10 * Rnd() ) + 1
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class


Answer the following:
1. How many objects are referred to? What are they?
2.What is the name of the variable referred to?
3. What are the three text options that appear in textbox1?
4. What do you think this code does?
5. Build this code into a project and get it to work!!


Simple Timer

Simple Timer
The timer object is quite simple to use. It is an invisible object that runs in the background of a form. The timer has one event, a timer event occurs every time the time interval expires.

For example if you set the timer interal to 1000(1 second), then every second a timer event will occur. You can code what happens when the timer event is triggered, just make sure the timers enabled property is set to true.

For Example:
Define a variable in the form-
Dim clock as integer

Place the following code in the timer event
clock=clock+1
label1.text=clock

You could add two buttons, one to start the timer and one to stop the timer. Starting the timer would involve setting the "clock" variable to 0 and the timer enabled property to true. Stop the timer simply involves setting the timer enabled property to false.

Tuesday, August 17, 2010

Variables

Variables are "containers" for values that can be used by a program. Variables can contain numbers or text. Variables are given names so they can be referred to in the programming code.

Today, I will create three variables, one called "point", one called "number", and one called "name".

Program used: Microsoft Visual Basic
Once I had a new form, I had to add three buttons from the toolbox to the form and change the names from:
* Button1 to Whole Number
* Button2 to Decimal Number
* Button3 to Name
I then added 3 Labels one beside each of the buttons and then a text box below the Name button.

I then changed the codes for the buttons:
Private Sub Button1_Click (ByVal sender As System. Object, ByVal e As System. EventArgs)
Handles Button1.Click
number = Rnd ()
Label1.Text = number
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System. EventArgs)
Handles Button2.Click
point = Rnd ()
Label2.Text = point
End Sub

Private Sub Button3_Click (ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
myname = TextBox1.Text
Label3.Text = myname
End Sub

The following are the screenshots of my end result for this class task.
T
he first screenshot is of the codes which is what makes the program run.
The second screenshot is of the actual program/form that I made.

Control Structures-Repitition

During class we all had a choice to pick a topic to research, so we could talk about it to everyone next lesson I chose Control Structures Repitition. The following is what I found:
This type of control structures specify a block of one or more statements that are repeatedly executed until a condition is satisfied. These are also called iteration control structures.
There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is placed:
-at the beginning of the loop (leading decision loop)a
-at the end of the loop (trailing decision loop)
-a counted numberof times (counted loop)
1. Leading Decision Loop:
In such loops the testing of the condition is at the beginning of the loop. Examples are DO-WHILE loops. The only way to terminate the loop is to render the DO-WHILE loop false.
2. Trailing Decision Loop:
In such structures the testing of condition is done at the end of loop except that the logic to keep on repeating is same as in case of other repitition structures. REPEAT...UNTIL is an example of trailing decision loop. In trailing loops the statems ase executed at least once before the condition is tested. These loops are sued in almost all kinds of computer program e.g. to print student records, to continue taking imput until a certain limit etc.

3. Counted Loop:
These are also known as DO or FOIR loops. In such kind of repitition structures the programmer knowns in advance the exact number of loop iterations. The loop execution is controlled by an important variable called the loop index. We can now conclude that selection control structures and repitition control structures find their use in almost any application. Ising them wisely and judiciously depends on the person who is going to use these loops. Proper implementation of loop structures can reduce program complexity and cost a lot.

Some problems require a sequence of tasks to be processed repeatedly, e.g.:
-apply same processing to a set of different data items
-processing until and target point is reached
A block of statements that are to be repeated
-A condition that will determine if the loop processing should stop
-The condition is evaluated once in each iteration
-A variable that will change each time the loop is processed
A variable that will change each time the loop is processed
-tested in the condition
The following image is a visual, an example for you to see what is meant by "the loop" or repitition in a simple flow chart like this.



Tuesday, August 10, 2010

Heads & Tails

Last Wednesday on August 4, we had a task called heads & tails where we had to run the program Microsoft Visual Basic, and create a new project with a form. Then we had to drag the label, button and text box from the toolbox to the form and change the name/property from BUTTON1 to CHECK and then edit the code for the label by inserting
If TextBox1.Text = "heads" Then Label1.Text= "win"
If TextBox1.Text = "tails" Then Label1.Text = "lose"
So then when you type heads into the text box the label at the top of the form will say win, and then if you type tails into the text box the label will say lose.