Friday, October 9, 2015

Visual Basic Programming Chapter 3 Variables and Calculations

3.1 Gathering Text Input

41010

Assume you have a text box named textBox. Write some code that inputs the value in the text box into a variable  name and then outputs "Greetings, NAME" to a message box (where NAME is replaced by the value that was input read into the variable .
For example, if the text box originally contained "Rachel" your code would display "Greetings, Rachel" in the message box.


name = textBox.Text
MessageBox.Show("Greetings, " & name)


3.2 Variables and Data Types
 declaration

60105
 Before a variable is used it must be_____
declared
60106
 Which of the following is NOT a legal identifier?
 
outrageouslyAndShockinglyLongRunon
 _42
_
lovePotionNumber9
7thheaven
 
7thheaven
 
60110
 Which is the best identifier for a variable to represent the amount of money your boss pays you each month?
 
notEnough
wages
paymentAmount
monthlyPay
money
 
monthlyPay
 
85016
 
QUESTION 1:  Correct

Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not?
 
temperature
feverTest
hasFever
fever

hasFever

QUESTION 2:  Correct

Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not?
 
divisible
isPrime
mightBePrime
number assignment 
 
mightBePrime 
 
assignment:

40947
Write a statement to set the value of num to 4 (num is a variable  that has already been declared).
num = 4
 
 
40338
 Given an integer variable  drivingAge that has already been declared, write a statement that assigns the value 17 to drivingAge
drivingAge = 17
 
40339
 Given two integer variables  oldRecord and newRecord, write a statement that gives newRecord the same value that oldRecord has.
newRecord = oldRecord
 
40948
 Given two integer variables  num and highest, write a statement that gives highest the same value that num has.
highest = num
 
40349
 Given three already declared int variables , i, j, and temp, write some code that swaps the values i i and j. Use temp to hold the value of i and then assign j's value to i. The original value of i, which was saved in temp, can now be assigned to j
 
temp = i
     i = j
     j = temp
 
40350
 
 Given two int variables , firstPlaceWinner and secondPlaceWinner, write some code that swaps their values.

Declare any additional variables  as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner
 
 
dim swap as Integer
swap = firstPlaceWinner
     firstPlaceWinner = secondPlaceWinner
secondPlaceWinner = swap
 
 
40351

Given two double variables , bestValue and secondBestValue, write some code that swaps their values. Declare any additional variables  as necessary.

Dim swap As Double
swap = bestValue
bestValue = secondBestValue
secondBestValue = swap
 
 
types and literals:
 60113
Write a long integer literal that has the value thirty-seven  
 37L
 
40038
 
Write a floating point literal corresponding to the value zero.
 0.0D

40039 
 
Write a literal corresponding to the floating point value one-and-a-half
 1.5D

 
40040
 
Write a literal corresponding to the value of the first 6 digits of PI ("three point one four one five nine"). 

3.14159D
 
 
input of variables:
40980
Write an expression that attempts to read a double value from the TextBox textBox and stores it in a Double variable , x, that has already been declared.
 
x = CDbl(textBox.Text)
 
 
output of variables: 


41098

Assume that message is a String variable . Write a statement to display its value in a MessageBoxt.
 MessageBox.Show(message)
 
 
41099 
 Assume that word is a String variable . Write a statement to display the message "Today's Word-Of-The-Day is: " followed by the value of word in a MessageBox. 
 
 MessageBox.Show("Today's Word-Of-The-Day is: " + word)
 
 input and output:


41011 
Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa") Write some code that reads a value from a text box named textBox into name then prints the message "Greetings, NAME!!!" where NAME is replaced the value that was read into name. For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!".
 
name = textBox.Text

MessageBox.Show("Greetings, " & name & "!!!")
 
 
 3.3 Performing Calculations
 
operations:
40326
Write an expression that computes the sum of the two variables  verbalScore and mathScore (already declared and assigned values). 

verbalScore + mathScore
 
 
40327
Given the variables  taxablePurchases and taxFreePurchases (already declared and assigned values), write an expression corresponding to the total amount purchased. 

taxablePurchases + taxFreePurchases
 
 
40328
Write an expression that computes the difference of the variables  endingTime and startingTime.
 
 endingTime - startingTime
 
40329
 
Given the variables  fullAdmissionPrice and discountAmount (already declared and assigned values), write an expression corresponding to the price of a discount admission. (The variable  discountAmount holds the actual amount discounted, not a percentage.) 

fullAdmissionPrice - discountAmount
 
 
40330
 
Given the variable  pricePerCase, write an expression corresponding to the price of a dozen cases. 

 pricePerCase * 12

40331

Given the variables  costOfBusRental and maxBusRiders, write an expression corresponding to the cost per rider (assuming the bus is full). 

costOfBusRental / maxBusRiders
 
40048
 
You are given two variables , already declared and assigned values, one of type double, named price, containing the price of an order, and the other of type int, named totalNumber, containing the number of orders. Write an expression that calculates the total price for all orders. 

price * totalNumber
 
 
40044
 
Write an expression that computes the sum of two double variables  total1 and total2, which have been already declared and assigned values. 

total1 + total2
 
40045
  Write an expression that computes the difference of two double variables  salesSummer and salesSpring, which have been already declared and assigned values
salesSummer - salesSpring
 
40046 
 
You are given two double variables , already declared and assigned values, named totalWeight, containing the weight of a shipment, and weightOfBox, containing the weight of the box in which a product is shipped. Write an expression that calculates the net weight of the product. 

totalWeight - weightOfBox
 
40047
 
You are given two variables , already declared and assigned values, one of type double, named totalWeight, containing the weight of a shipment, and the other of type int, named quantity, containing the number of items in the shipment. Write an expression that calculates the weight of one item

totalWeight / quantity
 
40905
 
Assume that an int variable  x that has already been declared,.
Write an expression whose value is 1 more than x

x + 1

 
40332
Write an expression that computes the remainder of the variable  principal when divided by the variable  divisor. (Assume both are type int.) 

principal Mod divisor
 
 
40346
Write an expression that evaluates to true if the value of the integer variable  widthOfBox is not evenly divisible by the integer variable  widthOfBook. (Assume that widthOfBook is not zero. Note: evenly divisible means divisible without a non-zero remainder.) 

widthOfBox Mod widthOfBook
 
 
41023
 Assume that price is an integer variable  whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents". So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents". 
 
MessageBox.Show((price \ 100) & " dollars and " & (price Mod 100) & " cents")
 
assignments and operations:
 
 
40340
 
Given two integer variables  matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge

gradAge = matricAge + 4
 
40953
Write a statement to set the value of area to the value of length times the value of width. (The variables  have already been declared and length and width have already been initialized.)
 
area = length * width
 
40949
  Write a statement to set the value of ans equal to the value of num plus 5. (These variables  have already been declared and num has already been initialized.) 
 
ans = num + 5
 
 
40950
Write a statement to set the value of price equal to three times the value of cost. (The variables  have already been declared and cost has already been initialized.)
 
price = 3 * cost
 
40951
Write a statement to add the values of x and y together, storing the result in sum. (The variables  have already been declared and x and y have already been initialized.)
 
sum = x + y
 
40952
Write a statement to subtract tax from gross_pay and assign the result to net_pay . (The variables  have already been declared and gross_pay and tax have already been initialized.)
net_pay = gross_pay - tax
 
40957
Write a statement to find the remainder remdr when num is divided by 5. (The variables  have already been declared and num has already been initialized.)
 
remdr = num mod 5
 
40956
Write a statement to assign to kilos the value of pounds divided by 2.2. (The variables  have already been declared and pounds has already been initialized.)
 
kilos = pounds / 2.2
 
40954
  Write a statement to multiply diameter by 3.14159 and assign the result to circumference. (The variables  have already been declared and diameter has already been initialized.)
 
circumference = diameter * 3.14159
 
 
40955
Write a statement to assign to weight_in_pounds the value of weight_in_kilos times 2.2. (The variables  have already been declared and weight_in_kilos has already been initialized.)
 
weight_in_pounds = weight_in_kilos * 2.2
 
40348
 
 Given two int variables , i and j, which have been declared and initialized, and two other int variables , itemp and jtemp, which have been declared, write some code that swaps the values i i and j by copying their values to itemp and jtemp, respectively, and then copying itemp and jtemp to j and i, respectively. 
 
itemp = i
jtemp =j
j = itemp
i = jtemp
 
 
 
 
 
 
precedence:
 
 
40333
Write an expression that computes the average of the values 12 and 40

(12 + 40) / 2
 
 
40334
 
Write an expression that computes the average of the variables  exam1 and exam2 (both declared and assigned values). 

(exam1 + exam2) / 2
 
41100 
 
Assume that an int variable  x has already been declared,.
Write an expression whose value is the last (rightmost) digit of x.
 
 
x mod 10
 
41001
 
Assume that price is an integer variable  whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid. 


price \ 100

41013
 
The dimensions (width and length) of room1 have been read into two variables : width1 and length1. The dimensions of room2 have been read into two other variables : width2 and length2. Write a single expression whose value is the total area of the two rooms. 

(width1 * length1) + (width2 * length2)
41014
 
A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable  small. Similarly, the length of the larger one is stored in the variable  large. Write a single expression whose value is the length of this wall.

small + large

41015 
 
Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one. The length of all the smaller ones is the same and is stored in the variable  small. Similarly, the length of all the larger ones is the same and is stored in the variable  large. Write a single expression whose value is the total area of this room. DO NOT use the pow function. 

(small + large) ^2

41016
 
Three classes of school children are selling tickets to the school play. The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables :tickets1, tickets2, tickets3 and class1, class2, class3. Write an expression for the average number of tickets sold per school child. 

(tickets1 + tickets2 + tickets3) / (class1 + class2 + class3)

41017
 
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression whose value is the 8th harmonic number. 

1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8

41018
 
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable  whose value is some positive integer N. Assume also that hn is a double variable  whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number. 

hn + 1 / (N+1)


41019
 
Assume that children is an integer variable  containing the number of children in a community and that families is an integer variable  containing the number of families in the same community. Write an expression whose value is the average number of children per family.
41020
 
Assume that children is an integer variable  containing the number of children in a community and that families is an integer variable  containing the number of families in the same community. Write an expression whose value is the average number of children per family.
41021
 
Assume that price is an integer variable  whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.
41022
 
 
Assume that price is an integer variable  whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.
 
 
 
combined assignment:
40189
 
Write a statement that increments the value of the int variable  total by the value of the int variable  amount. That is, add the value of amount to total and assign the result to total.
 
40341
 
Given an integer variable  bridgePlayers, write an statement that increases the value of that variable  by 4.
 
 
40342
 
Given an integer variable  profits, write a statement that increases the value of that variable  by a factor of 10. 

40960 
 
Write a statement using a compound assignment operator to add 5 to val (an integer variable  that has already been declared and initialized).
40961
 
  Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable  that has already been declared and initialized).
