In: Computer Science
USE VISUAL BASIC / VB
You will create a datafile with the following information: 70 80 90 55 25 62 45 34 76 105You will then write the program to read in the values from the datafile and as you read in each number, you will then evaluate it through an if statement for the weather of the day. For example, when you read in the value 70 from the datafile, you should print to the screen that the temperature is 70° and it is nice outside. After reading in all the values, you will determine which is the highest temperature and which is the lowest temperature. For example, your output will look like this:
The temperature for today is 70. It is nice outside.
The temperature for today is 80. It is getting warm outside.
The temperature for today is 90. It is hot outside.
The temperature for today is 55. It is cool outside.
The temperature for today is 25. It is really cold outside.
The temperature for today is 62. It is comfortable outside.
The temperature for today is 45. It is chilly outside.
The temperature for today is 34. It is cold outside.
The temperature for today is 76. It is nice outside.
The temperature for today is 105. It is really hot outside.
The high temperature for today is 105.
The low temperature for today is 25
Hints: You will need to create an if statement after you read your values in from the database to check the temperatures.
The ranges should be:
Above 100 –really hot
90-100 –hot
80-90 –getting warm
70-80 –nice
60-70 –comfortable
50-60 –cool
40-50 –chilly
30-40 –cold
20-30 –really cold
Below 20 –freezing
Notice that the number values such as 90 shows up in both selections. Instead of 90, one of them has to be 89 and so on. You will determine where the cutoffs are. Do not hard code the output information. Make sure you are running it through an if statement because my datafile will have different values than yours.
Below is the solution:
code:
Imports System.IO
Module Module1
Sub Main()
'declare the
variabel
Dim fileReader As
String
Dim dummyArray() As
String
Dim tempArray As New
ArrayList
fileReader =
File.ReadAllText("temperature.txt")
' Part 2: split string
based on spaces.
dummyArray =
fileReader.Split(New Char() {" "c})
For Each value As String
In dummyArray
tempArray.Add(Convert.ToInt32(value)) 'convert each value to
integer and store in arraylist
Next
'loop through each
arraylist call the function to get the temperature details
For Each num In
tempArray
tempDisplay(num)
Next
'declare a variable
to find the highest temperature and lowest temperature
Dim max As Integer =
Integer.MinValue
Dim min As Integer =
Integer.MaxValue
For Each element As
Integer In dummyArray
max = Math.Max(max, element)
min = Math.Min(min, element)
Next
'display the high and
low tepperature
Console.WriteLine("The
high temperature for today is " & max)
Console.WriteLine("The
low temperature for today is " & min)
Console.ReadKey()
End Sub
'functon to display the teperature info
Private Sub tempDisplay(num As Integer)
'select case/switch case
to get the info of the temperature
Select Case num
Case > 100
Console.Write("The temperature for today is " & num & ". it
is really hot outside.")
Case 90 To 100
Console.Write("The temperature for today is " & num & ". it
is hot outside.")
Case 80 To 90
Console.Write("The temperature for today is " & num & ". it
is getting warm outside.")
Case 70 To 80
Console.Write("The temperature for today is " & num & ". it
is nice outside.")
Case 60 To 70
Console.Write("The temperature for today is " & num & ". it
is comfortable outside.")
Case 50 To 60
Console.Write("The temperature for today is " & num & ". it
is cool outside.")
Case 40 To 50
Console.Write("The temperature for today is " & num & ". it
is chilly outside.")
Case 30 To 40
Console.Write("The temperature for today is " & num & ". it
is cold outside.")
Case 20 To 30
Console.Write("The temperature for today is " & num & ". it
is really cold outside.")
Case < 20
Console.Write("The temperature for today is " & num & ". it
is freezing outside.")
End Select
Console.WriteLine()
End Sub
End Module
sample output: