Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Visual Basic Program with simple numeric output. The algorithm that all correct programs must implement is: Test that valid input has been received. Reverse the

Visual Basic Program with simple numeric output.

image text in transcribed

image text in transcribed

image text in transcribed

The algorithm that all correct programs must implement is:

  1. Test that valid input has been received.

  2. Reverse the digits in the value received. For example, if 654 is input, giving the value 654, then the reversed number is 456.

  3. Subtract the reversed number from the original value. This difference will be called Diff in this algorithm. So, continuing the example started above: Diff = 654 - 456 = 198.

  4. Output the sum of Diff and the reverse of the digits in Diff. Thus, output 198 + 981 = 1089.

Note that, given valid input, the output will always be 1089.

Tasks

Look at the code for the Calculate buttons click-event handler in the given start solution. It implements the algorithm:

  1. If the input is numeric:

    1. If the input contains 3 characters:

      1. If the input contains an integer:

        1. If the input has strictly descending digits:

          1. Convert the input to an integer and implement the algorithm given above.

        2. Else output the error message: ERROR - Enter descending digits only.

      2. Else output the error message: ERROR - Enter integers only.

    2. Else output the error message: ERROR - Enter three-digit numbers only.

  2. Else output the error message: ERROR - Enter numeric input only.

The validity testing for:

  • Is the input an integer?

  • Does the input have descending digits?

and

  • Reverse the digits in the number.

all need to be implemented in functions. These functions have been started for you. Please look them over in the given start project.

The Is the Input an Integer? Function

Several things to note here:

  1. The code has already tested for numeric input on line 3.

  2. The code has already tested that the input string has length 3 on line 4.

So, the input string will be a 3-character long, numeric value. Note that invalid values, like 2.2, is a 3-character long, numeric value. Testing for valid integer characters in the input string is where this function comes in.

There are many, many ways of implementing this function. But, all correct code must work with the characters in the input string only. Any code that converts the input string to a numeric value inside the function will be considered incorrect and will have points deducted (even if the code generates correct results).

The Does the Input have Descending Digits? Function

Again, note that:

  1. Testing for numeric input occurred on line 3.

  2. Testing that the input string has length 3 occurred on line 4.

  3. Testing that the input is an integer occurred on line 5.

So, now we know that the input string is a 3-character long, integer value. Note that invalid values, like 123, is a 3-character long, integer value. Testing the input string for strictly descending digits must be done.

Like the last function, your code must work with the characters in the input string only. Any code that converts the input string to a numeric value inside the function will be considered incorrect and will have points deducted. Rather, since we already know that the string contains a 3-digit integer, just test that each character is greater than all of the characters that follow.

Two ways to get individual characters from a string:

  1. Use the String.Substring() method (see page 266 in the text for details).

  2. Use parenthesis to access individual characters by their index. For example,

    Dim Val As String = "abc" TextBox1.Text = Val(0) ' Puts "a" in TextBox1 TextBox2.Text = Val(1) ' Puts "b" in TextBox2 TextBox3.Text = Val(2) ' Puts "c" in TextBox3

The Reverse the Digits Function

After the input string has been validated and converted to an integer (use of CInt() is adequatenote that it will not throw an exception), this function must reverse the digits in the integer value passed in.

This routine must process the digits of a numeric input value: Any code that converts the input to and/or from a string inside this routine will be considered incorrect. Rather, use the numeric operators to reverse the digits.

Numerical Trick? Enter a 3-digit integer with strictly decreasing digits: Calculate Result Public Class MainForm Private Sub CalculateButton_Click (sender As Object, e As EventArgs) Handles CalculateButton.Click If IsNumeric (InputTextBox. Text) Then 4 If InputTextBox.Text.Length - 3 Then If IsInteger (InputTextBox.Text) Then If HasDescendingDigits(InputTextBox.Text) Then We know that Number will be a 3-digit integer with descending digits: Dim Number As Integer = CInt (inputText Box. Text) 8 10 Implement the rest of the algorithm here. 12 13 14 15 16 17 18 Else MessageBox.Show("ERROR Enter descending digits only.") End If Else MessageBox.Show("ERROR Enter integers only.") End If Else MessageBox.Show("ERROR- Enter three-digit numbers only.") End If 21 Else MessageBox.Show("ERROR- Enter numeric input only.") 23 24 25 26 27 End If End Sub Function ReverseDigits (ByVal Value As Integer) As Integer Dim ReturnNumber As Integer -0 'Reverse the digits of the integer Value. For example, if 542 is passed in, then ReturnNumber should get 245. Do NOT convert to/from String. You must ' implement this function using numeric processing. 31 32 Return ReturnNumber 34 End Function 36 37 38 Function HasDescendingDigits(ByVal Input As String) As Boolean Dim ReturnValue As Boolean True 39 40 41 42 43 Set ReturnValue to False if the first character in Input is not greater than the second and the third characters and that the second character is not greater than the third character. Do NOT test that the characters in Input are digits - that test will occur elsewhere just test their 'relation to one another. 45 46 47 48 49 50 51 52 53 54 Return ReturnValue End Function Function IsInteger (ByVal Input As String) As Boolean Dim ReturnValue As Boolean True Set ReturnValue to False if Input does not contain an integer (Note that we already know that it contains a number via IsNumeric). Return ReturnValue End Function 56 End Class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions