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

Need Help Coding This In Java - using arrayList For the next checkpoint, you wil

ID: 3606329 • Letter: N

Question

Need Help Coding This In Java - using arrayList

For the next checkpoint, you will write an application that will parse data from a text file that contains instructions for drawing some simple graphics. Each line of the file describes a polyline, that is, a sequence of points with line segments drawn between them. We'll encapsulate the data for one polyline in an object of type Polyline, and we'll draw everything using an application called Plotter. The code for these is provided for you in a jar file.

To start out, let's try an example to see how it works. First import the jar file polyline_plotter.jar into your project and add it to the build path (just as you would a specchecker). Then, try running the code TestPlotter.java The main method looks like this:

Drawing takes place on a window that is roughly 500 by 500 pixels. Points are specified using integer coordinates. The upper-left corner is (0, 0), where x-coordinates increase from left to right and y-coordinates increase from top to bottom.

To construct a Polyline object, we specify one of several possible colors, and optionally a line width in pixels. Then we use the addPoint method to add points to the polyline. The plotter will start at the first point given and draw a line segment between each successive pair of points. (The Point class comes from the package java.awt, see the import statements at the top of the TestPlotter.java file.)

An ArrayList is also useful whenever you are reading input because you don't have to know in advance how many items there will be.

Instead of hard-coding the color and point data for the polylines as in the TestPlotter example, we would like to be able to read the data from a text file. Suppose that each line of the file corresponds to one polyline, and consists of the following:

An optional integer line width

A color, which is a string such as "red" or "blue" (see the polyline javadoc for details on recognized colors)

A sequence of integers representing coordinates of the points, where each pair is one point, x followed by y (there is always an even number of integers following the color name)

A file may also contain blank lines, which should be ignored, and comments, which should be ignored. Any line starting with the pound sign character "#" is considered a comment.

For example, the text file describing the output from our test program could look like this (you can find it here too):

(Note: a simple way to tell whether a line is blank is to first use the String method trim(), which removes all leading and trailing whitespace, and then check whether the length is zero.)

Write a program that will read files of the above format and plot the polylines using the plotter. You can refer to the sample code QuizAverager as a model for the file-reading loop. Start with a helper method that parses just one line and returns the corresponding Polyline object. Then write a helper method that opens and reads the file, and returns an ArrayList of Polyline objects:

Then the main method can iterate over the list, and call plot() for each Polyline.

Then the main method can iterate over the list, and call plot() for each Polyline.

Then, try it on the file hello.txt. here:

Explanation / Answer

The Java Code is given below.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class ReadPolyline {

// Method to read the input file and return an
// ArrayList of polylines
private static ArrayList<Polyline> readFile(String filename)

throws FileNotFoundException

{

ArrayList<Polyline> polyline = new ArrayList<>();

Scanner reader = new Scanner(new File(filename)); // creating a Scanner object to read the input file

String line;

while(reader.hasNextLine()) // scanning the file till last line

{

line = reader.nextLine();

if(line.trim().length() != 0) // checking whether line is blank or not, if not then proceed

{

String[] tokens = line.split(" ");

int width = 0;

String color = tokens[1].trim();

// trying whether width of pixel is given or not

try

{

width = Integer.parseInt(tokens[0].trim());

}

catch(NumberFormatException ex)

{

}

Polyline pl;

int i = 1; // creating the for-loop variable

if(width != 0) // if width is given

{

pl = new Polyline(color, width); // then create a new Polyline with that width and color

i = 2; // assigning the for loop variable as 2 to start from 2nd index to store the points

}

else

pl = new Polyline(color); // else just create a Polyline with the color

for(; i < tokens.length; i++) // looping till number of points in a line

{

pl.addPoint(new Point(Integer.parseInt(tokens[i].trim()))); // adding each point to Polyline

}

polyline.add(pl); // adding the polyline to polyline arraylist

}

}

return polyline;

}

// main method

public static void main(String args[])

{

ArrayList<Polyline> polyline = null;

String filename = "hello.txt";

try {

polyline = readFile(filename);

}

catch (FileNotFoundException e)

{

System.out.println("Error is reading the file " + filename);

System.exit(-1);

}

// plotting the figures

for(int i = 0; i < polyline.size(); i++)

{

polyline.get(i).plot();

}

  

}

}

Java Code to read the file and to Plot

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class ReadPolyline {

// Method to read the input file and return an
// ArrayList of polylines
private static ArrayList<Polyline> readFile(String filename)

throws FileNotFoundException

{

ArrayList<Polyline> polyline = new ArrayList<>();

Scanner reader = new Scanner(new File(filename)); // creating a Scanner object to read the input file

String line;

while(reader.hasNextLine()) // scanning the file till last line

{

line = reader.nextLine();

if(line.trim().length() != 0) // checking whether line is blank or not, if not then proceed

{

String[] tokens = line.split(" ");

int width = 0;

String color = tokens[1].trim();

// trying whether width of pixel is given or not

try

{

width = Integer.parseInt(tokens[0].trim());

}

catch(NumberFormatException ex)

{

}

Polyline pl;

int i = 1; // creating the for-loop variable

if(width != 0) // if width is given

{

pl = new Polyline(color, width); // then create a new Polyline with that width and color

i = 2; // assigning the for loop variable as 2 to start from 2nd index to store the points

}

else

pl = new Polyline(color); // else just create a Polyline with the color

for(; i < tokens.length; i++) // looping till number of points in a line

{

pl.addPoint(new Point(Integer.parseInt(tokens[i].trim()))); // adding each point to Polyline

}

polyline.add(pl); // adding the polyline to polyline arraylist

}

}

return polyline;

}

// main method

public static void main(String args[])

{

ArrayList<Polyline> polyline = null;

String filename = "hello.txt";

try {

polyline = readFile(filename);

}

catch (FileNotFoundException e)

{

System.out.println("Error is reading the file " + filename);

System.exit(-1);

}

// plotting the figures

for(int i = 0; i < polyline.size(); i++)

{

polyline.get(i).plot();

}

  

}

}

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