Readers Writers   «Prev  Next»


Lesson 3Course project
ObjectiveExamine what the course project entails and how you will proceed.

Java Choose File Course Project

In this course you are going to create a program that allows users to choose a file and then choose different text encodings in which the file can be read and displayed. This FileViewer program will use a GUI file dialog box so users can choose the file and, if the file contains text, specify the encoding desired. You will not start working on the course project until you have gained some preliminary skills. In the first few modules, you will have a chance to write smaller programs and submit them.
This program builds on the course project developed for the previous course in DistributedNetworks's Networking with Java series, Java Streams. The Java Streams course project was a character-mode program that dumped a specified file in ASCII, hex, decimal, floating point numbers, double-precision floating point numbers, shorts, ints, or longs.
If you have not taken Java Streams, you can download the character-mode FileDumper program from the compressed download file available on the course Resources page.

The following quiz question tests your knowledge of the Java Console.
1.import java.io.*; 
6.public class Pass { 
7.  public static void main(String[] args) {
8.    Console c = new Console();
9.    String pw;
10.   System.out.print("password: ");
11.   pw = c.readLine();
12.   System.out.println("got " + pw);
13. }
14.}

If the user types the password x1y2z3 when prompted, what is the result? [ Please select 1 option: ]
  1. password: got
  2. password: got x1y2z3
  3. password: x1y2z3 got x1y2z3
  4. An exception is thrown at runtime.
  5. Compilation fails due to an error on line 8.


Answer: e
Explanation:
The given code fragment uses the Console class to prompt the user for a password and then prints the accepted password. The Console class provides methods to access the character-based console device associated with the current Java virtual machine.
By using the Console class you can
  1. access the console device and
  2. accept input or display output on the console.
If the virtual machine has a console, then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available, then an invocation of that method will return null. In order to access an instance of the Console class you should use the following syntax
Console c = System.console(); 
The given example uses the following syntax instead :
Console c = new Console();
which is not a valid syntax to access an instance of the Console class. Therefore, it will cause a compile time error as described in option E. Because the given code generates a compile time error, it will not produce any output or exception.

Run CourseProject Application - Exercise

Click the Exercise link below to take a look at the completed course project application.
Run Course Project Application - Exercise