Resource CST-135 Activity 4 Guide Contents Part 1: Reading from Text Files2 Section 1A – Simple Read Statements2 Section 1B – Create a method to read files9 Section 1C – Store a file in an array10...

1 answer below »
Complete Activity 4


Resource CST-135 Activity 4 Guide Contents Part 1: Reading from Text Files2 Section 1A – Simple Read Statements2 Section 1B – Create a method to read files9 Section 1C – Store a file in an array10 Part 2: Read and Write an Array of Object Properties to/from Files13 Section 2A – Basic File Write13 Section 2B- Create Methods to Write to Files15 Section 2C – Save an Array15 This activity has multiple parts. All parts must be completed prior to documentation submission. Your instructor will provide the materials necessary to demonstrate and explain the steps in each of these activities. Part 1: Reading from Text Files Goal This exercise will show you how to create a text file in a text editor, read the information into a Java program, and print the results to the screen. Execution Execute this assignment according to the following guidelines. Directions: Re-create the programs listed here. 1A - Simple Read Statements 1. Create a new project and place the following code inside of main() 2. If the file is located in a directory outside of the Java application, you can specify the file path. In this example, assume that in.txt is in the application project folder. 3. Add a new scanner object. Notice that there is a missing exception handler error when you type this code. 4. Fix this error by adding the "throws" statement on the main() definition line. This exception will be triggered if the file "in.txt " doesn't exist, and the program will crash with an error message. 5. Create a new text document inside the project folder. 6. Name the file "in" if your file extensions are hidden. If you have file extensions visible, then name the file "in.txt." File extensions are hidden or visible depending on the options you set in the operating system control panel. 7. Open the file and put some text inside. The example below used "one," "two," and "three" on separate lines. 8. Save the file and refresh the project files in Eclipse. Press F5 or right click on the project folder and choose refresh. You should see in.txt in the package explorer. 9. You should be able to open the in.txt file in Eclipse. 10. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 11. You could also create the new text file right from the Eclipse IDE. 12. Add some lines to the Untitled 1 file. This is test data so enter any strings you wish. 13. Save the file inside the project folder. 14. You should see two files in the project explorer. 15. Create three lines of code that will read from the file and print each line to the console. If your text file is shorter than three lines, the program will stop with an error. 16. Notice that the last line is printed but stops at the space character. 17. If you add another println statement, the last word "four " will be printed as well. 18. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 19. You can read the entire line by changing s.next() to s.nextLine() 20. You can also input text and assign it to variables such as integers. Notice that the example below uses the second text file named in1.txt. 21. Change the contents of in1.txt to different numbers. 22. Run the program again. Notice that the two variables, num1 and numb2 are of type int. If the text file contains nonnumeric data, the program will crash with an exception telling you that the data type for num1 is not a string. 23. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 1B - Create a method to read files 1. Create a new project and add a method called readString() as follows. 2. Create a string variable, append each new line to it, and return it. 3. Add some code in the main() method to print the results. 4. Run the program. You should see a single string for output. 5. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 6. Modify the function so that each word is separated by a space. The plus sign, when used with strings, is a concatenate operation. 1C - Store a file in an array 1. Continue using the previous program project. Add a new method and name it readArray() 2. Here is the strategy that will be attempted. 3. Add the code to the method 4. In the main() method, add code to invoke the newly coded method. 5. Be sure that the in.txt file has one word per line. 6. You should see the output as follows; 7. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 8. You can also print the array using the Arrays.toString() function. You will need to import Array. 9. The output is slightly different. 10. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. Deliverables This activity has multiple parts. All parts must be completed prior to documentation submission. See the instructions at the end of this activity. For this part of the activity you will need to include: 1. ZIP file containing the source code of the application. 2. Word document containing captioned screen shots that show the application being run successfully. Part 2: Read and Write an Array of Object Properties to/from Files Goal In this application, you will create a text file using a Java program. You will write an array of objects to a file with delimiter characters to separate items as well as read the same data back into an array of objects. Execution Execute this assignment according to the following guidelines. 2A - Basic File Write 1. Create a new project and outline the goals in comments. 2. Create some code to create a file, a file writer, and a print writer. Import necessary libraries. 3. Add an exception handler and some writeline statements. Be sure to include the close statement to create the file. 4. You should see the contents of the file. 5. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 6. Add a second parameter to the FileWriter constructor to change the write method to "append." The "true" parameter makes the file extendable. That is, it will not overwrite the contents of the file, but add the new material to the end of the file. 7. You should be able to see the file grow longer each time you run the program. 8. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. 2B - Create Methods to Write to Files The goal of this section is to create class methods that perform file writes. 1. Create a separate function farther down the page. Notice that this is the same code that was used earlier, but now it has been refactored into a method. 2. Delete or comment out the original code and create one line to call the new function. 2C - Save an Array In this section, you will save an array of objects to a text file. 1. Start with the following program, which has a method to do the file writing. 2. Create a class with some properties. You can make a similar class and properties or use the example below. The example includes some properties of type string, int, and float to show how to handle data formats that are not strings. 3. Now create an array of the object. Convert each item in the array to a string. Attach all of the strings together with a separator character such as "|" or ",". This method saves only one car at a time to the file, so the "append" flag is set to "true" each time a write action is performed. 4. Create a method to read each line, parse the results, and store them in an ArrayList. Return the entire list to the main program. 5. Take a screen shot of the application being run. Paste the image into a Word document. Write a caption beneath each screen capture to describe what is being demonstrated. Part 2 Deliverables This activity has multiple parts. All parts must be completed prior to documentation submission. See the instructions at the end of this activity. For this part of the activity you will need to include: 1. ZIP file containing the source code of the application. 2. Word document containing captioned screen shots that show the application being run successfully. Activity Deliverables This activity has multiple parts. All parts must be completed prior to documentation submission. A good way to organize the assignment is to create separate folders and ZIP the entire project. Inside each of these folders is usually another ZIP file that contains the project code and a Word document that contains screen shots of the application being demonstrated. 17 © 2021. Grand Canyon University. All Rights Reserved.
Answered 2 days AfterMay 25, 2021

Answer To: Resource CST-135 Activity 4 Guide Contents Part 1: Reading from Text Files2 Section 1A – Simple...

Pulkit answered on May 28 2021
149 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here