created 06/16/08


Chapter 29B Programming Exercises


Exercise 1 — Pascal to Java Translator

In the Pascal programming language, the assignment operator is the two character sequence := instead of a single = character as in Java. Write a program that reads in a source file line by line. Each sequence := in a line is changed into = before the line is output. (Of course, much more than this is needed to fully translate Pascal to Java.) Use redirection for file input and output:

C:\>java Transfomer < Hello.pas > Hello.java

Look at the chapter "File Input and Output" if it is not clear what the command line is doing. Here is a sample Pascal program (copy and paste it to a file for use with your program):

program noparmTest (input, output);
 var a, b : integer;
        c : real;
  function two: result integer;
    var n : integer;
    begin
      c := a + b;
      two := -c
    end
  begin
    a := 1;
    b := 2;
    c := a+b+two;
    write(c)
  end.

The output should be:

program noparmTest (input, output);
 var a, b : integer;
        c : real;
  function two:
    var n : integer;
    begin
      c = a + b;
      two = -c
    end
  begin
    a = 1;
    b = 2;
    c = a+b+two;
    write(c)
  end.

Here is a more challenging test file (which happens not to be legal Pascal):

program badPascal ( input, output );
var x, y, z : integer; {single colon is OK}
begin
  if x = y then      {will this mess up?}
     x := y+ z;      {should be no problem}

{check that blank line}

:= x*y/12.3;  {what about := at the start?}

  x := y;  y := z;  p := q  {multiple := in this line}
  
end.

To make the program easier, you could assume that there is only one := per line. Otherwise, you will need nested while loops.

Hints: use a Scanner and nextLine()for input. The outer while loop of the program uses hasNextLine() to decide if it should continue. Use indexOf() to look for :=. Use substring() and concat() (or +) to create the transformed line.

Click here to go back to the main menu.

Click here view the solution for this question


Exercise 2 — Lint Filter

An if statement or a while statement in Java should usually not contain the character =, unless it is part of ==, <=, >=, or !=. Sometimes, however, a single = is correct. Write a program that reads in a Java source program line by line and outputs only those lines that start with "if" or "while" (possibly preceeded by whitespace) and that somewhere contain a single equal sign with at least one space on the left and right: " = " . The user can then inspect each line of the output for problems. A much better program would detect a single "=" even when not surrounded by spaces, but that is much harder to write.

Use the methods startsWith(), trim(), and indexOf(). There are other methods that would simplify this program, but don't use them here.

C:\>java Line < BuggyProgram.java

A program, such as this, that filters out suspicious lines is sometimes called a "lint filter". Here is a program to test your filter:

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

class BigProblems
{
  public static void main ( String[] args )
  {
    boolean goOn = true;
    Scanner scan = new Scanner( System.in );
    System.out.println( "Enter an integer: ");
    int data = scan.nextInt();
    while ( goOn = true )
    {
      if ( data%2 = 1 )
        System.out.println( "Your number is odd" );
      if ( data%2=0 )
        System.out.println( "Your number is even" );

      System.out.println( "Enter another integer or 0 to quit: ");
      data = scan.nextInt();
      if ( data == 0 ) goOn = false;
    } 
  }
}

Click here to go back to the main menu.

Click here view the solution for this question


Exercise 3 — File Compressor

The English, words "a" and "the" can mostly be removed from sentences without affecting the meaning. This is an opportunity for compressing the size of text files! Write a program that inputs a text file, line-by-line, and writes out a new text file where each line has the useless words eliminated.

First write a simple version of the program that replaces substrings " a " and " the " in each line with a single space. This will remove many words, but sometimes these words occur at the beginnings or ends of lines, and sometimes the words start with capitals. So, improve your first program so that it handles those situations as well.

C:\>java Remover < verbose.txt > terse.txt

Note: there are various replace() methods of class String that would simplify this program. Try to write this program without using them.

Click here to go back to the main menu.

Click here view the solution for this question


Exercise 4 — Secret Code

A text message has been encoded by replacing each character of the message with an integer. Each integer is an index into a keyphrase that contains all the lower case letters of the alphabet as well as the space chacter. The keyphrase may contain the same character in several locations. The encoded text is series of integers, like this:

35 10 10 33 9 24 3 17 41 8 3 20 51 16 38 44 47 32 33 10 19 38 35 28 49 

To decode the message, look up each integer in the keyphrase and output the corresponding character. For example, say that the keyphrase is this (the index of each character has been written above it):

          111111111122222222223333333333444444444455
0123456789012345678901234567890123456789012345678901 
six perfect quality black jewels amazed the governor

using each integer from the encoded text as an index into the phrase results in the decoded message:

attack the bridge at dawn

Write a program that decodes a secret message contained in a text file. The first line of the text file contains the keyphrase. Then the file contains a sequence of integers, each of which indexes the keyphrase. Find the character corresponding to each integer and output the secret message. Note if a character such as 'e' occurs several places in the keyphrase it may be encoded as different integers in different parts of the secret message.

(The recipient of the secret message gets only the file of integers and must put the keyphrase at the top of the file.) For example, here is the contents of a secret message file ready for the program:

six perfect quality black jewels amazed the governor
35 10 10 33 9 24 3 17 41 8 3 20 51 16 38 44 47 32 33 10 19 38 35 28 49 

Here is a sample run of the program:

C:\> java Decode < secretFile.txt

attack the bridge at dawn

You will need the charAt() method of String.

Here is another secret message file, with keyphrase inserted, that you can use to test your program:

six perfect quality black jewels amazed the governor
31 16 2 3 4 42 48 7 27 9 10 43 12 13 35 15 1 40 18 3 
20 15 33 23 24 32 26 29 28 27 21 31 25 14 34 14 36 
42 38 19 40 41 27 3 44 50 46 42 48 49 50 6

Click here to go back to the main menu.

Click here view the solution for this question