assignment 18.2 new

Upload: lovestone69679

Post on 30-May-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Assignment 18.2 NEW

    1/16

    Assignment 18.2 Software Development (Visual Basic)

    The features of a Programming Language

    Variables (Naming conventions)

    Every piece of date used to make a program must be contained somewhere so that it can belooked back or reused. Programs store this in defined memory called variables. These variablescan be thought of as a temporary source of data. The contents of a variable ends when theprogram is terminated.

    The variable could be permanently available when the program is save so all tools used withinthe program will be accessible once closed. A variable have two very important attributeswhich make it unique, a name and a data type. A name is created by the programmer so it willbe easier to establish the variable within the coding of the program.

    A data type defines the sort of data a variable can store. Naming conventions for variables areusually expected to have a name with a letter at the beginning and a combination of both lettersand numbers which can consist of a maximum of 255 characters. Symbols are not acceptablewhen naming conventions.

    Variables (Local and Global)

    Global Variables

    A Global variable is a variable that is visible through the whole program and hose value may bemodified by any procedure in the program. So in simpler terms it is a variable that can assessed

    throughout a program instead of being restricted to a function.

    Local Variables

    A Local variable is used and visible only within the body of a procedure or function, and whichretains its value only for the duration of each call to that procedure. Within different procedures, different local variables may be declared with the same name, which eases the task of finding unique names, and avoids a whole class of errors in which a global variable isinadvertently altered by the wrong procedure.

    Throughout my program I declared number of different variables some local and some global.Below are samples of code from my program that I created which shows sections that have localvariables.

    Public Class FrmNoteSaverDim TheDate As DateTimeDim TheTime As DateTimeDim theResponse As String

    Variables declared within a separate module like this are limited to use within that module.When a variable is declared within a class this limits the overall use you have over that variableso you are restricted to most actions. This is what makes this particular section local.

  • 8/9/2019 Assignment 18.2 NEW

    2/16

    Loops

    Loops are mainly used for iterations so the command that you have stated will be repeateddepending on how many times you have set it to repeat. There are many types of loops howeverthere are only two main ones. These loops are:

    Fixed loops - which execute a fixed number of times Conditional Loops This executes the loop repeatedly until some condition is met

    Fixed Loops

    A Fixed loop like a said before follows a set of instructions as to how many times the programshould repeat itself. If I set the program to repeat itself 10 times, it will loop itself 10 times. Invisual basic these loops are implemented using for statements .

    Public Class FrmLoop

    Private Sub CmdFor_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles CmdFor.Click

    Dim A As IntegerA = 1For A = 1 To 10

    MsgBox(A)

    Next The code above supports a simple looping program that will independently loop itself 10

    times.

    For Loop Code

    X10

  • 8/9/2019 Assignment 18.2 NEW

    3/16

    Continuous Loop

    This Loop is similar to a Fixed Loop except there is a condition that needs to be met in order forthe loop to terminate. Throughout Visual Basic these loops are implemented using do LoopUntil statements.

    Private Sub CmdDo_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles CmdDo.Click

    Dim B As IntegerB = 1Do Until B = 10

    MsgBox(B)B = B + 1

    LoopEnd Sub

    The code above shows how a Do Loop can be written and executed, as you can see thecode is slightly different from the first one hence why they have different names.

    Conditional Statements

    Conditional statements allow a choice to be made, so the user can execute an event if acertain condition is met within the program. Below is sample code from my program toshow what this means.

    Public Class FrmSelection

    Private Sub TmrRun_Tick( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles TmrRun.Tick

    If CHBOption1.Checked Then

    PBBar1.Value = PBBar1.Value + 1

    ElseIf CHBOption2.Checked Then

    PBBar1.Value = PBBar1.Value - 1

    End If

    If PBBar1.Value >= 100 ThenTmrRun.Enabled = False

    End If

    End Sub

    Do Loop Code

    This is the Coding of the program, so

    this is considered very important. Thispart of the section makes the progressgo forward and back. Throughout the

    bottom of the coding, that part controlsthe number buttons from 1-4

    This code show a conditionalstatement as it basically says that if

    you click the Go Forwardcheckbox the progress bar will goforward by 1 and if you click theGo Back checkbox the bar will goback by 1. This is known as aConditional Statement .

  • 8/9/2019 Assignment 18.2 NEW

    4/16

    Logical Operators

    The conditional statements used in both If and Do Until are familiar with using logical operatorsto combine two or more conditions. Below is a table showing all the logical operators.

    Operator Meaning> Greater Than< Less Than= Equal to>= Greater Than or equal to20

    AND Gender = M this statement will onlyproduce true if Both elements are true(over 20 and Male)

    OR Produces true if either side is true Age>20OR Gender = M this statement willproduce true is EITHER the age is over 20OR the gender is Male

    NOT Produces the opposite result NOT(UserInput = Y) this statement will producefalse if the user input is Y, otherwise it willproduce true.

    Assignment Statements

    Assignment statements in VB are used to assign values to variables. The equals sign attached tothe statement is like an operator and its value is assigned from right to left. The examples belowshows that the value 100 is assigned to my variable which is called Lamar,

    Lamar = 100

    There are other ways to make combinations with mathematical operators such as,

    Lamar = 100 + 25

    The will take the original value within the statement and add 25 to it making the new value 125.

    The table below show a list of possible mathematical operator you can include in yourassignment statements.

    Operator Meaning Examples Order of precedence^ Exponentiation

    (raise to the powerof)

    2 ^ 3 = 8 1

    * Multiplication 3 * 2 = 6 2/ Integer Division 5 / 2 = 2 3Mod Remainder Part of

    division5 mod 2 = 1 4

    + Add 4 + 5 = 9 5- Subtract 3 2 = 1 6

  • 8/9/2019 Assignment 18.2 NEW

    5/16

    Input and Output Statements

    Input and Output Statements are usually completed via controls such as text boxes, list boxes

    and buttons which are placed on the form that will be displayed to the user. Below is anexmaple of available controls you can have on a form.

    Visual Basic is an event driven programming language, so subroutines are required to bewritten so that it can respond to user events such as clicking a button. The subroutinescollect the value the the user enters in the available text boxes, it then assigns the text property of the text box to a variable. For example:

    MyText = txtMyInput.text

    The statement assigns the value entered in the a text box called txtMyInput to a variablecalled MyText.

    Similarly you can output a value to a text box or label by simply writing the assignment statement in reverse. So the output text label called lblMylabel, the statement would bewritten as,

    lblMyLabel.text = Hello

    This is a media player which Idevoloped it includes all thenecessary controls with the

    use of coding to make itoperational. As you can see Ihave buttons, Labels,menustrips and trackbars . Allof these contols are essentialto make this program work .

    LabelButtons

    Menustri

  • 8/9/2019 Assignment 18.2 NEW

    6/16

    Card Game 21 Program 1 Analysis and Design

    This is the 6 th program I created and unlike all the other programs this one is rather enjoyable

    because it was made for 2 people and it is based on a popular card game called 21. The mainpurpose of this program is to provide fun and respond to the actions the user presents.

    There are a 4 buttons within the program which makes it what it is. As you can see there is a Player 1 and Player 2 Button which is what the user presses to start the game. The New Game buttonrestarts the game so it saves time from closing the whole program and opening it up again. The QuitGame button closes the program.

    Structure of the Program

    Clicking on the Player 1 Buttoninitiates the game by showingcards in the centre of the

    screen. You repeatedly keepon clicking on the player 1until you reach 21. If you get

    21 you win the first round, if you get anything other thanthat value you lose.

    If you win a RoundIf you lose a Round As both players continue toplay the game they willnotice that the progress bar

    will continue to increase.This indicates who iswinning and in the lead.

    From the picture above wecan clearly see that player 2is in the lead

    The New Game

    Button willrestart thegame to save

    trouble forclosing andreopening the

    program

    When a playerwins the game

    this messagebox will appear.

    In this case

    Player 1 Won

    the game

  • 8/9/2019 Assignment 18.2 NEW

    7/16

    The Coding Of the ProgramThis was the coding used to make the program functional, as you can see it was a long process to

    make this game.Public Class Form1

    Public P1Cards As Integer = 0

    Public P2Cards As Integer = 0

    Public P1Score As Integer = 0

    Public P2Score As Integer = 0

    Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load

    End Sub

    Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.ClickDim TheRandomClass As New Random

    Dim i As Integer = 0

    i = TheRandomClass.Next(1, 6)

    Label2.Text = i.ToString

    P1Cards += i

    Label1.Text = "Player 1 Cards: " + P1Cards.ToString

    If Label2.Text = 1 Then

    PictureBox1.Visible = True

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If Label2.Text = 2 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = True

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If Label2.Text = 3 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = True

    PictureBox4.Visible = False

    This section of thecode is programming

    when you click on thePlayer 1 button andthe cards switch eachtime

  • 8/9/2019 Assignment 18.2 NEW

    8/16

    PictureBox5.Visible = False

    End If

    If Label2.Text = 4 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = True

    PictureBox5.Visible = False

    End If

    If Label2.Text = 5 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = True

    End If

    If P1Cards > 21 Then

    MsgBox( "Bust!" , MsgBoxStyle.Critical, "End Of Turn" )

    P1Cards = 0

    Label2.Text = ""

    Label1.Text = "Player 1 Cards: 0"

    Button1.Enabled = False

    Button2.Enabled = True

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If P1Cards = 21 Then

    MsgBox( "21!" , MsgBoxStyle.Information, "End Of Turn" )

    P1Cards = 0

    Label2.Text = ""

    Label1.Text = "Player 1 Cards: 0"

    Dim o As Integer = 0

    o = 1

    P1Score += o

    Label4.Text = P1Score * 20

    ProgressBar1.Value = Label4.Text

    This parts programswhen you go over 21 andwhat message will begiven if it happened

  • 8/9/2019 Assignment 18.2 NEW

    9/16

    Button1.Enabled = False

    Button2.Enabled = True

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If ProgressBar1.Value = 100 Then

    MsgBox( "Player 1 Wins!" , MsgBoxStyle.Information, "End Of Game" )

    Button1.Enabled = False

    Button2.Enabled = False

    Label1.Text = "Player 1 Cards: 0"

    Label5.Text = "Player 2 Cards: 0"

    End IfEnd Sub

    Private Sub Button2_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click

    Dim TheRandomClass As New Random

    Dim i As Integer = 0

    i = TheRandomClass.Next(1, 6)

    Label2.Text = i.ToString

    P2Cards += i

    Label5.Text = "Player 2 Cards: " + P2Cards.ToString

    If Label2.Text = 1 Then

    PictureBox1.Visible = True

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If Label2.Text = 2 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = True

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If Label2.Text = 3 Then

    PictureBox1.Visible = False

    This section focuses on

    the progress bar andthe action that willoccur when it reaches

    100

    This section is the exactsame as the first exceptit is focusing on the

    player 2 button insteadof player 1.

  • 8/9/2019 Assignment 18.2 NEW

    10/16

    PictureBox2.Visible = False

    PictureBox3.Visible = True

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If Label2.Text = 4 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = True

    PictureBox5.Visible = False

    End If

    If Label2.Text = 5 Then

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = True

    End If

    If P2Cards > 21 Then

    MsgBox( "Bust!" , MsgBoxStyle.Critical, "End Of Turn" )

    P2Cards = 0

    Label2.Text = ""

    Label5.Text = "Player 2 Cards: 0"

    Button2.Enabled = False

    Button1.Enabled = True

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If P2Cards = 21 Then

    MsgBox( "21!" , MsgBoxStyle.Information, "End Of Turn" )

    P2Cards = 0

    Label2.Text = ""

    Label5.Text = "Player 2 Cards: 0"

    Dim o As Integer = 0

  • 8/9/2019 Assignment 18.2 NEW

    11/16

    o = 1

    P2Score += o

    Label7.Text = P2Score * 20

    ProgressBar2.Value = Label7.Text

    Button2.Enabled = False

    Button1.Enabled = True

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = False

    End If

    If ProgressBar2.Value = 100 Then

    MsgBox( "Player 2 Wins!" , MsgBoxStyle.Information, "End Of Game" )

    Button1.Enabled = False

    Button2.Enabled = False

    Label1.Text = "Player 1 Cards: 0"

    Label5.Text = "Player 2 Cards: 0"

    End IfEnd Sub

    Private Sub Button3_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button3.Click

    P1Cards = 0

    P2Cards = 0

    P1Score = 0

    P2Score = 0

    Label1.Text = "Player 1 Cards: 0"

    Label5.Text = "Player 2 Cards: 0"

    Label2.Text = ""

    Button1.Enabled = True

    Button2.Enabled = False

    ProgressBar1.Value = 0

    ProgressBar2.Value = 0

    PictureBox1.Visible = False

    PictureBox2.Visible = False

    PictureBox3.Visible = False

    PictureBox4.Visible = False

    PictureBox5.Visible = FalseEnd Sub

  • 8/9/2019 Assignment 18.2 NEW

    12/16

    Testing Of the ProgramTest Test Data Use Expected Output Actual Output Test ResultPlayer 1 Button Generate Cards Generated the

    CardsPASS

    Player 2 Button Generated Cards Generated theCards

    PASS

    New GameButton

    Restart the Game Restarted theGame

    PASS

    Quit GameButton

    Close The Game Closed The Game PASS

    Technique/Tool Justification Witness Statement Picture Box This tool helps to display the pictures

    before. The cards are all implementedby using this tool

    This tool helped incredibly well with program because it displayed all the necessary cards once inplay. Without this key tool my program would be inactive and faulty.

  • 8/9/2019 Assignment 18.2 NEW

    13/16

    Program Selection Method Analysis and Desi gn

    This was the 4 th program I created; it took very little time to create it as the amount of buttons and

    functions I had installed was the very minimum. The main purpose of this program is the response tothe clicking of a button and returns an event. Also the click of either of the options should make theprogress bar gradually increase or decrease. In my opinion this program isnt as important as thebrowser because it doesnt serve any real purpose towards the public. It is simply used for thetesting of different buttons and functions.

    This is what the program looks like; it is a simple but manageable program. The background of theprogram makes it look more presentable a sleek. Like I

    said before there are 6 buttons you can click from andeach one of them gives a different response, especiallythe Go forward and Go Back buttons. Button 1,2,3,4

    presents a message box telling the viewers what he justdid. In the next part of this report you will see what I

    mean.

    Clicking on the Go Forward option allows the progressbar to go forward slowlybut surely.

    Clicking on the Go Backoption allows the progress

    bar to go back slowly butsurely

  • 8/9/2019 Assignment 18.2 NEW

    14/16

    Structure of the Program

    Test TestDataused

    ExpectedOutput

    ActualOutput

    TestResult

    GoForwardCheck Box

    ProgressBar ShouldGoForward

    ProgressBar wentforward

    PASS

    Go Back

    Check Box

    Progress

    Bar ShouldReverse

    Progress

    BarReversed

    PASS

    Button 1 Shows aMessageBox

    Showed aMessageBox

    PASS

    Button 2 Shows aMessageBox

    Showed aMessageBox

    PASS

    Button 3 Shows aMessageBox

    Showed aMessageBox

    PASS

    Button 4 Shows aMessage

    Showed aMessage

    PASS

    Clicking on the Go

    Forward ButtonClicking on the

    Go Back Button

    Clicking on

    Button 1

    Clicking on

    Button 2

    Clicking on

    Button 3

    Clicking on

    Button 4

  • 8/9/2019 Assignment 18.2 NEW

    15/16

  • 8/9/2019 Assignment 18.2 NEW

    16/16

    Technique/Tool Justification Witness StatementCheckbox The reason for this tool is give

    the user selection choices. If he/she does select the GoForward he/she can select theGo back button as well.

    Progress Bar This is used to show theprogress of the event

    Buttons I have used the button toinitiate a click event.

    The properties

    show thecharacteristics anddetails of the

    program. Soeverything on theprogram looks the

    way it is becauseof its properties.

    Within this program I used the checkbox tool, button tool and a progress bar. Both these tools arevery helpful and actually made the program what it is. With these too tools the program would havelacked stability. The button tool was coded to initiate an event.