Você está na página 1de 3

C# to Java practical Lab exercises

The aim is to get each of the three C# programs converted to Java and running as Java
projects in NetBeans. Skeleton projects have been provided. More detailed instructions are
given below.
1. HelloDogs from week 3 of AWD
1.1Download the zipped C# and Java projects and unzip them in separate folders.
1.2Start Visual Studio and NetBeans (NetBeans is on the Start Menu under Programming>Java) and open the two projects (File->Open Project in NetBeans).
1.3In NetBeans open the skeleton classes for Dog.Java and DogChorus.java.

1.4In Visual Studio open Dog.cs. Select all the code from within the Dog class and copy
and paste it into the Java Dog class.

1.5Make the changes to the code in Dog.java so that it will compile as a Java class.
1.6Do the same for DogChorus i.e. copy the code in the DogChorus class in DogChorus.cs
from Visual Studio into the corresponding Java class in NetBeans and convert the code.
1.7Check that there are no errors by cleaning and building the project in NetBeans. To do this
you can right-click the project name in the Projects panel and select Clean and Build.
Any error messages will appear in the Output panel at the bottom.
1.8If there are no errors run the project. You can do this by right-clicking the project name
and selecting Run. This will execute the main() method in the class HelloDogs. This
creates an instance of DogForm which provides the GUI for the program. With luck the
program will run and behave in a similar way to the equivalent C# program.
1.9Note that if you double click DogForm.java it will open in the GUI builder where you can
create Java in a similar way to in Visual Studio.

2. Cats with inheritance from week 9 of AWD


Note that we are not studying Java web applications in this course, at least not in any depth,
as they are covered in a final year course (Programming Distributed Components). However
for your interest I have provided both a Java Desktop and a Java Web skeleton version of this
project so that you can experiment with it. The code for the classes Cat and BreedCat are
exactly the same for both the desktop and web projects which illustrates that good design can
make your code reusable. Start by working on the Desktop project called
CatExampleWithInheritanceJavaDesktopSkeleton
2.1Following the method described above for HelloDogs copy the code for Cat and BreedCat
from Visual Studio into the corresponding skeleton classes provided in the NetBeans
project. Then make the code changes necessary to the classes so that they will compile as
Java.
2.2Run the project by right-clicking the project name and selecting Run. A desktop version of
the cats program should appear behaving in a similar way to the .Net cats web application.

2.3OPTIONAL EXERCISE If you are interested do the same for the web application. The
NetBeans web project is called CatExampleWithInheritanceJavaWebSkeleton. This time
when you run the application it will start a web server plugged in to NetBeans and a web
browser to access the web application. The GUI is defined in the Java Sever Page (JSP) file
called index.jsp in the Web Pages folder. Note that JSP is outside the scope of this course
and this version is just provided for your interest.

3. Generics Example from week 6 of AWD


As with the Cats example two NetBeans projects are provided: a desktop version and for your
interest a web version. Start by working with the Desktop project which is called
GenericsExampleJavaDesktopSkeleton.
3.1Follow the same procedure as for the Dogs and Cats projects. This time the classes to
convert are Person and People. Once you have converted the classes and successfully
built the project you should be able to run it and be rewarded with a Java Desktop version
of the .Net web application

3.2OPTIONAL EXERCISE If you are interested do the same for the web application. The
NetBeans web project is called GenericsExampleJavaWebSkeleton.

Você também pode gostar