Program 7 - Text Files

Goal Description Hints Passoff

Due Date - 13 Apr

Goal

 
Learn to do file reading and writing
 CS 142
Description
 

Step 1

Write a method that accepts a file name (String), an object name (String) and an Obj3D. If the Obj3D is a Sphere, Box, Cone or Cylinder write out the commands from project 6 that will generate this object. For example

Obj3D aBox=new BoxObj();
aBox.setColor(Color.RED);
aBox.setScale(1,3,2);
aBox.setLocation(2,1,1);
aBox.rotX(90);
writeObj("myObjFile.txt", "redBox",aBox);     

Might write the following to the file "myObjFile.txt"

box redBox
move redBox 2 1 1
scale redBox 1 3 2
rotx redBox 90
roty redBox 0
rotz redBox 0

You only need to account for the color, scale, location and rotation angles of the object. Any object other than a sphere, box, cone or cylinder should throw an exception and not write out anything.

Step 2

Add the command file to the command line code from project 6. This command will ask the user for a file name and will read commands from the file instead of from System.in. After reading all of the commands from the file and executing them as you did in project 6, you should again begin reading commands from the user through System.in. See the hints for how to ask the user for a file name. This should only read from .txt files. You can create .txt files either using the method from step 1 or using NotePad (in the Accessories folder for Starting programs).

Step 3

Add the command write objectName to the command line code from project 6. This will pop up a file dialog and ask the user where they want to write to. The named object will then be written out to the specified file (see helpful code below).

Hints
 
  • Use instanceof to test an object to see if it is a SphereObj, ConeObj, CylinderObj or BoxObj.
  • You can copy and paste the following code into your program. This code will help in implementing the file command. It will ask the user for a file name. If the user selects any file that is not a .txt file or if the user decides not to select a file, this will throw an exception
  • 	private static Scanner askUserForFileScanner() throws Exception
    	{
    		JFileChooser fc=new JFileChooser();
    		int rslt=fc.showOpenDialog(null);
    		if (rslt==JFileChooser.APPROVE_OPTION)
    		{	File f=fc.getSelectedFile();
    			if (f.getName().endsWith(".txt"))
    			{
    				try 
    				{
    					return new Scanner(f);
    				}
    				catch (Exception e) { return null; }
    			}
    			else
    				throw new Exception("file \""+f+"\" is not a .txt file");
    		}
    		else
    			throw new Exception("user did not select a file");
    	}
    
    
  • You can copy and paste the following code into your program. This code will help in implementing the write command. It will ask the user for a file name. If the user specifies any file that is not a .txt file or if the user decides not to select a file, this will throw an exception
  • 	private static String askUserForFileName() throws Exception
    	{
    		JFileChooser fc=new JFileChooser();
    		int rslt=fc.showSaveDialog(null);
    		if (rslt==JFileChooser.APPROVE_OPTION)
    		{	File f=fc.getSelectedFile();
    			if (f.getName().endsWith(".txt"))
    			{
    				return f.getCanonicalPath();
    			}
    			else
    				throw new Exception("file \""+f+"\" is not a .txt file");
    		}
    		else
    			throw new Exception("user did not select a file");
    	}
    
  • Use your code from Program 6 to do most of this. If you built your code correctly the file command simply passes a different scanner object to your method that parses all of the commands.
Passoff
 

__ 2) Program correctly writes object descriptions to a file

__ 2) Program correctly opens a file, reads object descriptions and executes them.