Google Answers Logo
View Question
 
Q: JAVA ( No Answer,   5 Comments )
Question  
Subject: JAVA
Category: Computers > Programming
Asked by: axmedeey-ga
List Price: $30.00
Posted: 14 Mar 2004 02:41 PST
Expires: 31 Mar 2004 03:01 PST
Question ID: 316542
1) This part requires you to write two versions of a Java program that
performs a standard data-processing task, namely, to read and modify
the contents of one file, writing the results to a second file. In
particular:
·	The user on the command line provides the names of the input file
and output file when the program is run. Your program should check
that these two arguments are provided, that the first is a file that
is not a directory, and that the second is not an existing file. Hint:
the File class provides suitable methods.
·	The contents of the input file should be read in one byte at a time.
Hint: read () either returns an integer between 0 and 255 (the value
of an unsigned byte) or -1 (upon reaching the end of the input
stream).
·	Each byte read should be written to the output file as soon as it is
available, except for the following three transformations:
a)	The value 36 should be changed to the value 163, 
b)	The value 10 should be inserted after the value 12, and 
c)	If the value 111 is immediately followed by the value 101, they
should be replaced by the single value 248.
  Can you make the first version of your program single-threaded, and
the second version of your program multi-threaded:

Request for Question Clarification by maniac-ga on 28 Mar 2004 19:48 PST
Hello Axmedeey,

I saw your last comment. Do you still need help on this program?

If so, I suggest you:
 - post the complete error messages you get when trying to run the program
 - describe the Java development system used to build the application
 - describe the Java run time system you are using to run the application
Also indicate which operating system you are using [that may be relevant].

The original commenter might not respond but I (or another GA
researcher) may be able to handle your problems.

  --Maniac

Request for Question Clarification by maniac-ga on 30 Mar 2004 17:03 PST
Hello Axmedeey,

I am posting this as another RfQC since I cannot be sure the program
will work for you with your system. I was able to get the programs
running using javac / java.

I copied the source file for the "Single threaded one" and saved as
  STFileTranslator.java
I also commented the line 
  package ga;

There is also an apparent bug at line 47 - the message should use
outputFileName in the runtime exception message.

Then:
  javac -c STFileTranslator.java
produced STFileTranslator.class.

I then created a sample file named a.txt with the following contents:
Hello World
AAA


$200.00

Moebius

and ran
  java STFileTranslator a.txt b.txt
to produce a file that read
Hello World
AAA



£200.00

Møbius

Alas - you can't see the changes to the newlines (ASCII 10) and form
feeds (ASCII 12) but I did confirm that had changed using a hexdump
utility:
  hexdump a.txt
  hexdump b.txt
and comparing the output.

I did the similar steps for MTFileTranslator - and it worked just as well.

If this works and you wish to pay for my work in fixing this program -
please let me know. If not, please include the items I asked for
previously in a clarification.

  --Maniac
Answer  
There is no answer at this time.

Comments  
Subject: Re: JAVA
From: morkeld-ga on 21 Mar 2004 14:44 PST
 
// Single threaded one first
package ga;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class STFileTranslator
{
    protected boolean initialized = false;

    protected File inputFile = null;

    protected File outputFile = null;

    public STFileTranslator()
    {

    }

    public void init( String[] args )
    {
        if ( args.length != 2 ) {
            throw new RuntimeException(
                "Syntax:  java ga.FileTranslator <input file> <output file>");
        }

        String inputFileName = args[0];
        inputFile = new File(inputFileName);
        if ( !inputFile.exists() )
            throw new RuntimeException("Specified inputFile '" + inputFileName
                + "' does not exist.");
        if ( !inputFile.canRead() )
            throw new RuntimeException("Specified inputFile '" + inputFileName
                + "' cannot be read.");
        if ( inputFile.isDirectory() )
            throw new RuntimeException("Specified inputFile '" + inputFileName
                + "' is a directory, not a file.");

        String outputFileName = args[1];
        outputFile = new File(args[1]);
        if ( outputFile.exists() )
            throw new RuntimeException("Specified outputFile '"
                + inputFileName + "' should not exist.");

        initialized = true;
    }

    public void process() throws IOException
    {
        if ( !initialized ) {
            throw new RuntimeException(
                "FileTranslator object not properly initialized!");
        }

        int data, previousData = -1;
        BufferedOutputStream out = new BufferedOutputStream(
            new FileOutputStream(outputFile));
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(
            inputFile));
        while ( (data = in.read()) != -1 ) {
            if ( data == 36 ) {
                out.write(163);
            } else if ( data == 12 ) {
                out.write(12);
                out.write(10);
            } else if ( data == 111 && in.available() >= 1 ) {
                // wait until next loop, if more can be read
            } else if ( previousData == 111 ) {
                if ( data == 101 ) {
                    out.write(248);
                } else {
                    out.write(111);
                    out.write(data);
                }
            } else {
                out.write(data);
            }

            previousData = data;
        }

        out.close();
        in.close();
    }

    public static void main( String[] args ) throws IOException
    {
        STFileTranslator fileTranslator = new STFileTranslator();
        fileTranslator.init(args);
        fileTranslator.process();
    }
}


// Multi-threaded implementation second
package ga;

import java.io.IOException;

public class MTFileTranslator extends STFileTranslator implements Runnable
{
    public void run()
    {
        try {
            process();
        } catch ( IOException ex ) {
            System.err.println("Error while processing");
        }
    }

    public static void main( String[] args ) throws IOException
    {
        MTFileTranslator fileTranslator = new MTFileTranslator();
        fileTranslator.init(args);

        // Perform the processing in its own Thread
        Thread t = new Thread(fileTranslator);
        t.start();
    }
}
Subject: Re: JAVA
From: axmedeey-ga on 23 Mar 2004 10:54 PST
 
hi is this answer or comment. thank you
Subject: Re: JAVA
From: axmedeey-ga on 26 Mar 2004 09:18 PST
 
hi there the program did not run for me , so please can you make it
clear that every thing working properly then i my understand the whole
package.
whenever i try to run the program the gives me alot of errors, i don
not now what is going on, please help again thank you.
Subject: Re: JAVA
From: axmedeey-ga on 29 Mar 2004 05:41 PST
 
hi maniac the whole program did not work for me , so please can do it
again with a different stall, it may work, and i,m sure i,m using
everything wright.
thanks man
Subject: Re: JAVA
From: axmedeey-ga on 31 Mar 2004 03:01 PST
 
it did not work for me yet bu thanks for you help, it maybe my problem

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy