Showing posts with label If. Show all posts
Showing posts with label If. Show all posts

Friday, February 11, 2011

Practice #2 Spacely, Heat & Clothing

1. Mr. Spacely has been rethinking the company's shipping schemes. As an employee for Spacely's Sprockets, Spacely has asked you to write a quick program that will be used to determine shipping costs.A total purchase of items totaling under $50, we charge $5 shipping.A total purchase of items totaling $50 and over, there is no shipping charge.
     Dim purchase, shipping, total As Double
  
     shipping = 5
  
     purchase = CDbl(txtPurchase.Text)
  
     If purchase < 50 Then
  
       total = purchase + shipping
  
     ElseIf purchase >= 50 Then
  
       total = purchase
  
     End If
  
     MessageBox.Show("The total of the item plus shipping is " & FormatCurrency(total))
  
   End Sub  
2. You are looking for something to control your heat in your apartment and you discover there is NOT an app for that. It's about time that someone created one. You decide that you are the one to do it. Here's what you want to do.
-You want to turn the heat on when the temp has dropped below 72
-You also want to turn the AC on when the temp gets above 76
Your app should display in a message box if the heat is on, the AC is on, or if the system is idle. Plug in different temps for the room in a variable to see what the thermostat will do with it.
 Dim temperature As Double
  
 temperature = CDbl(txtTemp.Text)
  
 If temperature <= 72 Then
  
 MessageBox.Show("The heat is on.", "Heat")
  
 ElseIf temperature > 72 And temperature < 76 Then
  
 MessageBox.Show("The system is idle", "Idle System")
  
 ElseIf temperature >= 76 Then
  
 MessageBox.Show("The A/C is on.", "A/C")
  
 End If
  
 End Sub
  
 End Class
  

3. You are working on a clothing website where people can buy kids socks. It's really hard for the customers to know what size they should buy for what age. It would be a good idea for the customer to have a tool to input their child's age and have the website suggest a size for them. Write a tool where you can set the age as a variable and have it suggest on of the sizes below:
a. 0-2 years - XS
b. 3-4 years - S
c. 5-8 years - M
d. 9-12 years - L
e. 13+ years - XL
     Dim age As Double
  
     age = CDbl(txtpurchase.Text)
  
     'Calculate total 
  
     If age <= 2 Then
  
       MessageBox.Show("The child's size is XS.", "XS")
  
     ElseIf age >= 3 And age <= 4 Then
  
       MessageBox.Show("The child's size is Small", "Small")
  
     ElseIf age >= 5 And age <= 8 Then
  
       MessageBox.Show("The child's size is Medium", "Medium")
  
     ElseIf age >= 9 And age <= 12 Then
  
       MessageBox.Show("The child's size is Large", "Large")
  
     ElseIf age >= 13 Then
  
       MessageBox.Show("The child's size is X-Large.", "X-Large")
  
     End If
  
   End Sub  

If Statements

A conditional expression is formed by relating values, either constants or variables . The comparison is evaluated as Boolean True or False, and the script reacts to that condition. The following script presents a simple example of script decision making, making one decision.
  If Args.CommandName = "Button 1" Then
  
   ButtonResponse.Text = "You clicked Button 1"
  
  End If
  
 End Sub
  
This shows an If statement of either or:
     Dim Message As String
  
     Message = "Skittles is an old form of bowling in which a wooden " & "disk is used to knowck down nine pins arranged in a square."
  
     If txtmessage.Text.ToUpper = "N" Then
  
       MessageBox.Show(Message, "Definition")
  
     End If
  
     txtQuote.Text = "Life ain't all beer and skittles." & " - Du Maurier (1894)"
  
   End Sub
  
 End Class  

This can make many decision depending on what it asks for, here it's honor students according to a certain GPA.
    Dim gpa As Double = CDbl(txtGpa.Text) 
  
    Dim honors As String 
  
    If gpa >= 3.9 Then 
  
     honors = " summa cum laude." 
  
    ElseIf gpa >= 3.6 Then 
  
     honors = " magna cum laude." 
  
    ElseIf gpa >= 3.3 Then 
  
     honors = " cum laude." 
  
    ElseIf gpa >= 2 Then 
  
     honors = "." 
  
    End If 
  
    txtResults.Text = "You graduated" & honors 
  
   End Sub