Starting from:

$25

CS212- Object-Oriented Programming in Java: Lab 7 Solved

Aim: Working with Eclipse, Two-dimensional arrays and StringTokenizers.

Open Eclipse, set up your private workspace on the H: drive, and create a Java Project for Lab7. Instructions are provided separately.
A Java program (HelloWorld.java) has been provided on the public Z: drive under the folder Lab7. Import this program into your Lab7 project in Eclipse:
Right click on the src folder under Lab7 in your list of Eclipse projects.
Choose Import, expand General by clicking on the + sign, choose File System
Click on Next, then Browse and go to the Z: drive and select the folder Lab7 and click OK. Click the box next to HelloWorld.java (the file you want to import) and click Finish.
In Eclipse, double click the file HelloWorld.java. It should open the file in a tab. With the cursor somewhere inside the tab with the source code, right click and choose Run As Java Application. The program should run, and the output should appear at the bottom in the Console tab.
Using the same procedure to import a file, bring the program TwoDimArray.java into your Lab7 project. Open the file and observe what it does, and run it. The output in the console should be:
1 2 3 

4 5 6  7 8 9

Fill in the method PrintArrayEven with code that only prints the even numbers in the array. Instead of the odd numbers, print an asterisk ("*"). The output should be:
* 2 * 

4 * 6 

* 8 *

Now let's read the numbers for the array from a file. First, you will need to import the
TextFileInput program from the Lab7 folder in the Z: drive to the src folder of your Lab7 Java project. Also, import the file twodimension.txt into the Lab7 folder from the Z: drive.

        Open the file twodimension.txt. The first two lines represent the number of rows and columns. The rest of the file contains the numbers to be put in the array row by row. Write a method a method:

                                public static int[][] fillArray(String myFile)

        that will read from the input file myFile and return a two dimension array of integers that are read from the file. The method should read the first two lines of the file and declare the array (which it will eventually return), then have a doubly-nested for loop to read all the numbers into the array. Remember to use Integer.parseInt() to convert each of the strings read in from the file to integer. Run the main program again, using the array returned by this method for printing.

More products