File Reading | My Assignment Tutor

T121: ICT102 Introduction to ProgrammingTutorial 10 Topic:Submission:Description:Files I/OBy end of week. This tutorial will introduce you to File I/O.Exercise 1: File ReadingThis given code asks user for name of the file: Testfile.txt (uploaded on Moodle withtutorial). The program display content of the file with line number and a colon “:” beforeactual content. Your job is to replace question marks with required syntax. Note: save textfile in your project folder otherwise, provide the complete path to the file.import java.util.Scanner;import*;/**This program demonstrates a solution to theLine Numbers programming challenge.*/public class LineNumbers{public static void main(String[] args) throws IOException{String filename; // The name of the file String input;int lineCount;// To hold file input// To hold line numbers // Create a Scanner object for keyboard input.Scanner keyboard = ??????// Get the file name.System.out.print(“Enter the file name: “);filename = ?????????// Open the file.File file = new File(filename);Scanner inFile = ?????// Initialize the line counter to 1.lineCount = 1;// Display the lines with line numbers.while (inFile.hasNext()){input = inFile. ??????System.out.println(lineCount + “: ” + input);lineCount++;}// Close the file.inFile.???????}}Exercise 2: Distance TravelledWrite a program that asks users how much distance can a rocket cover per day then ask howmany total days is the trip. It should then use a loop to display the total number of Kmstraveled at the end of each day. For example, if a user inputs 1000 Kms and 3 days, it shoulddisplay a report similar to the one that follows:Day Total Kms coveredDay 1 1000Day 2 2000Day 3 3000Input Validation: Do not accept a negative number for distance and do not accept anyvalue less than 1 for time travelled.Submission Task: Distance Travelled- File OutputModify the program you wrote in Exercise 2 so that it writes the report to a file instead ofthe screen. Open the file in Notepad or another text editor to confirm the output.


Leave a Reply

Your email address will not be published. Required fields are marked *