40962
 
  Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable  that has already been declared and initialized).
40963
  Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized).
 
 
 
mixing different data types:
 
formatting numbers and dates:
 
 
40010 
 
Assume that m is an integer variable  that has been given a value. Write a statement that assign to the string variable  s the formatted string corresponding to the value of m with two digits to the right of the decmial point and thousands separators.
 
40015
Use the ToString method with a format string to convert the double variable , pi to the string variable , s, in fixed point format and 4 digits of precision.
 
40016
Use the ToString method with a format string to convert the double variable , pi to the string variable , s, in scientific point format and 3 digits of precision.
 
40014
 
Assume that pricePerDozen is an variable  that has been given a value. Write a sequence of statements that assigns to the string perEgg a string formatted as Currency and containing the price per egg.
40017
 
Assume that numQuestions and numCorrect are variables  that has been given values. Write a sequence of statements that assigns to the string percentIncorrect a string formatted as a percentage indicating the percentage of INCORRECT questions correct to two decimal places.
40013
 Assume that m is an integer variable  that has been given a value. Write a statement that assign to the string variable  s the formatted string corresponding to the value of m with two digits to the right of the decmial point and thousands separators.
 
 
 
3.6 Class-Level Variables
 
3.7 Exception Handling
 
40011
 
Assume that s is a string variable  that is supposed to contain a value to be converted to integer. Write a fragment of code that converts the value to integer variable  and displays that value multipled by 2. If the value cannot be converted, display the message 'Invalid number' instead.
 
 
Try
MessageBox.Show(CInt(s) * 2)
Catch
MessageBox.Show("Invalid number")
End Try
 
 
 
 
 

2 comments:

  1. "Assume that pricePerDozen is an variable that has been given a value. Write a sequence of statements that assigns to the string perEgg a string formatted as Currency and containing the price per egg."

    Dim dblPricePerEgg AS Double = pricePerDozen/12
    perEgg = dblPricePerEgg.ToString("c"

    ReplyDelete
  2. "Assume that numQuestions and numCorrect are variables that has been given values. Write a sequence of statements that assigns to the string percentIncorrect a string formatted as a percentage indicating the percentage of INCORRECT questions correct to two decimal places."

    Dim numIncorrect AS Integer = numQuestions - numCorrect
    Dim percentCalc AS Double = numIncorrect/numQuestions
    percentIncorrect = percentCalc.ToString("p2")

    ReplyDelete