Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code level: beginner write a method Square A Square is defined by a Point and an int. The Point represents the coordinates of the

java code

level: beginner

write a method

Square

A Square is defined by a Point and an int. The Point represents the coordinates of the squares upper-left corner. The int represents the length of the sides. Since Square extends Shape, it will also implicitly inherit the name.

  1. Implement the public getters for the side length and upperLeft Point instance variables:
    • getSide(), getUpperLeft()
  2. Implement private setters for the upper left point and side length. These do not return anything and are shallow, meaning that you will simply point the corresponding instance variable to the object/value passed into the method. See Line8B and Point for an example.
    • setUpperLeft(Point upperLeft), setSide(int side)
  3. Implement four constructors with the following parameters. If not given, assign the default name NoName. You should use the setters you just wrote in these constructors rather than directly assigning values to your instance variables. Remember to use deep copy!
    • Point upperLeft, int side
    • Point upperLeft, int side, String name
    • Square square
    • (none)-- Default upper left corner is at (0,0) and default side length is 0
  4. Implement public String toString():
Square: ; upperLeft: Point: (, ); side: 

Make use of toString() from the Shape and Point classes to implement this method. For example, if we have a Square named Gary with side length of 9 at (10,10):

Square: Gary; upperLeft: Point: (10, 10); side: 9
  1. Implement the draw() method.
    1. As it turns out, drawing the Square has already been implemented in the JavaFX library, we just need to invoke the right methods. Again, take a look at the documentation for the Rectangle class. All you need to do is call the constructor, change its color, and add the Rectangle object to the group.
    2. The parameters for our Square.draw() is different from the parameters of the Rectangle object! You need to convert our information (coordinate of upper left, side length) into information the constructor expects (x, y, width, height).
    3. Make sure that the fill parameter will cause the function to choose the appropriate JavaFX class to instantiate. If fill is false, make the fill of the shape empty by passing in null to the appropriate method and set the stroke outline to the color passed in.

Grid

We will draw a nice piece of modern art by using the Square class.

public void drawGrid(Group group, boolean fill, int n) 

Terminating Case (Base Case)

If n == 0, return without drawing any squares.

Initial Case

The color of the squares will be random, and the parameter n tells how many squares we want to draw for this particular instance of the fancy shape.

As you know, the Square is defined by its side and upper left point. For our initial step, we will draw the square as we usually do by calling draw(). The arguments group and fill will be passed simply from the parameters of drawGrid(). Generate a new random color (see Shape class for a hint) and make a call to draw().

Recursion

The recursion for this portion is slightly more complicated. In the drawBullsEye and drawTriforce methods, the recursion was linear: if n = 3, then we recursed three times because it drew three shapes embedded within each other. In other words, each method called itself only once. Here, we will have the method call itself four times for each n. That is, there should be four calls to drawGrid() in drawGrid() itself.

In the colorful grid picture on the top of this page, n is 4. If n == 1, then one square is drawn. If n == 2, then five squares are drawn. A new square is drawn at each vertex (corner) of root square. If n == 3, then 21 squares will be drawn (1 + 4 + 16).

However, some squares will be drawn over pre-existing squares! Since the recursive case is to always draw another square at the vertex of the original square, when n == 3, the original first square gets drawn-over four times and the darker blue squares below get drawn-over once. This is why n == 3 looks like it has only 13 squares, rather than 21.

In Shape Class:

image text in transcribed

import javafx.scene.*; import javafx.scene.paint.*; import javafx.scene.shape.*; import java.util.*; public abstract class shape ( private string shapeName; public Shape(String shapeName) this.shapeName shapeName; public Shape) this ("NoName"); public string getshapeName () return this.shapeName; public abstract void draw (Group group, Color c, boolean fill); public string tostring) return null; public static Color getRandomColor) Random random new Random; double r = random.nextDouble(), g random.nextDouble(), b random.nextDouble() return new Color(r, g, b, 1); // color constructor import javafx.scene.*; import javafx.scene.paint.*; import javafx.scene.shape.*; import java.util.*; public abstract class shape ( private string shapeName; public Shape(String shapeName) this.shapeName shapeName; public Shape) this ("NoName"); public string getshapeName () return this.shapeName; public abstract void draw (Group group, Color c, boolean fill); public string tostring) return null; public static Color getRandomColor) Random random new Random; double r = random.nextDouble(), g random.nextDouble(), b random.nextDouble() return new Color(r, g, b, 1); // color constructor

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions

Question

=+how they could be specifically applied.

Answered: 1 week ago

Question

What do you think of the MBO program developed by Drucker?

Answered: 1 week ago