MyprogrammingLab 12.10 - 12.11

24 July 2022
4.7 (114 reviews)
7 test answers

Unlock all answers in this set

Unlock answers (3)
question
Declare a variable logfileName that is suitable for holding the name of a file.
answer
String logfileName;
Explanation: A variable logfileName would be suitable for holding the name of a file because it is a string variable that can store characters.
question
Suppose a reference variable of type File called myFile has already been declared . Create an object of type File with the initial file name input.dat and assign it to the reference variable myFile.
answer
myFile = new File ("input.dat");
Explanation: ;The File class is a class in Java that represents a file or directory on a computer's file system. The File class has a constructor that takes a String representing the file name or directory name and creates a File object. The File class also has a method called getName() that returns the name of the file or directory as a String. In this example, the file name is input.dat and the directory name is the current directory."
question
Declare a local variable output that is suitable for referring to an object that provides methods for writing to a text file.
answer
PrintWriter output = new PrintWriter("textfile.txt");
Explanation: ;A local variable output is a variable that is only accessible within the local scope. This means that it can only be used within the block of code in which it is declared. It is not accessible from outside the block.output is a suitable variable for referring to an object that provides methods for writing to a text file because it can only be used within the local scope. This means that it will not conflict with any other variables that may be used in the same file."
question
Write an expression whose value is a reference to a newly created PrintWriter object associated with a file named "output .txt". (Do not concern yourself with any possible exceptions here- assume they are handled elsewhere.)
answer
new PrintWriter("output.txt")
question
Given an initialized String variable output, write an expression whose value is a reference to a newly created PrintWriter object associated with a file whose name is contained in output. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)
answer
new PrintWriter(output)
Explanation: Given an initialized String variable output, write an expression whose value is a reference to a newly created PrintWriter object associated with a file whose name is contained in output.PrintWriter writer = new PrintWriter(output);
question
Write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file named "output .txt". (Do not concern yourself with any possible exceptions here- assume they are handled elsewhere.)
answer
PrintWriter output = new PrintWriter("output.txt");
question
Given an initialized String variable outfile, write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file whose name is given by outfile. (Do not concern yourself with any possible exceptions here- assume they are handled elsewhere.)
answer
PrintWriter output=new PrintWriter(outfile);
Explanation: Assuming the variable outfile has already been initialized, the following code declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file whose name is given by outfile.PrintWriter output = new PrintWriter(outfile);