Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will need to import the following file into your new lab folder. Follow the instructions from Clsoed Lab 01 to import this file into

You will need to import the following file into your new lab folder. Follow the instructions from Clsoed Lab 01 to import this file into your ClosedLab12 folder.

Lab12a.java

In addition, you will need to place the following file in your ClosedLab12 folder to use as input for these exercises:

lab12aInput.txt

You will be writing a simple Java program that implements some basic file I/O operations. For the first exercise, you will write code that takes the name of a text file as input from the command line, opens that file if it exists and reads the file one line at a time. The program will reverse each line as it is read from the file and print the reversed line from the file to the console. For the second exercise, you will modify your code so that instead of printing each line reversed, the code prints the entire file reversed. For the third exercise you will modify your code one more time so that the program writes the output to a file instead of to the console.

Exercise 1 Description

Your code will prompt the user to enter a file name. If this file does not exist the program will produce an error message and exit. Otherwise the program will open the file and read a line from the file, reverse the line, and then print the line to the console. The program should process the entire file and terminate properly when the end of the file is reached.

Exercise 1 Sample Output

This is a sample transcript of what your program should do, assuming that the file lab12aInput.txt file provided above is used. Text in bold is expected input from the user rather than output from the program. Enter an input name: lab12aInput.txt ykcowrebbaJ sevot yhtils eht dna ,gillirb sawT' ;ebaw eht ni elbmig dna eryg diD ,sevogorob eht erew ysmim llA .ebargtuo shtar emom eht dnA !nos ym ,kcowrebbaJ eht eraweB" !hctac taht swalc eht ,etib taht swaj ehT nuhs dna ,drib bujbuJ eht eraweB "!hctansrednaB suoimurf ehT :dnah ni drows laprov sih koot eH thguos eh eof emoxnam eht emit gnoL ,eert mutmuT eht yb eh detser oS .thguoht ni elihwa doots dnA ,doots eh thguoht hsiffu ni sa dnA ,emalf fo seye htiw ,kcowrebbaJ ehT ,doow yeglut eht hguorht gnilffihw emaC !emac ti sa delbrub dnA hguorht dna hguorht dna !owt ,enO !owt ,enO !kcans-rekcins tnew edalb laprov ehT daeh sti htiw dna ,daed ti tfel eH .kcab gnihpmulag tnew eH ?kcowrebbaJ eht nials uoht tsah dnA" !yob hsimaeb ym ,smra ym ot emoC "!yallaC !hoollaC !yad suojbarf O .yoj sih ni deltrohc eH sevot yhtils eht dna ,gillirb sawT' ;ebaw eht ni elbmig dna eryg diD ,sevogorob eht erew ysmim llA .ebargtuo shtar emom eht dnA .)2781( erehT dnuoF ecilA tahW dna ,ssalG-gnikooL eht hguorhT morf -- Note that if a file name is provided that does not exist, the program should provide an error message and end rather than crashing: Enter an input file name: NoSuchFile.txt There was a problem reading from NoSuchFile.txt

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab12b.java in the same ClosedLab12 folder. For this exercise, you should extend the code that you wrote in Exercise 1. Your code should reverse the entire file instead of each individual line. (HINT: Think about how you can store ALL of the Strings from the file as you read it, then print them in reverse order).

Exercise 2 Sample Output

This is a sample transcript of what your program should do. Compare this to the output provided in Exercise 1 above - the last line of Exercise 1 is the first line below, and the first line of Exercise 1 is the last line below. The entire file has been reversed: Enter an input file name: lab12aInput.txt .)2781( erehT dnuoF ecilA tahW dna ,ssalG-gnikooL eht hguorhT morf -- .ebargtuo shtar emom eht dnA ,sevogorob eht erew ysmim llA ;ebaw eht ni elbmig dna eryg diD sevot yhtils eht dna ,gillirb sawT' .yoj sih ni deltrohc eH "!yallaC !hoollaC !yad suojbarf O !yob hsimaeb ym ,smra ym ot emoC ?kcowrebbaJ eht nials uoht tsah dnA" .kcab gnihpmulag tnew eH daeh sti htiw dna ,daed ti tfel eH !kcans-rekcins tnew edalb laprov ehT hguorht dna hguorht dna !owt ,enO !owt ,enO !emac ti sa delbrub dnA ,doow yeglut eht hguorht gnilffihw emaC ,emalf fo seye htiw ,kcowrebbaJ ehT ,doots eh thguoht hsiffu ni sa dnA .thguoht ni elihwa doots dnA ,eert mutmuT eht yb eh detser oS thguos eh eof emoxnam eht emit gnoL :dnah ni drows laprov sih koot eH "!hctansrednaB suoimurf ehT nuhs dna ,drib bujbuJ eht eraweB !hctac taht swalc eht ,etib taht swaj ehT !nos ym ,kcowrebbaJ eht eraweB" .ebargtuo shtar emom eht dnA ,sevogorob eht erew ysmim llA ;ebaw eht ni elbmig dna eryg diD sevot yhtils eht dna ,gillirb sawT' ykcowrebbaJ

Exercise 3

Create a copy of your solution for Exercise 2 and name it Lab12c.java in the same ClosedLab12 folder. For this exercise, you should extend the code that you wrote in Exercise 2. Now instead of printing the output to the console, the program should output the reversed file to a new file. Your program should prompt the user to enter separate file names for input and for output, should display an error if the user tries to use the same filename for both input and output, and should handle the cases where the program cannot write to the output file.

Exercise 3 Sample Output

This is a sample transcript of what your program should do. In addition to the actual interaction at the console, you should check to make sure that your output file gets created and has the proper contents. You can do this by selecting File -> Open File from your Eclipse window and then browsing to find the appropriate filename. Enter an input file name: lab12aInput.txt Enter an output file name: lab12aOutput.txt If you provide the same name for an input file as for an output file, the program should display an error message Enter an input file name: lab12aInput.txt Enter an output file name: lab12aInput.txt ERROR! Your input file and output file MUST be different. If you provide a file name that is unwriteable (such as attempting to write the file to a drive that does not exist on your system), you should receive an appropriate error message: Enter an input file name: lab12aInput.txt Enter an output file name: q:\lab12aOutput.txt Error writing to file q:\lab12aOutput.txt

Skeleton:

/* * Lab12a.java * * A program that prompts the user for an input file name and, if that file exists, * displays each line of that file in reverse order. * Used to practice simple File I/O and breaking code up into methods as well as a first * step to implementing Lab13b.java - reversing the entire file and Lab13c.java writing * output to a separate output file. * * @author ENTER YOUR NAMES HERE * */ package osu.cse1223; import java.io.*; import java.util.*; public class Lab12a { public static void main(String[] args) { //Fill in the body } // Given a Scanner as input prompts the user to enter a file name. If given an // empty line, respond with an error message until the user enters a non-empty line. // Return the string to the calling program. Note that this method should NOT try // to determine whether the file name is an actual file - it should just get a // valid string from the user. private static String getFileName(Scanner inScanner) { } // Given a String as input return the reverse of that String to the calling program. private static String reverse(String inString) { } }

File:

Jabberwocky 'Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. "Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!" He took his vorpal sword in hand: Long time the manxome foe he sought So rested he by the Tumtum tree, And stood awhile in thought. And as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came! One, two! One, two! and through and through The vorpal blade went snicker-snack! He left it dead, and with its head He went galumphing back. "And hast thou slain the Jabberwock? Come to my arms, my beamish boy! O frabjous day! Callooh! Callay!" He chortled in his joy. 'Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. -- from Through the Looking-Glass, and What Alice Found There (1872). 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

IBM Db2 11 1 Certification Guide Explore Techniques To Master Database Programming And Administration Tasks In IBM Db2

Authors: Mohankumar Saraswatipura ,Robert Collins

1st Edition

1788626915, 978-1788626910

More Books

Students also viewed these Databases questions

Question

List two things that can make a line graph misleading.

Answered: 1 week ago

Question

Use a three-step process to develop effective business messages.

Answered: 1 week ago