IC211: Lab 2

Due: End of class on Thursday 20 Jan.
  1. Complete transition from C++ to Java, including:
    1. Loops
    2. Arrays
  2. Practice your structured programming

  1. Loops
  2. Loops work pretty much the same as in C++. We have while loops and for loops. There is only one major difference: The test must be of type boolean.
    allowed in C++ but not in java:
        int i = 10;
        while(i) {
          cout << i << " ";
    Rot13 is a simple Caesar's cypher that given a string replaces each letter with the letter 13 later in the alphabet(wraping around when you get to the end). More, formally: if c is the position of a letter in the alphabet (not the ascii or unicode value), ROT13(c) = (c+13)%26. You have to convert from unicode to the position value before apploying that function, and convert back to get the new character. This may require consideration of the distinction between upper and lower case in unicode. A nice property of ROT13 is that when you run it twice, you get back to the original text.

    Write a program that reads a line of text, performs ROT13 on it and prints the result. Non-letter characters should not be changed, and capitalization should be preserved, e.g. A → N and a → n. Show this to your instructor.

    Things you need to know:

    1. If you have a String, putting .charAt(i) after the string name, returns the character at position i:
      	String s;
      	char c = s.charAt(0);//returns the first character
    2. Placing .length() after the string name returns the length of the string:
      	String s;
      	for(int i = 0 i < s.length(); i++){

  3. Arrays.
  4. In java, there are only dynamic arrays.
          int anIntArray[];
          boolean aBoolArray[] = new boolean[10];
          String[] aStringArray = { "array", "of", "String" };
    Some things to note:
    1. The declaration creates the variable, not the memory itself, so we need the "new"
    2. The brackets can go on either side of the variable name.
    3. Access is done as with C++:
      	  int[] ia = new int[101];
      	  for (int i = 0; i < ia.length; i++)
                ia[i] = i;
      	  int sum = 0;
      	  for (int i = 0; i < ia.length; i++)
                sum += ia[i];
    Write a program that reads in 10 words from a line of text, and sorts them in alphabetical order. You may wish to use selection sort as discussed in IC210.

    Things you need to know:

    1. When you pass arrays to functions in Java, you don't use the *: public static int maxIndex(String [] arrayIn, int sizeIn) NOT int maxIndex(String *arrayIn, int sizeIn)
    2. You cannot compare two strings with the <, you need to use .compareTo(String t), which returns 0 if the string are as in:
      	String s,t;
      	if(s.compareTo(t) < 0) ...
      s.compareTo(t) is less than 0 if s comes before t, equal to 0 if they are the same, and is greater than 0 if s comes after t.
    Show this to your instructor when you are done.