I have an array of type Point, and it is already sorted. I need to create a meth
ID: 3870402 • Letter: I
Question
I have an array of type Point, and it is already sorted.
I need to create a method that does the following:
Furthermore, the second scenario
Do the method in java and post the code here.
Name the output file sortedPoints.txt.
The method writePointsToFile ) in the AbstractSorter class writes the sorted points (in order of increasing index) from the array points to a file with provided name, under certain format that is decided by the sorting order indicated by the value of the instance variable sortByAngle in AbstractSorter. In the output file, adjacent coordinates must have at least one blank space in between. The format is detailed below (a) If sortByAngle false, every line displays the r-and y-coordinates of a single point. This format will allow Mathematica to generate a monotonic polylineExplanation / Answer
import java.io.*;
import java.util.*;
//Abstract class AbstractSorter definition
abstract class AbstractSorter
{
//To store boolean value true or false
boolean sortByAngle;
//To store points
String points[];
//Method to write points to specified file
abstract void writePointsToFile();
}//End of class
//Class AbstractSorterDemo derived from AbstractSorter class
public class AbstractSorterDemo extends AbstractSorter
{
//Creates points and assigns value to it
String points1[] = {"-10 0", "-7 -10", "-7 - 10", "-6 3", "-3 -9", "-2 1", "-1 -6", "0 -10", "0 0", "0 8", "3 3", "5 -2", "5 5", "7 3", "8 4"};
String points2[] = {"-7 -10", "-7 -10", "0 - 10", "-3 -9", "-1 -6", "5 -2", "10 5", "10 5", "7 3", "8 4", "5 5", "3 3", "0 0", "-2 1", "0 8", "-6 3", "-10 0"};
//PrintWriter object initialized to null
PrintWriter pw = null;
//File class object declared
File file;
//Overrides method to write onto the file
void writePointsToFile()
{
//Checks if sortByAngle value is false
if(sortByAngle == false)
{
//Assigns points1 data to points of abstract class instance variable
points = points1;
//File class object created to open sortedPoints.txt file for writing
file = new File ("sortedPoints.txt");
//Try block begins
try
{
//PrintWriter object created to write onto the file name specified with file object
pw = new PrintWriter (file);
//Writes heading to the file
pw.printf("Sort by angle is false");
//Loops till end of points
for(int counter = 0; counter < points.length; counter++)
{
//For new line
pw.println();
//Writes data to file
pw.printf("%s", points[counter]);
}//End of for loop
}//End of try block
//Catch to handle file not found exception
catch (FileNotFoundException e)
{
e.printStackTrace();
}//End of catch
//Close printWriter
pw.close();
}//End of if
//Otherwise sortByAngle value is true
else
{
//Assigns points2 data to points of abstract class instance variable
points = points2;
//File class object created to open sortedPoints.txt file for writing
file = new File ("sortedPoints.txt");
//Try block begins
try
{
//PrintWriter object created to write onto the file name specified with file object
pw = new PrintWriter (file);
//Writes heading to the file
pw.printf("Sort by angle is true");
//For new line
pw.println();
//Writes the zero position value to the file
pw.printf("%s", points[0]);
//Loops till end of points
for(int counter = 1; counter < points.length; counter++)
{
//For new line
pw.println();
//Writes data to file
pw.printf("%s %s %s", points[counter], points[0], points[counter]);
}//End of for loop
}//End of try block
//Catch to handle file not found exception
catch (FileNotFoundException e)
{
e.printStackTrace();
}//End of catch
//Close printWriter
pw.close();
}//End of else
}//End of class
//main method definition
public static void main(String[] args)
{
//To store user choice
int ch;
//Scanner class object created
Scanner sc = new Scanner(System.in);
//AbstractSorterDemo class object created
AbstractSorterDemo asd = new AbstractSorterDemo();
//Displays the menu
System.out.println("1: Sort by angle");
System.out.println("2: Do not sort by angle");
//Accepts user choice
System.out.println(" Enter your choice: ");
ch = sc.nextInt();
//Checks if user choice is one
if(ch == 1)
//Set the sortByAngle value to true
asd.sortByAngle = true;
//Checks if user choice is two
else if(ch == 2)
//Set the sortByAngle value to false
asd.sortByAngle = false;
//Otherwise
else
{
System.out.println(" Error: Wrong choice ");
System.exit(0);
}//End of else
//Calls the method to write data to file
asd.writePointsToFile();
}//End of main method
}//End of class AbstractSorterDemo
Sample Run 1:
1: Sort by angle
2: Do not sort by angle
Enter your choice: 3
Error: Wrong choice
Sample Run 2:
1: Sort by angle
2: Do not sort by angle
Enter your choice: 1
File sortedPoints.txt contents
Sort by angle is true
-7 -10
-7 -10 -7 -10 -7 -10
0 - 10 -7 -10 0 - 10
-3 -9 -7 -10 -3 -9
-1 -6 -7 -10 -1 -6
5 -2 -7 -10 5 -2
10 5 -7 -10 10 5
10 5 -7 -10 10 5
7 3 -7 -10 7 3
8 4 -7 -10 8 4
5 5 -7 -10 5 5
3 3 -7 -10 3 3
0 0 -7 -10 0 0
-2 1 -7 -10 -2 1
0 8 -7 -10 0 8
-6 3 -7 -10 -6 3
-10 0 -7 -10 -10 0
Sample Run 3:
1: Sort by angle
2: Do not sort by angle
Enter your choice: 23
File sortedPoints.txt contents
Sort by angle is false
-10 0
-7 -10
-7 - 10
-6 3
-3 -9
-2 1
-1 -6
0 -10
0 0
0 8
3 3
5 -2
5 5
7 3
8 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.