Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

C# Sharp to Java - Practice Exercises Java


Lesson 8:

File management


Exercise 8.11:

C# Sharp to Java


Objetive:

Create a basic C# Sharp to Java translator.

It must accept a C# Sharp source files, and create an equivalent Java source file. It will receive the file name in the command line, and it must translate at least:

"Main()" into "main( String[] args )"
"string" into "String"
"bool" into "boolean"
"Console.WriteLine" into "System.out.println"
" : " into " extends " if it is in the same line as the word "class" (and any further improvements you may think of, such as strings handling, or converting a ReadLine to a try-catch block).


Source Code:


import java.util.*;
public class CsharpToJava
{
	public static void main(String[] args)
	{
		String line;
		String name;

		if (args.length < 1)
		{
			name = new Scanner(System.in).nextLine();
		}
		else
		{
			name = args[0];
		}

		java.io.FileReader iFile = new java.io.FileReader(name);
		java.io.BufferedReader iFileBufferedReader = new java.io.BufferedReader(iFile);
		java.io.FileWriter oFile = new java.io.FileWriter(name + ".java");

		do
		{
			line = iFileBufferedReader.readLine();

			if (line != null)
			{
				line = line.replace("bool ", "boolean ");
				line = line.replace("string ", "String ");
				line = line.replace("Console.WriteLine", "System.out.println");

				oFile.write(line + System.lineSeparator());
			}

		} while (line != null);

		iFile.close();
		oFile.close();
	}
}
Exercisey 8.11

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.