Practice Quiz 1

Getting Started
  1. Create a project called PQ1

  2. Download and import PQ1-starter

Your directory should look like this:

PQ1/
└── src
    ├── Circle.java
    ├── Point.java
    ├── PQ1.java
    ├── Rectangle.java
    ├── Shape.java
    └── Triangle.java

The Circle, Point, Rectangle, Shape, and Triangle classes have been provided to assist with questions in the PQ1 class.

You will only need to modify PQ1.

Instructions
  1. Implement the createShapeArray method:

    • Create and return an ArrayList of Shapes. Add the shapes to the ArrayList in the following order:

      • A Circle using Point a and Point b.

      • A Triangle using Point a, Point b, and Point c.

      • A Rectangle using Point a, Point b, Point c, and Point d.

  2. Implement the displayArea method:

    • Loop through an array of shapes and print using the following specifications:

      • the shape type (the class name) in capital letters

      • followed by a colon and a space

      • the area up to 2 decimal places (use printf or String.format) followed by a newline.

The list from the first problem prints:

CIRCLE: 78.54
TRIANGLE: 5.00
RECTANGLE: 10.00
  1. Recursively implement the sumOfSquares method:

    • returns the sum of the squares of each element of the ArrayList.

    • Additionally, add the square of each element to the ArrayList after the corresponding element. If the ArrayList is empty, a zero is returned and no numbers are added to the ArrayList.

For example:

List<Integer> example = new ArrayList<>(Arrays.asList(1, 2, 3));
System.out.println(example); // [1, 2, 3]
System.out.println(sumOfSquares(example)); // 14
System.out.println(example) // [1, 1, 2, 4, 3, 9]

Important
Once you have completed the quiz, turn it in to Checkin