Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSC120 Fall 2019 Final Exam

December 10, 2019

1.  (3 points) Writing a program that receives a ile path from the user and writes to a ile of the given path requires importing four classes. You can accomplish this with

import   java . io .*;

import   java . util . Scanner ;

but without using the *, you need to state the three classes from java.io, as in:

import   java . io . XXX ;  //  for   a   file

import   java . io . YYY ;  //  for   printing

import   java . io . ZZZ ;  //  for   run -time   error

State the three classes indicated as XXX, YYY, and ZZZ.

2.  (4 points) State the output of the following code fragment.

int   c  =  0 ,  d ;

for   (   int   i  =   1000;   i   >   1;   i  =   i  /  4  +   1   )

{

c  ++;

d  =   ( int )(  Math . sqrt (  c   )   );

System . out . printf (   "%04 d :%03 d :%+03 d \ n " ,  i ,  c ,  d   ); }

3.  (11 points) Write a public static method unique that receives a String[] parameter named words and returns an int representing how many diferent values appear in the array.  For example, if words is array with elements {"abc",  "def",  "abc",  "def",  "def"} returns 2. A suggested solution is as follows:

(i)  Create the exact copy of words.

(ii)  Sort the elements in the copy from (i).

(iii) Find out for how many values of i the sorted array has identical values between positions i-1 and i.

(iv)  Return the length of the array minus the quantity from (iii).

You may assume that import  java.util.*; appears at the start of the class ile in which this method appears.

4.  (25 points) Write a program named Hashes for the following task:

.  Receive a path to an input ile and a path to an output ile.

. If the iles are identical (in terms of the full paths), print an error message  "Identical files!".

.  Otherwise, if the ile speciied by the input path does not exist, is not a regular ile, or is not readable, produce an error message "An  invalid  file!".

.  Otherwise, read the contents of the ile line by line, and print the contents into the output ile with a preix attached to each line, where the preix rotates among #, ##, and ### in this order, starting with #.

Suppose we have a text ile named nightingale.txt, whose contents are:

" When   two   lovers  meet   in  Mayfair ,"

so   the   legend   tells ,

" songbirds   sing ,

winter   turns   to   spring .

Every   winding   street   in  Mayfair

falls   beneath   the   spell . "

I  know   such   enchantment   to  be

 cause   it   happened   one   evening   to  me :

then the program may go as follows:

Enter  input   file   path :  nightingale . txt

Enter   output   file  path :  foo . txt

After successful execution, the contents of foo.txt as:

#" When   two   lovers  meet   in  Mayfair ,"

## so   the   legend   tells ,

###" songbirds   sing ,

# winter   turns   to   spring .

## Every   winding   street   in  Mayfair

### falls   beneath   the   spell . "

# I  know   such   enchantment   to  be

## ’ cause   it   happened   one   evening   to  me :

Also, if the path foobar does not pass the tests, the program may go like:

Enter   input   file  path :   foobar

Enter   output   file  path :   foobar0

An   invalid   file !

If the user provides the same ile path for both, the program should behave as follows:

Enter   input   file  path :   foobar

Enter   output   file  path :   foobar

Identical  files !

The program detects the use of identical iles when paths difer but the iles are identical.  For example, if ddd is a ile folder, the program may behave as follows:

Enter   input  file   path :  ddd /../ foobar

Enter   output   file  path :   foobar

Identical  files !

We envision that the source code for this program takes the following form:

import   java . util .*;

import   java . io .*;

public   class   Hashes   {

public   static  void  main (   String []   args   )  throws   ZZZ   { Scanner   keyboard   =  new   Scanner (   System . in   );

System . out . print (  " Enter   input   file  path :   "   );

String   inputPath  =   . . .   //   file   path   [1]

System . out . print (  " Enter   output   file  path :   "   );

String   outputPath   =       //  file  path   [ 1]

XXX   fIn  =       //  input   file   [ 1]

XXX   fOut   =       //  output   file   [ 1]

if   (       )  {   //   check   name   equality   using   full   paths   [ 3]

System . out . println (  " Identical  paths !"   );

}

else   if   (       )  {   //   check   the   property   [6]

System . out . println (  " An  invalid   file !"   );

}

else   {

YYY   ppp  =       //  instantiation   [ 1]

Scanner   sss  =   . . .   //   [1]

int   counter  =   . . .   //   [1];

while   (   . . .   )  {   //   [1]

. . .   //  read ,  print ,   and   update   counter   [8]

}

sss . close ();

ppp . flush ();

ppp . close ();

}

}

}

Here each ... represents some missing code fragment and after // appears some hint. Also, XXX, YYY, and ZZZ are from Problem 1. The numbers in the square brackets show the points assigned.