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

Lab5 Exercise: Write a Java application that reads graphical instructions from a

ID: 3599842 • Letter: L

Question


Lab5 Exercise: Write a Java application that reads graphical instructions from a file called drawing.txt and executes them Detailed Requirement The file drawing.txt may contain any number of instructions; it might even be empty, in which case an appropriate message should be displayed Each line in drawing.txt represents 1 graphical instruction. A graphical instruction has the following format: instruction (parameter1, parameter2, etc.) Your application should first parse the file drawing.txt line by line and indicate all lines that contain invalid instructions using the following message: "Instruction in line N is not valid ". Once the whole file is parsed, if at least 1 invalid instruction is detected, the program stops with a message "Program stops: Syntax errors detected!!"; otherwise, the instructions will be executed. The list of valid instructions is as follow: move ( x, y): translate the current position (xc, yc) by (x, y) (i.e. xc=xc+x; yczyc+y). setColor (r, g, b): sets the current drawing color to the rgb color (r,g,b); r, g and b should be in the range [0, 255] setBackground(r, g, b): sets the current background color to the rgb color (r,g,b); r, g and b should be in the range [0, 255] Fill(status); status-true/false: if status true the 2D object will be solid otherwise just line drawing. line(x1, y1, x2, y2): draw a line from (x1+xc, y1+yc) to (x2+xc, y2+yc rect(xl,y1, w, h): draws a rectangle whose top left corner is (x1+xc, y1+yc) and width and height are (w, h). The rectangle will be solid only if fill status is true. oval(x1,y1, x2, y2): draws an oval whose top left corner is (x1+xc, y1+yc) and width and height are (w, h). The oval will be solid only if fill status is true

Explanation / Answer

package com;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class ReadFileExample1 {

private static final String FILENAME = "D:\test.txt";

public static void main(String[] args) {

BufferedReader br = null;

FileReader fr = null;

try {

//br = new BufferedReader(new FileReader(FILENAME));

fr = new FileReader(FILENAME);

br = new BufferedReader(fr);

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null) {

System.out.println(sCurrentLine);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)

br.close();

if (fr != null)

fr.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}