Reference no: EM132517919
CSE1IOO Intermediate Object-Oriented Programming - La Trobe University
Programming Assignment
Problem Description
In this assignment, you are to develop a basic text-based drawing application. A sample drawing is shown below.
It has
Borders which form a rectangle around a drawing area of 20 by 30 (drawn with the asterisk char- acter in this example)
• Two lines (the ground and the lamp post)
• One circle (the lamp)
• Two rectangles (the house walls and the door)
• One triangle (the roof)
• A string of text ("FOR SALE" displayed vertically)
Initially, your main Problem Task is to develop the classes whose instances are objects that make up a drawing. Essentially, they are classes that represent windows and shapes.
You will then develop a menu program that allows the user to progressively create a drawing (by adding shapes and removing shapes), save the definition of the drawing in a text file and read it back to recon- struct it.
Problem Task 1
a) Design and implement
• The class to represent a drawing window
• The abstract class Shape
• The class that represents a line
b) Test your classes with the EightLines program below. The program must be able to run with your classes without any change.
public class EightLines {
public static void main(String [] args){ Window window = new Window(20, 20, '*');
int row = 10, column = 10, length = 5;
Line line = new Line(row, column, length, 0, 1, '1'); window.addShape(line);
line = new Line(row, column, length, 1, 1, '2'); window.addShape(line);
line = new Line(row, column, length, 1, 0, '3'); window.addShape(line);
line = new Line(row, column, length, 1, -1, '4'); window.addShape(line);
line = new Line(row, column, length, 0, -1, '5'); window.addShape(line);
line = new Line(row, column, length, -1, -1, '6'); window.addShape(line);
line = new Line(row, column, length, -1, 0, '7'); window.addShape(line);
line = new Line(row, column, length, -1, 1, '8'); window.addShape(line);
window.display();
}
}
Problem Task 2
a) Design and implement the rest of the shapes
b) Test your classes with the program HouseForSale below. It should produce the drawing on page 2.
import java.util.*;
import java.io.*;
public class HouseForSale
{
public static void main(String [] args) throws Exception
{
{
// Create a window
Window w = new Window(20, 30, '*');
// Draw the ground
Line ground = new Line(19, 1, 29, 0, 1,'#'); w.addShape(ground);
// Draw the post
Line post = new Line(12, 5, 6, 1, 0, '#'); w.addShape(post);
// Draw the light
Circle light = new Circle(10, 5, 2, '+'); w.addShape(light);
// Draw the house
Rectangle house = new Rectangle(8, 16, 11, 10, '='); w.addShape(house);
// Draw the door
Rectangle door = new Rectangle(11, 19, 8, 4, '='); w.addShape(door);
// Draw the roof
Triangle roof = new Triangle(2, 21, 6, 1, 0, '*'); w.addShape(roof);
// Display text message
Text msg = new Text(2,10, "FOR SALE", 1, 0); w.addShape(msg);
w.display();
}
}
}
It is required that the classes you develop must allow the HouseForSale program to run without any changes.
Problem Task 3
(a) In the class Window, implement the following method
void writeSpecToFile(String fileName)
The method saves the specification of the drawing window (as opposed to the display image of the drawing) in the specified text file.
(b) Write a program, called T3Main.java, to generate a drawing (just like the one in the class House- ForSale), display it on the screen and save it to a text file called T3Drawing.txt.
The format of the output text file must conform to the specification illustrated by the example below (the example specification is for our House For Sale drawing), minus the comments.
20 30 // number of rows and columns of the drawing window
* // character for border
. // a dot to signal the end of the ‘‘record''
line // the shape is a line
19 1 29 0 1 // row & column positions of base, length, row increment, column increment # // display character
.
line
12 5 6 1 0 #
.
circle
10 5 2 // row position of base, column position of base Base, radius
+ // display character
.
rectangle
8 16 11 10 // row position of base, column position of base Base, height, width
= // display character
.
rectangle 11 19 8 4
=
.
triangle
2 21 6 1 0 // row & column positions of base, height, row increment, column increment
*
.
text
2 10 // row & column positions of base FOR SALE // the text itself
1 0 // row increment, column increment
.
Problem Task 4
(a) In the class Window, implement the following static method (class method)
Window readSpecFromFile(String fileName)
The method reads the text file, constructs and returns the Window object representing the drawing specified in the text file.
The input text file for this method has the same format as described in Problem Task 3.
(b) Write a program, call it T4Main.java, to read the description of a drawing from a text file, e.g.
T3Drawing.txt, and display the drawing on the screen.
Problem Task 5
Add to the class Window two methods, which will be used in Problem Task 6. The first method
public void addGrid()
adds numbers to the borders to indicate the row and column indexes of the cells, as shown in the example below. The numbers on the edges would help us manipulating the shapes.
The second method has the signature:
public void refreshImage()
This method first makes all the cells of the drawing image blank (except those on the borders). Then it asks each shape of the window to draw itself on the window.
This method would be called when we want to be sure that the image is up to date.
Problem Task 6
For this task, you implement a program called DrawingBoard.java. (You may also need to add some simple methods to the existing classes to support this program.)
• This program first allows the user to create a new drawing window or to load an existing one.
• It then allows the user
-To add a shape to the drawing,
-To delete a shape from the drawing,
-To select a shape and move it (up, down, left, right) or make it bigger or smaller,
-To save the specification of the current drawing window to a text file.
For simplicity, we will only be concerned with lines. To be clear, your menu program should be able to read and display drawing that have shapes other than line (as you will use what you'll develop in Problem Task 3 and 4). However, the program provides options to add, delete, move, and change sizes of lines only.
The interactions between the user and the program are described below.
1. Create a new drawing window or load an existing one
At the start, the program prompts the user to enter NEW in order to create a new drawing window or to enter a fie name to load an existing drawing window.
Here is a sample run to create a new drawing window (inputs by the user are displayed in bold):
> java DrawingBoard
Enter the window file name (or NEW):
NEW
Enter mumner of rows, number of columns and character (separated by space):
10 30 *
2. Menu options
The last two lines in the display above are the reminders of the menu options.
The first line is meant to remind the user of the following options: Add (to add a shape), Erase (to delete a shape), Select (to select a shape, the selected shape can be moved or have its size changed as will be seen shortly), Write (to write the specification of the window to a text file), and Quit.
The second line displays reminders for options that can be applied to the selected shape: Up (to move the shape up), Down (to move the shape down), Left (to move the shape left), Right (to move the shape right), + (to increment the size of the shape) and - (to decrement the size of the shape).
3. Adding shapes
The way this option works is summarized in the table below:
Reminder Add
Purpose To add a shape (a line)
To choose the option Enter a and press ENTER
System's response Display the format to enter a line
User's response Enter details for a line
System's response Add the shape to the drawing window, display the
window and the menu option reminders (ready for the next option)
4. Erasing (Deleting) shapes
Reminder Erase
Purpose To erase (delete) a shape (a line)
To choose the option Enter e and press ENTER
System's response Display the indexes and details of the shapes
User's response Enter the index of the shape to be erased (user can
only select a Line)
System's response Erase the shape from the drawing, display the draw-
ing and the menu option reminders (ready for the next option)
5. Selecting shapes
We can select a shape to move it or to change its size.
Reminder Select
Purpose To select a shape
To choose the option Enter s and press ENTER
System's response Display the indexes and details of the shapes
User's response Enter the index of the shape to be selected (user can
only select a Line)
System's response Mark the shape as being selected (you don't need to
show any information on screen about which shape was selected), display the drawing and the menu op- tion reminders
6. Moving selected shapes and changing their sizes
To move the previously selected shape up:
Reminder Up
Purpose To move the shape up one row (by moving the base
point of a line)
To choose the option Enter u and press ENTER
System's response Reduce the row base of the shape by 1, display the
drawing and the menu option reminders
7. Saving the specification of a drawing window to a file
Reminder Write
Purpose To save the specification of the window to a text file
To choose the option Enter w and press ENTER
System's response Prompt for the input file name
User's response Enter the file name
System's response Write the window's details to the file (overwrite),
display the drawing window and the menu option reminders
9. Making the program more robust
Once we start the program and take a few options, we do not want the program to crash. To prevent this, put the actions for each of the menu options in a try/catch block.
Problem Task 7
• Design and implement the class to represent an oval.
• Write program T7Tester.java to test your class.
• Submit both Oval.java and T7Tester.java.
Attachment:- Programming Assignment.rar