📄 question.txt
字号:
ex8e Drawing Part 3: Draw Files
===============================
Introduction
============
This is the second large exercise, which in total is
worth a large percentage of the course. However, this exercise has
been split into three parts of increasing difficulty, full marks can
only be obtained by completing all 3 parts and also submitting the
associated report.
This is part 3!
Problem specification
=====================
You are to extend your previous program into one that can read
its creation data from a file rather than through prompts.
-----------------------------------------------------------------
The specification for the files Line.java and Rectangle.java are
identical to the previous part, so if you managed to meet the
specification for those files in your previous program all you
need to do is copy and paste them for part 3.
------------------------------------------------------------------
The specification for DrawFiles.java should be the same as the
previous part (DrawMore.java), but with at least the following
changes and additions:
Instance Variables
------------------
Still none.
Public Methods
--------------
underlinedPrint( String value ) which is exactly the same as the
previous program specification and can be invoked from within either
of the other two classes provided it is declared "public" and "static".
Invoke it via DrawMore.underlinedPrint( "Test" )
Private Methods
---------------
promptAndReadValue( String value ) which is exactly the same as the
previous program specification
promptAndReadFileName( String value ) which is similar to the
promptAndReadValue( ... ) method, but the prompt is different and it
returns a string
runMainMenu() which is exactly the same as the previous program
specification, but with the addition of a "Create from file" option
selected with 'f' or 'F'. Insert this just before the quit option.
runOptionsMenu( String name ) which is exactly the same as the previous
program specification, but with the addition of an option 3 "Draw X to
file" where X is the named line or rectangle. (Move the previous option
3 to option 4)
createLine( boolean isHorizontal ) which is similar to the first part of
the previous sepcification
processLine( Line myLine ) which is similar to the second part of the
previous createLine( ... ) specification
createRectangle( boolean isSquare ) which is similar to the first part of
the previous sepcification
processRectangle( Rectangle myRectangle ) which is similar to the second
part of the previous createRectangle( ... ) specification
createFromFile() which returns nothing, but prompts for a file name and
reads data from the named file to create lines or rectangles. This method
only extracts the characters from the file, it does not try to make any
sense of them!
parseFile( String content ) which returns nothing, but breaks the given
content string into lines (defined by '\n' or '\r' characters)
parseLine( String line ) which returns nothing, but breaks up the given
line string into meaningful data and calls the relevant processLine( ... )
or processRectangle( ... ) method with the correct arguments
drawToScreen( String input ) which is exactly the same as the previous
program specification
drawToFile( String input ) which is similar to the drawToScreen( ... )
method, but it prompts for a file name and draws the input string to the
named file instead
-----------------------------------------------------------------------
Notes
=====
After processing a line of data from the file, the program should show
the options menu for that object in the same way as if it were created
via the main menu and prompts (see Typical input and output).
When selecting to return to the main menu from the options menu, the
program should only return to the main menu if there were no more data
lines to process from the data file. If there were other data lines,
then the program should process these in turn, by showing the relevant
options menu for that object.
Constants should be used **everywhere possible** and should be declared
as "static" and "final".
Test files (file2.txt to file8.txt) have been created in your working directory, if you alter these files
remember to revert to the original versions otherwise you WILL fail some
of the dynamic tests!
Hints
=====
Look up the following classes in the Java API
(http://java.sun.com/j2se/1.5.0/docs/api/):
File
FileReader
FileWriter
FileNotFoundException
IOException
StringBuffer
StringTokenizer
NoSuchElementException
Error conditions
================
Include all the previous error conditions from part 2.
When opening the file named "myFile.txt", print the following if that
file could not be found:
ERROR: Could not find file named "myFile.txt"
If any other type of error occurs when opening the file name "myFile.txt"
for reading print:
ERROR: There was a problem reading from the file "myFile.txt"
If any type of error occurs when opening the file name "myFile.txt" for
writing print:
ERROR: There was a problem writing to the file "myFile.txt"
If a data file contains a negative value for a length, width or height
print the following and then ignore the rest of that data line:
ERROR: Negative number found when parsing file
If a data file contains a type value that is not a line or rectangle
(e.g. not 'h', 'H', 'v', 'V', 's', 'S', 'r' or 'R') print the following
and then ignore the rest of that data line:
ERROR: Unknown type found when parsing file
If a data file contains a line which is not valid for another reason
(e.g. line with no length given) print the following and then ignore the
rest of that data line:
ERROR: Type structure could not be parsed
Typical input/output
====================
Given the following data file "test.txt":
H,-1
J,3
s,
v,5
R,4,6
The program output would be:
Main Menu
=========
H) Create Horizontal Line
V) Create Vertical Line
S) Create Square
R) Create Rectangle
F) Create from file
Q) Quit
Enter choice: f
Enter the input file name: test.txt
ERROR: Negative number found when parsing file
ERROR: Unknown type found when parsing file
ERROR: Type structure could not be parsed
Options Menu
============
1) Print Vertical Line's properties
2) Draw Vertical Line to screen
3) Draw Vertical Line to file
4) Return to main menu
Enter choice: 1
Vertical Line Properties
========================
Orientation is Vertical Line
length is 5
Options Menu
============
1) Print Vertical Line's properties
2) Draw Vertical Line to screen
3) Draw Vertical Line to file
4) Return to main menu
Enter choice: 2
+
|
|
|
+
Options Menu
============
1) Print Vertical Line's properties
2) Draw Vertical Line to screen
3) Draw Vertical Line to file
4) Return to main menu
Enter choice: 4
Options Menu
============
1) Print Rectangle's properties
2) Draw Rectangle to screen
3) Draw Rectangle to file
4) Return to main menu
Enter choice: 1
Rectangle Properties
====================
width is 4
height is 6
Options Menu
============
1) Print Rectangle's properties
2) Draw Rectangle to screen
3) Draw Rectangle to file
4) Return to main menu
Enter choice: 2
+--+
| |
| |
| |
| |
+--+
Options Menu
============
1) Print Rectangle's properties
2) Draw Rectangle to screen
3) Draw Rectangle to file
4) Return to main menu
Enter choice: 3
Enter the output file name: output.txt
Options Menu
============
1) Print Rectangle's properties
2) Draw Rectangle to screen
3) Draw Rectangle to file
4) Return to main menu
Enter choice: 4
Main Menu
=========
H) Create Horizontal Line
V) Create Vertical Line
S) Create Square
R) Create Rectangle
F) Create from file
Q) Quit
Enter choice: q
The contents of "output.txt" would be:
+--+
| |
| |
| |
| |
+--+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -