0
HTW Berlin Medieninformatik

HTW Berlin
Fachbereich 4
Internationaler Bachelor Studiengang
Internationale Medieninformatik (Bachelor)
Info 2: Informatik II
Summer Term 2023


Exercise 2: Histogram

Finger Exercises

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

  1. In some programming languages, such as Ada, you can define an array of characters with any discrete type as the index:

    someArray : ARRAY ['A' .. 'Z'] of INTEGER;

    You can then access the array, for example, using a value of character type: 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. Normalization of Strings means transforming all Strings to either uppercase or lowercase before comparing them. Write a method that takes a character as a parameter and returns a normalized version of the character without using the methods available in the Java String class. 
  3. What is a "carriage return"? Where does the name come from?

 


Lab exercises:

These are the required exercises for this week.

Work in groups of two, each of you hands in the same report. Remember to put your names on the report. You can continue to use BlueJ, or move to Eclipse, if you wish. We will be practicing much programming in the small this semester.

  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 int to a file? A float? Don't convert the last two to Strings. Observe what happens. 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 for this file: https://sherlock-holm.es/stories/plain-text/cano.txt
  4. Output a histogram of the character frequencies. One simple kind of histogram has horizontal lines proportional to the magnitude of the number it represents. For example:

    A : **********
    B : *****
    C : ******
  5. What is the complexity of your algorithm?
  6. Now ask ChatGPT to create a program in Java that reads in a file and creates a histogram of letter frequencies. Compare this to your result. Does the generated code compile? Does it work? How does it differ from your code?

For the bored:

  1. The Sherlock Holmes file has a lot of special characters. Include them in your histogram!
  2. Make your histogram application display the histogram with vertical lines and input the file name as a parameter.
  3. Look up Unicode - there are a lot more characters here! How many more? Assume you have a text with an unknown number of different (and bizarre) Unicode characters. How can you make a histogram for such a text?

 

Your report is due by 10.00 pm the night before your next lab! As in Informatics 1, 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. How many lines of code did you write for each exercise? Record this in your report.


Copyright Prof. Dr. Debora Weber-Wulff
Questions or comments: <weberwu@htw-berlin.de>
Some rights reserved. CC-BY-NC-SA - Copyright and Warranty