Вы находитесь на странице: 1из 1

Homework 4

Option Strict On
Public Class frmAcctBalance
'declare module-level variables
Dim mdecAllCharges As Decimal
Dim mdecAllCredits As Decimal
Dim mintCustomersoverlimit As Integer
Private Sub btnCalculate_Click(sender As
System.Object, e As System.EventArgs) Handles
btnCalculate.Click
'Declare local variables
'inputs
Dim intAccountNumber As Integer
Dim decBeginningBalance As Decimal
Dim decTotalCharges As Decimal
Dim decTotalCredits As Decimal
Dim decCreditLimit As Decimal
'outputs
Dim decEndingBalance As Decimal
Dim decAvailableCredit As Decimal
'Get inputs & validate from form
Try
intAccountNumber =
Convert.ToInt32(txtAccountNumber.Text)
decBeginningBalance =
Convert.ToDecimal(txtBeginningBalance.Text)
decTotalCharges =
Convert.ToDecimal(txtTotalCharges.Text)
decTotalCredits =
Convert.ToDecimal(txtTotalCredits.Text)
decCreditLimit =
Convert.ToDecimal(txtCreditLimit.Text)
Catch ex As Exception
MessageBox.Show("Incorrect input")
Exit Sub
End Try
'Do calculations
decEndingBalance = decBeginningBalance +
decTotalCharges - decTotalCredits
decAvailableCredit = decCreditLimit -
decEndingBalance
'if available credit is less than zero,
' put "over credit limit" on form and
add to customers over limit

If decAvailableCredit < 0 Then
lblMessage.Text = "EXCEEDED CREDIT
LIMIT"
mintCustomersoverlimit += 1
Else
lblMessage.Text = "Available Credit: "
& decAvailableCredit.ToString("C")

End If
'running totals
mdecAllCharges += decTotalCharges
mdecAllCredits += decTotalCredits

'Show results on form
lblAllCharges.Text =
mdecAllCharges.ToString("C")
lblAllCredits.Text =
mdecAllCredits.ToString("C")
lblCustomersOverLimit.Text =
mintCustomersoverlimit.ToString
lblEndingBalance.Text =
decEndingBalance.ToString("C")

End Sub

Private Sub btnClear_Click(sender As
System.Object, e As System.EventArgs) Handles
btnClear.Click
'clear the form
txtAccountNumber.Text = ""
txtCreditLimit.Text = ""
txtBeginningBalance.Text = ""
txtTotalCharges.Text = ""
txtTotalCredits.Text = ""
lblEndingBalance.Text = ""
lblAllCharges.Text = ""
lblAllCredits.Text = ""
lblCustomersOverLimit.Text = ""
lblMessage.Text = ""
'clear module level variables
mdecAllCharges = 0
mdecAllCredits = 0
mintCustomersoverlimit = 0
End Sub

Private Sub btnExit_Click(sender As
System.Object, e As System.EventArgs) Handles
btnExit.Click
'close
End
End Sub

Private Sub Label1_Click(sender As
System.Object, e As System.EventArgs) Handles
Label1.Click
End Sub
End Class

Homework 5
Option Strict On
Public Class FrmGrade
'declare module level variables
Dim mintStudentswithFs As Integer
Dim mintstudentswithAs As Integer
'declare constants for grade weights
Const EXAM As Decimal = 0.17D
Const INDIVIDUAL_HW As Decimal = 0.24D
Const TEAM_HW As Decimal = 0.15D
Const FINAL_PROJECT As Decimal = 0.1D

