Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming using Netbean: Inheritance, polymorp, and abstract base classes

ID: 3569595 • Letter: J

Question

Java Programming using Netbean: Inheritance, polymorp, and abstract base classes.

Implement Java console application that reads drawing commands from a text file and writes Scable Vector Graphics to standard output. The application uses these drawing commands to build a collection of concrete classes: Line, Rectangle, and Circle. These concrete classes derive from an abstract base class, Shape. This collection of shape is maintatined as a private field of an svg class.

UML

-shapes: LIst <Shape>

-height : double

-width: double

+render (out: Print Stream): void

+addShape (shape: Shape) : void

<<abstract>>

shape

+render (out: PrintStream): void

+addStyle (key: String, value: String) :void

+ render Attributes (out: PrintStream) : void

-x1: double

-y1: double

-x2: double

-y2:double

-x: double

-y:double

-width: double

-height:doube

-cx:double

-cy: double

-r: double

The Svg class maintating the collections of shapes and two fields of type double representing the width and height of the graphic. The class provides a method, addShape, that receives shape objects and adds them to its private collection. The Svg class also provides a second method, render. the writes SVG elements to a specified output stream.

class Svg {

private ArrayList <Shape> shapes;

private double width;

private double height;

:

void addShape (Shape shape) {...}

void render (PrintStream out) {...}

:

The Shape class maintains an array of styles, which are stored as collection of String types obtained from reading two tokens, a Key and a Value, and joining them with the ':' character. The class also defines an abstract method, renderAttributes, that derived classes use to write XML attributes to an output stream.

abstract class Shape {

private ArrayList <String> styles;

:

The Line class contains fields of type double representing two points in Cartesian Space:

class Line extends Shape {

private double x1;

private double y1;

private double x2;

prvate double y2;

:

The rectangle class contains fields of type of double representing a lcoation and size in Cartesian Space:

class Rectangle extends Shape {

private double x;

private double y;

private double width;

priavte double height;

The Circle class contains fields of type double representing a center coordiante and radius in Cartesian Space:

class Circle extends Shape {

private double cx;

private double cy

private double r;

:

When all commands have been read from text file, the application writes the Standard Output a series of SVG elements based on the commands from the input file. The implementation writes the SVG elements by passing system.out to the render mehtod of an Svg instance.

The application determines the name of the text file from the comman-line argumetns. If the user supplies no arguments, or the user supplies two or more argumetns, the application terminates after writing the Standard Error, "Invalid command-line arguments.".

$java MarkDavis

Invalid command-line arguments.

$java MarkDavis file1.txt file2.txt file3.txt

Invalid command-line arguments.

If any I/O error occurs while reading the input file, the applciation terminates after writing to Standard Error, "Failed to read input file: XXX". wher XXX is the value returned by Exception.getMessage().

$java MarkDavis

Failed to read input file. (Access is denied)

$java MarkDavis missing.txt

Failed to read input file: missing.txt (The system cannot find the file specified)

The applciatio ignore case for all comparisons of tokens read from the input file.

Parsing Errors

If a Parsing Error, such as NumberFormatException, occurs while reading the input file, the applciation terminates after writing to Standard Error "Invalid toke: XXX." where XXX is the unsupported or invalid token read from the file.

$cat bad-input.txt

SVG 320 200 RECT 10 10 20 bad-height

END

$java MarkDavis bad-input.txt

Invalid token: 'bad-height'/

SVG Header

IF the first token from the input file is not SVG, the applciation terminates with a Parsing Error.

$cat bad-input.txt

PNG 50 25

$java MarkDavis bad-input.txt

Invalid token: 'PNG'.

The application then reads two more tokens from the input file representing the width and then the height of the graphic, and if these two tokens cannot be parsed as double types, the applciation terminates with a Parsing Error.

$cat bad-input.txt

SVG six nin

$java YiZhu 4 bad-input.txt

Invalid token: 'six'

If the SVG token, the width, and the height are valid, the application creates a new instance of the Svg class and initiates it with the speicfied width and heigh. Then the application loops over additional toekens of the input file. For each interation. the application first reads one Command Token, and if ths Commands Toke is not LINE, RECT, or CIRCLE, the applciation terminates with a Parsing Error.

$cat bad-input.txt

SVG 320 200 POLYGON 5

$java MarkDavis bad-input.txt

Invalid token: 'POLYGON'

Line Shapes

If the Command Token is LINE, the application reads four tokens that represent x1, y1, x2, and y2 values that are used to create a new instance of the Line class; if any of these values fail to parse as double types, the application terminates with a Parsing Error.

$ cat bad-input.txt

SVG 320 200 LINE 10 20 bad-x2 60

$ java MarkDavis4 bad-input.txt

Invalid token: 'bad-x2'.

Rectangle Shapes

If the Command Token is RECT, the application then reads four tokens that represent x, y, width, and height values that are used to create a new instance of the Rectangle class; if any of these values fail to parse as double types, the application terminates with a Parsing Error.

$ cat bad-input.txt

SVG 320 200 RECT 10 20 bad-width 60

$ java MarkDavis4 bad-input.txt

Invalid token: 'bad-width'.

Circle Shapes

If the Command Token is CIRCLE, the application then reads three tokens that represent cx, cy, and r values that are used to create a new instance of the Circle class; if any of these values fail to parse as double types, the application terminates with a Parsing Error.

$ cat bad-input.txt

SVG 320 200 CIRCLE 10 20 bad-radius

$ java MarkDavis4 bad-input.txt

Invalid token: 'bad-radius'.

Styles

After successfully parsing a LINE, RECT, or CIRCLE command, the application enters a Style Loop that reads style information for the class derived from Shape. For each iteration, the application reads a first token, and if this token is END, the shape is added to the Svg instance, the application exits the Style Loop, and it then continues to process new commands from the input file.

If the first token is not END, the application interprets it as a Key, reads a second token and interprets it as a Value, and then passes this Key-Value pair to the Shape class to add as a style. The application continues to read and add styles in this fashion until the END token is encountered.

SVG Output

When all tokens have been read from the input file, the application renders the Svg instance to Standard Output. The Svg class data is formatted as follows.

<svg width='WIDTH' height='HEIGHT'> : (shapes) : </svg>

Between the opening and closing svg tags, the application renders each shape in the order they were defined in the input file. The format of each shape is specific to its type, but each contains a style attribute that is common to all shapes. Arrays of n styles are formatted as follows.

STYLE := Key1:Value1;Key2:Value2;...;Keyn:Valuen;

Line shapes are formatted as follows.

<line x1='X1' y1='Y1' x2='X2' y2='Y2' />

Rectangle shapes are formatted as follows.

<rect x='X' y='Y' width='WIDTH' height='HEIGHT' />

Circle shapes are formatted as follows.

<circle cx='CX' cy='CY' r='R' />

Examples

$ cat input.txt

svg 300 300

rect 5 5 290 290

fill #f8f8f8

end

line 5 5 295 295

stroke #804040

stroke-width 2

stroke-dasharray 5,10

end

line 5 295 295 5

stroke #804040

stroke-width 2

stroke-dasharray 5,10

end

rect 5 5 290 290

fill none

stroke #c0c0c0

stroke-width 2

end

circle 150 150 75

stroke #444

stroke-width 2

fill #844

fill-opacity 0.25

end

$ java MarkDavis4 input.txt

<svg width='300.0' height='300.0'>

<rect x='5.0' y='5.0' width='290.0' height='290.0'/>

<line x1='5.0' y1='5.0' x2='295.0' y2='295.0'/>

<line x1='5.0' y1='295.0' x2='295.0' y2='5.0'/>

<rect x='5.0' y='5.0' width='290.0' height='290.0'/>

<circle cx='150.0' cy='150.0' r='75.0'/>

</svg>

SVG

-shapes: LIst <Shape>

-height : double

-width: double

+render (out: Print Stream): void

+addShape (shape: Shape) : void

Explanation / Answer

contact on timacs12@gmail.com

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote