$24
This lab will help you to practice reading from/writing to files and catching/throwing exceptions. All parts of this lab must be part of a single java program – do not turn in more than one .zip file.
Write a method to prompt the user for a double value between 0.0 and 1.0, inclusive. If the user enters a something that is not a number, or if they enter a number that is not in that range, display an appropriate error message and reprompt them. For instance, when called like this from main:
double val = getValueBtw0And1(); System.out.println(“You entered “ + val);
a possible execution of the program is:
Enter a value
between 0.0
and 1.0, inclusive: bob
Error: bob
is
not
a number.
Enter
a value
between
0.0
and 1.0, inclusive: 2
Error: 2.0
is
not
between 0.0 and 1.0
Enter
a value
between
0.0
and 1.0, inclusive: 1
You entered 1.0
Provide a value for the parameter s that will cause the following output (given in parts a through d). If it is not possible to generate a particular output, explain why. Write your answers to this question within a comment block in your .java file.
public static void problem2(String s) {
try {
int stringLen = s.length();
System.out.println("The length of the string is " + stringLen);
int i = Integer.parseInt(s);
System.out.println("The value is " + i);
} catch (NumberFormatException e1) {
System.out.println("A number format exception occurred");
} catch (NullPointerException e2) {
System.out.println("A null pointer exception occurred");
} catch (Exception e3) {
System.out.println("Some type of exception occurred");
} finally {
System.out.println("In the finally block");
}
}
a)
A null pointer exception occurred
In the finally block
b)
The length of the string is 1
A number format exception occurred
In the finally block
c)
A null pointer exception occurred
Some type of exception occurred
In the finally block
d)
The length of the string is 1
The value is 5
Note: For part d, notice that there is no mention of the finally block.
Write a method that prompts the user for a string indicating a filename and an integer x and displays to the console every xth word of the file. Your method should not throw any exceptions – handle them within the method. Assuming the file has the following contents:
Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth.
and the integer is 5, the program should produce this output:
years forth new and that equal. in testing any so We great We a as for their might fitting should a not consecrate, this living here, above add will remember but what is rather, to they thus It to the us—that we that gave of resolve not this have freedom—and people, the from
Write a method that takes a string indicating a filename of the form filename.txt and create a file called filename.rev with the contents of the file reversed. If the input filename ends in .txt but doesn’t exist, your method should throw a FileNotFoundException. If the input filename doesn’t end in .txt throw a custom exception called InvalidFilenameException. Call your method three times from main: once with a filename that ends in .txt and is an existing file, once with a filename that ends in .txt but is not an existing file, and once with a filename that does not end in .txt.
Assuming the text provided in problem 3 is contained in a file gettysburg.txt, then calling the method with the parameter gettsyburg.txt should generate a file called gettysburg.rev with the contents:
.htrae eht morf hsirep ton llahs ,elpoep eht rof ,elpoep eht yb ,elpoep eht fo tnemnrevog taht dna—modeerf fo htrib wen a evah llahs ,doG rednu ,noitan siht taht—niav ni deid evah ton llahs daed eseht taht evloser ylhgih ereh ew taht—noitoved fo erusaem lluf tsal eht evag yeht hcihw rof esuac taht ot noitoved desaercni ekat ew daed deronoh eseht morf taht—su erofeb gniniamer ksat taerg eht ot detacided ereh eb ot su rof rehtar si tI .decnavda ylbon os raf suht evah ereh thguof ohw yeht hcihw krow dehsinifnu eht ot ereh detacided eb ot ,rehtar ,gnivil eht su rof si tI .ereh did yeht tahw tegrof reven nac ti tub ,ereh yas ew tahw rebmemer gnol ron ,eton elttil lliw dlrow ehT .tcarted ro dda ot rewop roop ruo evoba raf ,ti detarcesnoc evah ,ereh delggurts ohw ,daed dna gnivil ,nem evarb ehT .dnuorg siht wollah ton nac ew ,etarcesnoc ton nac ew ,etacided ton nac ew ,esnes regral a ni ,tuB
.siht od dluohs ew taht reporp dna gnittif rehtegotla si tI .evil thgim noitan taht taht sevil rieht evag ereh ohw esoht rof ecalp gnitser lanif a sa ,dleif taht fo noitrop a etacided ot emoc evah eW .raw taht fo dleifelttab taerg a no tem era eW .erudne gnol nac ,detacided os dna deviecnoc os noitan yna ro ,noitan taht rehtehw gnitset ,raw livic taerg a ni degagne era ew woN
.lauqe detaerc era nem lla taht noitisoporp eht ot detacided dna ,ytrebil ni deviecnoc ,noitan wen a tnenitnoc siht no htrof thguorb srehtaf ruo oga sraey neves dna erocs ruoF
Calling the method with a filename that ends in .txt but does not exist should result in this output:
The file could not be found.
Calling the method with a filename that does not end in .txt should result in this output:
Your filename must have a .txt extension.
5. This problem involves two input files with the following format:
FirstName, LastName, Title, Salary
The entries within each the files will already be sorted in ascending order by salary. Write a method that produces a new file containing the entries from both input files, still sorted in ascending order by salary. If any entry is missing a field, you should ignore it (i.e. do not write that entry to the combined file). Prompt the user for the names of the input files and the output file. Handle all exceptions appropriately, including displaying “friendly” error messages to the user rather than the stack trace. Assuming the first input file has the contents:
Smith, John, Ensign, 22000
Doe, John, Ensign, 25000
Jones, , Crewman, 30000
Scott, Montgomery, Engineer, 60000
Kirk, James, Captain, 75000
Pike, Christopher, Admiral, ,
and the second input file has the contents:
Chapel, Christine, Nurse, 55000
McCoy, Leonard, Doctor, 70000
Spock, ,Commander, 72000
the output file should have the contents:
Smith, John, Ensign, 22000
Doe, John, Ensign, 25000
Chapel, Christine, Nurse, 55000
Scott, Montgomery, Engineer, 60000
McCoy, Leonard, Doctor, 70000
Kirk, James, Captain, 75000
Rubric
For each question (20 points total):
Computes the correct answer: 2 point
Complies with all directions: 1 point
Follows style guidelines and commented as appropriate: 1 point