FHTW Berlin

FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG2: Programmierung II
Sommersemester 2003


Exercise 1: Arrays, Matrices, and Files

Pre-lab:

Please think through the following questions before coming to the lab. Write down your answers and bring them with you for check-in.

  1. In some programming languages, such as Ada, you can define an array of characters:
    someArray : ARRAY ['A' .. 'Z'] of INTEGER;
    and then access the array using a character: someArray['T']. Java does not have this feature. How would you go about making an array in Java for representing counters for the letters 'A' to 'Z'?
  2. We talked about normalization of Strings in the lecture. Write a method that takes a character as a parameter and returns a normalized version of the character.
  3. A Matrix can be considered to be an Array of Arrays or a Vector of Vectors. Draw two pictures of a 4 x 5 Matrix, one as an Array of Arrays and one as a Vector of Vectors.
  4. How would you go about inserting a row into a Matrix? How would you delete a column? Write this down in German or English, not in Java

Required exercises:

These are the required exercises for this week. Work in groups of two, and turn in just one report for the group.

  1. How do you go about reading in characters from a file? Write and test a method that returns the next character in a file. Note that you have to do something with the carriage returns - such as ignoring them - and that you have to decide what to do when there are no characters to be returned.
  2. How do you write a String to a file? How do you write an Integer to a file? An int? How do you create a file, anyway?
  3. Now the fun begins! Write a Java application to read in a file character by character, counting the frequencies with which each character occurs. When there are no more characters, create a file frequency.txt and output the frequencies for each character.
  4. A class for a Matrix data type could have the following methods:

    public class Matrix{
      public Matrix (int h, int w){}
      // pre: h >= 0, w >= 0;
      // post: constructs an h row by w column matrix

      public Object elementAt (int row, int col) {}
      // pre: 0 <= row < height(), 0 <= col < width()
      // post: returns the object at (row, col)

      public void setElementAt (Object value, int row, int col){}
      // pre: 0 <= row < height(), 0 <= col < width()
      // post: changes location at (row, col) to value

      public void insertRowAt (int r){}
      // pre: 0 <= r <= height()
      // post: inserts row of null values to be row r

      public void insertColAt (int c) {}
      // pre: 0 <= c <= width()
      // post: inserts column of null values to be column c

      public Vector removeRowAt (int r){}
      // pre: 0 <= r < height()
      // post: removes row r amd returns it as a Vector

      public Vector insertColAt (int c) {}
      // pre: 0 <= c < width()
      // post: removes column c and returns it as a vector

      public int width(){}
      // post: returns number of colums in the matrix

      public int height(){}
      // post: returns number of rows in matrix

    }

    Implement a class for this data type! Test your class by constructing an addition table and a multiplication table from 1 to 12 using your implementation.

 

For the bored:

  1. Output a histogram of the character frequencies. A histogram has horizontal lines proportional to the magnitude of the number it represents. For example:
    A : **********
    B : *****
    C : ******
  2. Design and implement a class MatrixAddition. Use another group's Matrix implementation! Can you find errors in their implementation? 

    a11 a12   +   b11 b12   =   a11+b11 a12+b12
    a21 a22        b21 b22        a21+b21 a22+b22

For the really bored:

  1. Make your histogram application display the histogram with vertical lines and input the file name as a parameter.
  2. What is the maximum size of an array of Strings that can be allocated on your machine? What is the maximum size of an array of Integers? Of booleans? Now look at matrix size: How large can a Matrix get for Strings, Integers or booleans? 
  3. An m x n Matrix  could be implemented using a single Vector with mn locations. Assuming that this choice was made, how would you implement the method elementAt()? Reimplement Matrix using just a single Vector!

Your report is due by 9.30 the morning of your next lab! As in Programming I, I am more interested in process than in product, although we are now getting more interested in products as well. Your report should include any collaborators, summarize what you learned, and note the time you invested in this exercise.

 


Letzte Änderung: 15.03.03 - 22:22