COSC4301                             Test-1                            3/21/2012

 

 

Name (Please PRINT IN CAP):________________________________________

 

Choose and Do six problems:

 

A.                    ArrayList <String> toDoList = new ArrayList <String> ();

                        toDoList.add (“FirstLine”);

                        toDoList.add (“SecondLine”);

                        toDoList.add (“ThirdLine”);

                        toDoList.add (“ForthLine”);

                        toDoList.add (1, “NewLineAddedAtIndex1”);

                        toDoList.add (2, “AnotherLineAtIndex2”);

                        toDoList.set (4, “BeautifulDay”);

                        String sss = toDoList.remove (1);

                        sss = toDoList.get (1);                          

                        System.out.println (“\n REMAINING ITEMS IN THE TODOLIST:::\n”);

                        for (String moreitem: toDoList)   // for-each loop

                              System.out.println (moreitem);

 

            Give the outputs of the above program segment.

 

 

B.         Suppose that a year is a Leap year if it is a multiple of 4 but not that of 100.

            Construct a for loop which prints out all leap years between year 2010 and year 3000 such that

            exactly ten leap years will be printed out on each output line except that the first leap year of

every new century must be printed on a new output line.   

 

 

C.         Explain  this parameter.

 

D.        Write a main() method that does the following assuming that all needed packages are imported:

            1. read all input lines from an input file given as the first argument in the command.

            2. retrieve all words separated by whitespaces from all input lines taken and store them into an

                array of Strings.

            3. and, sort these words in alphabetically ascending order and resulting sorted words will be stored back

    into the initial array.

 

 

E.         Consider the class IntegerArray of Program-2.

 

Design and code a second constructor with the following header:

 

Public IntegerArray(FileInputStream inStream) throws FileNotFoundException

 

Complete this second constructor which will, just like the first constructor, set the Instance Variable original by reading integers from the given input Stream to be passed as the lone parameter of the Constructor.  Remember that you need to create a Scanner object using the given input stream in order to get inputs

 

 

F.         Consider the class IntegerArray of Program-2.

Design and code the following two methods  whose headers will be, respectively:

public int median() // finds and returns the median of array sorted instead of array original.

public float average() // calculates and returns the average of array original.

 

 

 

G.         The (generic) Class ArrayList is defined and included in the java.util package.

Briefly compare this class to arrays and also list some useful methods of this class.

  .

 

H.        Consider the class Matrix of program-3.

            Design and code the definition of a method whose heading will be:

                   public void rowExchange (int row1, int row2)

 

            This method will simply exchange the given two rows of the instance variable inverse.

 

 

I.           Describe the effects of each of the following UNIX Commands:

 

1.             $  ls  -l  *.java

2.             $  javac   myFirst.java

3.             $  chmod  744   *.java

4.             $  cp  -r  dirA   dirB

5.             $  mv  fileA   fileB

6.             $  rm   fileA

7.             $  vi    fileA