Programming exercises in Java, VBA, VB.NET

March 24, 2009JPNo CommentsRate This ArticlenewLinks to this article


    Submitted for your approval: three programs that all do the same thing, in three different languages, as a programming exercise for those of you interested in this sort of thing.

    Each of these three programs does the same thing, in nearly the same way: You choose whether you want upper or lower case letters, and the code builds a string consisting of the alphabet, either upper or lower case.

Java:

    The Java program begins by creating a 26 character array, then gets the initial Chr() value from a custom function called getCase. This function prompts the user for the case they want for the alphabet. If you type 'lower', it returns the number 97 (the character value for the letter 'a') otherwise it returns 65 (the character value for the letter 'A').

(Tip: you can verify these values by opening Excel and, in cell A1, typing =CHAR(ROW()) and filling down to row 122 and beyond.)

    Then a For loop loops through the numbers 0 through 25 (the elements of the alphabet[] array) and assigns characters to each member of the array. The current value of the alphabet array is printed to the console / debug window.

    The character value is incremented with each iteration of the loop, so it either loops through the numbers 97 through 122 (lower case) or 65 through 90 (upper case), depending on what the user chose when prompted earlier. The char modifier casts the number into its character equivalent.

import java.io.*;
import java.util.*;

public class alphab
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {
        char[] alphabet = new char[26];

        int asc_val = getCase();

        for (int i = 0; i < alphabet.length; i++) {
            alphabet[i] = (char)asc_val;
            System.out.print(alphabet[i] + " ");
           asc_val++;
        }
    }

    private static int getCase() {
        // returns user choice to calling sub
        // if no choice is made (or invalid choice), assume uppercase

        // variables
        String case_type = new String();
        final String lwr = "lower";
        int return_type = 65;

        System.out.println("Enter 'upper' for uppercase,
                    'lower' for lowercase"
);
        case_type = console.next();

        if (case_type.equals(lwr))
            return_type = 97;

        return return_type;
    }
}

VBA:

    In VBA, we can't print the array members directly, so we build a string and display it in a messagebox. Otherwise it's nearly identical.

Sub alphab()

Dim alphabet(1 To 26) As String
Dim asc_val As Long

' get user choice, upper or lower
' default is upper
asc_val = getCase

' loop through array and populate it with alphabet
Dim i As Long
Dim strAlphabet As String
For i = 1 To 26
  alphabet(i) = Chr(asc_val)

  ' build string consisting of upper or lowercase letters
 strAlphabet = strAlphabet & " " & alphabet(i)

  asc_val = asc_val + 1
Next i

' show result
MsgBox strAlphabet

End Sub

Private Function getCase() As Long

' returns user choice to calling sub
' default is upper case

Dim case_type As String
Dim return_type As Long

Const lwr As String = "lower"
Const UPPER_CASE As Long = 65 ' asc char value for uppercase A
Const LOWER_CASE As Long = 97 ' asc char value for lowercase a

case_type = InputBox("Enter 'upper' for uppercase, 'lower' for lowercase")

If case_type = "lower" Then
  return_type = LOWER_CASE
Else
  return_type = UPPER_CASE
End If

getCase = return_type

End Function

VB.NET:

    The VB.NET version is most similar to the VBA version, with a few borrowed elements from Java.

Public Class alphab
  Public Sub alphab()
    Dim alphabet(0 To 25) As String
    Dim asc_val As Long

    asc_val = getCase()

    ' loop through array and populate it with alphabet
   Dim i As Long
    Dim strAlphabet As String

    For i = 0 To 25
      alphabet(i) = Chr(asc_val)

      ' build string consisting of upper or lowercase letters
     strAlphabet = strAlphabet & " " & alphabet(i)

      asc_val += 1

    Next i

    ' show result
   MsgBox(strAlphabet)
  End Sub

  Private Function getCase() As Long
    ' returns user choice to calling sub
   ' default is upper case

    Dim case_type As String
    Dim return_type As Long = 65

    Const lwr As String = "lower"
    Const LOWER_CASE As Long = 97 ' asc char value for lowercase a

    case_type = InputBox("Enter 'upper' for uppercase, 'lower' for lowercase")

    If case_type.Equals(lwr) Then
      return_type = LOWER_CASE
    End If

    getCase = return_type

  End Function
End Class

    Notes: The .NET IDE (Visual Basic 2008 Express Edition) wouldn't let me create a one-based array. Or at least, I don't know how. If it's the same as VBA (putting Option Base at the top of the module) then I'd rather not.

    It's funny to see how you can combine elements of VBA and Java in .NET. For example, you can declare and initialize a new variable at the same time in .NET (i.e. "Dim return_type As Long = 65"), saving space. VBA won't let you do that.

    You can also use operators like += in .NET; VBA just throws an error.

    Java is the only language of the three that can print multiple times to a single line, using the print function. With VBA and VB.NET, I had to build strings in order to get everything to print on one line; Debug.Print inserts a carriage return every time I use it.

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

2 Response(s) to Programming exercises in Java, VBA, VB.NET ↓

  1. Stan Scott says:

    One suggestion with VBA. To display an array as a string, it's much more important to first populate the array, and THEN convert it to a string, like this:

    strAlphabet = Join(alphabet," ")

    The second argument of the Join determines what character, if any, will be used to separate the array values.

  2. JP says:

    I used a loop and combined those two steps, but you've got a point. Built in functions (where available) should be used in place of loops whenever possible.

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated September 2, 2010 @ 7:03 pm