Private Sub btnCalculate_Click(sender As
System.Object, e As System.EventArgs) Handles
btnCalculate.Click
'declare variables
'inputs
Dim decindividualHW As Decimal
Dim decTeamHW As Decimal
Dim decExam1 As Decimal
Dim decExam2 As Decimal
Dim decexam3 As Decimal
Dim decFinalProject As Decimal
'outputs
Dim decfinalnumeric As Decimal
Dim strfinalletter As String
'Validate(Data)
If IsNumeric(txtIndividualHw.Text) = False
accumulator: a variable that increments by varying amount;
used to keep a running total; typically an accumulator increases by
the value of another variable
algorithm: plan of action for solving a problem; usually an outline
of steps or formula for finding a solution/ch.2: in programming;
the structure used to plan the solution to a programming problem
arguments: values used to complete a method or procedure (aka
parameters)
assignment: a statement used to place a value into a variable*
Boolean: data type used to store True or False values
bugs: the mistakes and errors in a program
class: definition for an object
code window: area where developer writes/edits code for a
program
compile: process of converting a developers forms and code into
a standalone program
concatenation: to join; used to combine two strings into one.
condition: a statement that evaluates to either True or False
constant: storage location that is assigned to a value when it is
created, value cannot be changed while program is running
controls: objects used to create a user interface; including
textboxes, labels, buttons, and more
controlchars: a method containing special characters used for
output to a RichTextBox
counter: variable that increments (or decrements) by a set
amount; typically, they add one to a variable, but any amount can
be used
date: a data type used to store dates and times
data: facts/figures used in decision making; raw information
data type: set of specific methods for storing data in a variable;
each data type is designed to hold data of a particular structure:
e.g. strings hold text, shorts hold sm. #s, singles hold #s with
decimals
data type conversion: conversion of data from one type to
another; i.e. conversion of a String of text into a number
data validation: testing to ensure that user input falls within an
acceptable range or meets a specified set of conditions so it can be
processed without errors
debugging: process of removing errors from a program
declaration: process of creating a variable and determining the
type of data it will store; variables are declared using a declaration
declarations: variables created at the start of the procedure;
used to set aside memory for storing data
design time: part of programming project where the program is
designed and coded
desk check: manual process of checking each line of code and
updating the values of the variables to determine if the program
fucntions as it should; manually checking/finding for errors in a
program
developer: person who writes/develops a computer program
event: action in a program; events trigger code that makes
changes to a program
events: user-initiated activities; example: a click, a program can
also initiate an event
event handler: method that responds to actions by the user
inputbox: control used to gather input from the user
flowchart: graphical depiction of a program; shows input,
processing, decisions, loops and output for a program
form: a window, user sees a form when running a program
form window: the devleopement window that contains the forms
for a programming project
initialize: the process of placing the first value into a variable or
constant; numeric variables it automatically places a 0 in the
variable; string variables are automatically initialized to null; other
data types are initialized to other values
input: data that goes into a program so the computer can process
them and produce outputs (results)/data added by user to the
program
interface: connections between the user and the computer; what
the user sees on the screen and works with while a program is
running
keywords: aka reserved words; in VB that have special function
and cant be used as variables/control name
length: method used to determine the number of characters in a
string; spaces are counted also
load event:
local variable: a variable that is declared in a procedure; it is only
visible in that procedure and can only be used there
logical errors: errors in the developers thinking that make a
program work differently than expected
logical operators: And, Or, Not and XOr. Used to create
compound statements to handle complex logical operations
message box: control used to display information to a user or to
gather input from the user
method: a procedure contatined in a class; it performs an
operation on data stored in the class
module level variable: variable that is declared at the top of a
module; it is visible in all the procedures for that module
nested Ifs: If structures that are completely contained inside
other structures
nested statement: a statement that is part of a larger block of
code
null: empty set; string variables are initialized to null
objects: sometimes aka controls; building blocks for a program
output: information produced by a program, usually displayed on
screen/information sent by the computer to the user
procedure: block of code that completes a specified task
processing: calculations of a program/work done by a program
pseudocode: cross between program code and a description;
English-like statements that describe what a program should do
relational operator: a comparison sign: =, >, <, >=, <=, or
<>. These symbols are used to compare the values on either side
repetition: repeating one or more steps in a program
RichTextBox: a control used to display multiple lines of text
runtime: time when a program is running, either as a finished
project or while its being tested
scope: visibility of a variable in a program; where a variable can
be used in a program
selection: decision structure in a program where the program
follows one set of directions instead of another
sequence: series of steps; executed in order
show: a method used to display a control
stacked Ifs: If statements that are stacked one on top of another
syntax: required structure of a line of code
ToLower: a method used to convert letters to lower case
toolbox: developer window that contains controls for creating a
program
tooltip: a popup message provided for controls; a property for
controls
toupper: a method used to convert letters into upper case
user: individuals who use a completed program
user interface: what user sees when working with a program,
what a developer creates when designing a program
variable: a container in memory for storing data
vbNewLIne: method used to end a line of an output in a
RichTextBox and begin a new line
The order of steps in a program is usually declaration, input, processing and
output.
Programming tasks can be divided into three broad categories: (1) sequence,
(2) selection and (3) repetition
The sequence (order) of commands is essential to the (correct) completion of
a programming task.
Computers follow a set of directions called a program, and the description of a
program is an algorithm.
Computer programs are miniscule, incremental and precise. You MUST pay
close attention to detail to be a good programmer!
Computers have no intellect and no insight. They blindly follow directions.
Events can be user interaction with the computer. There are also many other
events. Events trigger the execution of code in a program.
A Visual Basic .NET project is a folder that contains many files and other
folders. Be cautious about making changes to these files. The finished
program is in an .exe file in the project folder.
All computer operations are performed in binary, 0s and 1s.
A variable is a storage container for your data.
There are numerous data types, each with a specific use.
All variables must be declared before they can be used. Declarations are used
to name and type a variable.
Variables and constants must all conform to VB naming conventions. Only
letters, numbers and underscores (_) can be used; the name must start with a
letter; it cannot have spaces or use special characters; and it cannot be a
keyword.
Programmers usually use additional naming conventions to make it easier to
identify variables by using meaningful names, starting with a lower-case
letter, capitalizing the first letter of each word, and using a prefix that
describes the data type.
An assignment statement is used to change the value stored in a variable.
The variable is ALWAYS on the left and the value being placed in the variable
is ALWAYs on the right. An easy way to remember this is to remember that
you have to tell the computer where to store the answer before you tell the
computer how to find the answer.
All numeric variables are automatically initialized to zero. All string variables
are initialized to the empty string.
Quotation marks around a string are essential. They tell the computer where
the string starts and ends.
Computers use the same order of operations as mathematicians
Parentheses, Exponents, Multiplication & Division, Addition & Subtraction
Declarations are used to declare variables and constants. Input is used to get
data from the user. Processing manipulates the data to find results. Output
returns the results to the user.
There are many potential problems with any program. The best way to
understand a program is to run it and test it.
Controls and variables each have specific uses in a program and both must
work together to solve a problem.
Comments are used to explain and describe code. The computer ignores
comments, so they do not affect system performance. When in doubt, use
too many comments rather than too few.
A program header (called a prologue in the book) is a set of comments at the
beginning of a program that describes the program.
You can set a shortcut key for a Button by adding the ampersand (&) to the
Text property of the Button. Place it in front of the character that you want as
the shortcut. For example, the Text property of the Exit button should be
E&xit so that the text on the actual button shows up as Exit. Remember you
have to press the ALT key to get the underlines to show up and work.
Output can be formatted to make it easier to read and understand. To format
output to currency, use (c) at the end of the .ToString method. e (scientific
notation), n (number) and p (percent) can also be used for formatting.
The number of decimal places can be specified when formatting. Add a
number to the argument to specify the number of decimals to display, for
example, .ToString (n3) will show a number with 3 decimal places.
What is the difference between a syntax, runtime and logic error? When
might each of these error types occur and what might cause them?
Developers are expected to create a program that is easy and enjoyable for
the user.
Type conversion converts one data type to another type. Data type
conversion is typically used to convert Strings into a numeric format.
Use Option Explicit to force variables to be declared. Use Option Strict to
force Visual Basic to be a strongly-typed language so that the compiler will not
allow implicit conversions of data types.
Values entered in textboxes are ALWAYS strings, so they must be converted
to numeric types for calculations. Numeric types MUST be converted to
strings to display on a form.
Testing is an essential part of programming.
Variable scope determines where a variable can be used.
The scope of a variable is controlled by where it is declared and how it is
declared.
Local variables are declared, used and discarded, all in the same procedure.
Module-level variables store values as long as the form is being used. Theyre
created when the form is created and persist until the form is closed. They
can be used in any procedure on the form.
Counters are variables that keep count. Usually they count up by 1, but they
can be used to count by any fixed number, either up (positive) or down
(negative).
Accumulators add a variable to a running total.
Variables dimmed in a click event (local variables) dont work as well for
counters and accumulators. Their values dont persist between clicks.
The computer stores dates as numbers. The date is the integer part of the
number and the time is the decimal part of the number.
The system clock on your computer has the current date and time.
Use the crosshatch/pound sign (#) to enclose a date and slashes to separate
the units.
The list of methods is similar to the built-in functions in Excel.
NumericUpDowns (NUDs) prevent the user from entering non-numeric data.
They can also be used to pre-define minimums, maximums and increments.
String manipulation = math for text. Concatenation (putting strings together)
is the most common form of string manipulation.
Decision structures allow the computer to execute alternate blocks of code
based on the outcome of a decision.
The key to writing decision structures is to understand how data flow through
a program.
A general rule in programming is to never write the same code twice if you
can avoid it.
An If structure is based on a condition. When the condition is True, the
computer runs the Then part of the code. When the condition is False, the
computer runs the Else part of the code.
All conditions will be either True or False and only one block of code will run
on any given pass.
Strings and numbers cannot be compared to each other in a decision
structure.
Strings must match exactly in a comparison. If you would like the comparison
to be case-insensitive, use the .ToUpper or .ToLower methods discussed in
Chapter 4.
Logical operators give the following results:
Condition A Condition B AND Result OR Result XOR Result
Both True True True True True False
A True/B False True False False True True
A False/B True False True False True True
Both False False False False False False
Data validation checks the users input to make sure its within an established
range before processing. This prevents processing problems and runtime
errors.
Case structure is another way of making decisions in programming logic.

Then
MessageBox.Show("Incorrect Input")
Exit Sub
End If
If IsNumeric(txtTeamHw.Text) = False Then
MessageBox.Show("Incorrect Input")
Exit Sub
End If
If IsNumeric(txtExam1.Text) = False Then
MessageBox.Show("Incorrect Input")
Exit Sub
End If
If IsNumeric(txtExam2.Text) = False Then
MessageBox.Show("Incorrect Input")
Exit Sub
End If

If IsNumeric(txtExam3.Text) = False Then
MessageBox.Show("Incorrect Input")
Exit Sub
End If
If IsNumeric(txtFinalProject.Text) = False
Then
MessageBox.Show("Incorrect Input")
Exit Sub
End If
'convert input
decindividualHW =
Convert.ToDecimal(txtIndividualHw.Text)
decTeamHW =
Convert.ToDecimal(txtTeamHw.Text)
decExam1 =
Convert.ToDecimal(txtExam1.Text)
decExam2 =
Convert.ToDecimal(txtExam2.Text)
decexam3 =
Convert.ToDecimal(txtExam3.Text)
decFinalProject =
Convert.ToDecimal(txtFinalProject.Text)
'check range 0-100
If decindividualHW < 0 Or decindividualHW
> 100 Then
MessageBox.Show("Range invalid")
End If
If decTeamHW < 0 Or decTeamHW > 100 Then
MessageBox.Show("Range invalid")
End If
If decExam1 < 0 Or decExam1 > 100 Then
MessageBox.Show("Range invalid")
End If
If decExam2 < 0 Or decExam2 > 100 Then
MessageBox.Show("Range invalid")
End If
If decexam3 < 0 Or decexam3 > 100 Then
MessageBox.Show("Range invalid")
End If
If decFinalProject < 0 Or decFinalProject
> 100 Then
MessageBox.Show("Range invalid")
End If
'calculations
decfinalnumeric = (INDIVIDUAL_HW *
decindividualHW) + (EXAM * decExam1) + (EXAM *
decExam2) + (EXAM * decexam3) + (FINAL_PROJECT *
decFinalProject) + (TEAM_HW * decTeamHW)
'get letter grade
Select Case decfinalnumeric
Case 89.5D To 100
strfinalletter = "A"
mintstudentswithAs += 1
Case 79.5D To 89.49D
strfinalletter = "B"
Case 69.5D To 79.49D
strfinalletter = "C"
Case 59.5D To 69.49D
strfinalletter = "D"
Case 0 To 59.49D
strfinalletter = "F"
mintStudentswithFs += 1
End Select
'show results on form
lblFinalLetterGrade.Text = strfinalletter
lblStudentswithAs.Text =
mintstudentswithAs.ToString
lblStudentswithFs.Text =
mintStudentswithFs.ToString
lblFinalNumeric.Text =
decfinalnumeric.ToString("N2")
End Sub
Private Sub btnClear_Click(sender As
System.Object, e As System.EventArgs) Handles
btnClear.Click
txtExam1.Text = ""
txtExam2.Text = ""
txtExam3.Text = ""
txtTeamHw.Text = ""
txtFinalProject.Text = ""
txtIndividualHw.Text = ""
lblFinalNumeric.Text = ""
lblFinalLetterGrade.Text = ""
End Sub
Private Sub btnExit_Click(sender As
System.Object, e As System.EventArgs) Handles
btnExit.Click
'Exit
End
End Sub
Private Sub btnReset_Click(sender As
System.Object, e As System.EventArgs) Handles
btnReset.Click
mintstudentswithAs = 0
mintStudentswithFs = 0
lblStudentswithAs.Text = ""
lblStudentswithFs.Text = ""
End Sub
Private Sub FrmGrade_Load(sender As
System.Object, e As System.EventArgs) Handles
MyBase.Load

End Sub
End Class


\

Вам также может понравиться