Question
NEED HELP WITH CODE!!! package Programming; import java.awt.Color; import java.util.ArrayList; /* PLEASE DO NOT MODIFY A SINGLE STATEMENT IN THE TEXT BELOW. READ THE FOLLOWING
NEED HELP WITH CODE!!!
package Programming; import java.awt.Color; import java.util.ArrayList;
/* PLEASE DO NOT MODIFY A SINGLE STATEMENT IN THE TEXT BELOW. READ THE FOLLOWING CAREFULLY AND FILL IN THE GAPS*/
public class PE1 { public static void main(String[] args) { /* A canvas should be created for drawing. * All the shapes should be drawn on the canvas. * The bottom left coordinate of the canvas is (0,0) * The top right coordinates of the canvas is (1, 1) * The input parameter to Draw() constructor is the * title of the canvas. */ Draw blankCanvas = new Draw("Programming"); /* To draw a point, point function is called with proper parameters: * point(x_coordinate_of_point, y_coordinate_of_point) */ blankCanvas.point(0.7, 0.7); /* To draw a circle, circle function is called with proper parameters: * circle(x_coordinate_of_center, y_coordinate_of_center, radius) */ blankCanvas.circle(0.5, 0.5, 0.5);
/* To draw a square, square function is called with proper parameters: * square(x_coordinate_of_center, y_coordinate_center, sides_half_length) */ blankCanvas.square(0.5, 0.5, 0.4); /* * To change the color of the pen, setPenColor is used with three numbers that are in [0, 255] range. * All the colors can be made as a combination of red, green and blue. * The parameters show the richness of red, green and blue of a color. * For example setPenColor(new Color(0, 0, 0) sets the color of the pen * to black and setPenColor(new Color(255, 255, 255) sets the color to * white. */ blankCanvas.setPenColor(new Color(150, 150, 150)); /* To draw a line, line function is called * with proper parameters: * line(x_coordinate_of_center, y_coordinate_center, sides_half_length) */ blankCanvas.line(0.0, 0.5, 1, 0.5); }
1. You are required to implement the method called nestedCircle() recursively, which gets 6 input parameters and return a string. This method keeps drawing circles with the same center and different radius until the radius gets negative. The output of the method is a list of all the circles radius from the most outer to the most inner one. Sample Call: PE1.nestedCircle (0.5, 0.5, 0.4, 0.05, blankCanvas, "") Corresponding Output: [0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.0] /** * This method draws a number of circles that share the same center, as long as the radius is positive. * @param x is the x-coordinate of the circles * @param y is the y-coordinate of the circles. * @param radius is the radius of a circle. * The function is called with the radius that is cut to two decimal points. * For example 0.39876543210 must be cut to 0.39 * @param diff is the difference between the radius of a circle and its immediate inner circle. * @param page is the canvas on which the circles are drawn. * @param radiusList is an accumulated list of the radius of the circles that were drawn. * @return a list of all the circles' radius that were drawn. */ public static String nestedCircle (double x, double y, double radius, double diff, Draw page, String radiusList) { // your code goes here. return ""; }
2. You are required to implement a recursive method that draws a set of squares where the center of one square falls on one corner of another square. The number of squares that should be drawn is given as an input parameter called order. If the order is one, then only one square is drawn. If the order is two, then 5 squares will be drawn when on each corner of the central square another square is drawn, whose side length is half the central squares side length. Please see the picture below that shows the drawing for order = 1 to 4. The output of this method is a list of the coordinates of the smallest squares that are drawn. /** * This method recursively draws 4 squares, whose center falls on a corner of * previously drawn square. The side of the square is half as much as the side of the * square that is drawn in previous round. * @param x is the x-coordinate of the square * @param y is the y-coordinate of the square * @param halfLength is half the size of the square's side * @param order is the number of the rounds by which a set of squares is drawn * @param page is the canvas on which the squares are drawn. * @return a list of the center of smallest squares that are drawn.. * The coordinates should be cut to one decimal point. For example: * 0.39876543210 is cut to 0.3 */ public static String squares (double x, double y, double halfLength, int order, Draw page) { // your code goes here. return ""; }
3. You are going to have nested triangles. The robot wants to drill a hole on the vertices of triangles and draw a line for the horizontal side of the triangles. This method starts with three points, which are the vertices of the largest (the most outer) triangle. A nested triangle is a triangle, whose vertices fall on the midpoints of the sides of the outer triangle. The method gets an integer called order, that specifies how deeply the recursive method should be called. If order = 1, only one horizontal line and three points are drawn, and three points are returned as the coordinates that should be drilled by the robot. Please see the pictures below where nested triangles with different orders are created by the robot. To make it more visible, the points that should be drilled and returned from the method are drawn in black. You dont need to change the color as we only check the return value of the method. Since this method is recursive, it is possible that a drilled point (i.e. a triangle vertex) is computed more than once. The ArrayList that holds the points should not contain any duplicate points. Also, for the ease of testing, we ask you to add point.toString()to the ArrayList. This method can be found in Point class.
Sample Call 1: Point p1 = new Point(0.1, 0.1); Point p2 = new Point(0.9, 0.1); Point p3 = new Point(0.5, 0.9); ArrayList
/** * This method specifies which coordinates should be drilled. It also draw the * horizontal line of each triangle. No duplicate point should be added to the output. * @param p1 is one of the vertex of the triangle * @param p2 is the second vertex of the triangle * @param p3 is the third vertex of the triangle * @param order is the number of times a nested triangle should be drawn. * order >= 0 , however if it is zero, nothing should be drawn * @param page is the canvas on which this method draws. * @param array is the list of the points that should be drilled. To add to this list point.toString() must be added. * @return an array that contains all the points that should be drilled. this method should not have any duplicate points in it. */ public static ArrayList drillPoints(Point p1, Point p2, Point p3, int order, Draw page, ArrayList array) { // your code goes here. return new ArrayList(); }
} /** * This class creates a point. * */ class Point { double x; double y; /** * This is the constructor that builds a point * @param x is the x-coordinate of the point * @param y is the y-coordinate of the point */ public Point(double x, double y) { // your code goes here. Task 0 this.x = x; //initialize x this.y = y; //initialize y } /** * This method returns the mid point of a line, * whose two ends are given. * @param p1 is one end of the line * @param p2 is the other end of the line * @return the mid point of the line. Both the * coordinates are cut to two decimal points. * e.g. 0.37654 is cut to 0.37 */ public static Point midpoint(Point p1, Point p2) { // your code goes here. return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); } @Override /** * This method returns the coordinate of this object as a string. */ public String toString() { return "["+this.x + ", "+ this.y +"]"; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started