CS 410 - Homework Assignment 1

This assignment is about using a Finite State Machine to help you design and write a program that processes strings. You are to write a Java application that reads lines of text from standard input and writes strings to standard output that represent floating point literals.

Write a Java program (called Filter.java) that reads a line of text from standard input and then finds within that line of text a floating point literal. That is, your program should "clean up" the input strings by discarding unneeded and invalid characters, so that what is left is a valid floating point literal (or the empty string) and write the cleaned up string to standard output. Here are some examples. The string "a1b*3+.y7" should be cleaned up to be "13.7". The string "3a4b5c.d.6e-f7" should be cleaned up to be "345.6e-7". The string "e.1^2*00e003e" should be cleaned up to be ".12e3".

Here is a description of what a valid floating point literal looks like.

Java Floating Point Literals

Here are a few more requirements for your output strings (these requirements are not really part of the definition of a floating point literal).

  • No leading zeros in the integer part unless the zero is followed by a decimal point (so "0.1" is valid but "0000.1" should become "0.1" and "0001" should become "1").
  • No trailing zeros in the fraction part unless the zero is preceded by a decimal point (so "1.0" is valid but "1.000" should become "1.0" and "2.1000" should become "2.1".
  • No leading zeros in the exponent part.
  • Simplify +/- signs in both the integer part and the exponent part. So "++1" should become "+1", "--1" should become "+1" and "-+--1" should become "-1".

You should design a Finite State Machine and then write a Java program that implements that FSM. As part of this assignment you should turn in an image of your FSM. Since the alphabet for your FSM is the whole ASCII character set, it is not practical to label edges with all of the possible input characters. So use the following conventions to simplify your FSM diagram. Use notation like 0-9 to mean all the characters between 0 and 9. Use the character * on an edge leading out of a state to mean that that edge is for all the characters not mentioned on any other edge leading out of that state (in other words, * will mean a kind of "default" transition).

Here are some programs that can be used to describe and draw FSM's. If you find another program that you think works well, please let me know about it.

It is important that your program read from standard input and write to standard output because I will use that fact to automatically test your program on a lot of input strings. To further make the testing easier, make sure you call your Java program Filter.java.

Turn in a zip file called CS410Hw1Surname.zip containing your Filter.java source file, an image file of your Finite State Machine, and the source code to your FSM (in whatever description language you used). This assignment is due Thursday, February 5.


Return to the main homework page.
Return to the CS 410 home page.


compliments and criticisms