Unit 6: Practice

Table of Contents

  1. Practice
    1. Geometric
  2. Challenges
    1. Robot

Practice

Geometric

classes

abstract classes

abstract methods

inheritance

Comparable interface

GeometricObject

Rewrite the GeometricObject class from Unit 5 so that it is abstract and contains 2 abstract methods: getArea and getPerimeter.

public abstract class GeometricObject {
    // write your code here
}

Solution

Circle

Rewrite the Circle class from Unit 5 so that it extends the abstract GeometricObject class you just wrote.

public class Circle extends GeometricObject {
    // write your code here
}

Solution

Rectangle

Rewrite the Rectangle class from Unit 5 so that it extends the abstract GeometricObject class you just wrote.

public class Rectangle extends GeometricObject {
    // write your code here
}

Solution

TestGeometricObject

Write a class called TestGeometricObject which tests the getArea, getPerimeter, and compareTo methods of Circle and Rectangle.

To do this, create an array of Circle and Rectangle objects. For each GeometricObject in the array, print its area and perimeter. To test the compareTo method, use Arrays.sort on the array and print the sorted array. (The Arrays.sort method sorts the array in place. For example, if you called your array shapes, you would write Arrays.sort(shapes).)

import java.util.Arrays; // so that you can use Arrays.sort()

public class TestGeometricObject {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution

Challenges

Robot

enumerated types

finite state machines

classes

methods

aggregation

The code referenced in the following exercises was for the 2019-2020 FTC season, Skystone.

The DcMotor and Servo classes that are provided in the following exercises are extremely simplified versions which model the actual classes that are built-in to the FTC SDK. You should either download or copy and paste the code since you will use them in the classes you’ll write.

View DcMotor

View Servo

Arm

Partially rewrite the Arm class so that there is only 1 field called motor of type DcMotor.

Create an enum called Position whose values are the names of each position the arm can be in. It should have 1 field, ENCODER_VALUE, a constant int. Write a constructor for the enum so that each arm position has its corresponding encoder value.

The Arm class should have 1 method called moveArm which takes a Position and sets the position of the Arm’s motor to the Position’s encoder value.

public class Arm {
    // field here

    public enum Position {
        // write your code here
    }

    public void moveArm(Position pos) {
        // write your code here
    }
}

Solution

FoundationGripper

Partially rewrite the FoundationGripper class so that there is only 1 field called gripper of type Servo.

Create an enum called Position whose values are the names of each position the foundation gripper can be in. It should have 1 field, SERVO_POSITION, a constant double. Write a constructor for the enum so that each foundation gripper position has its corresponding servo position value.

Write a method called moveGripper which takes a Position and sets the position of the foundation gripper’s servo (gripper) to the Position’s SERVO_POSITION value.

public class FoundationGripper {
    // field here

    public enum Position {
        // write your code here
    }

    public void moveGripper(Position pos) {
        // write your code here
    }
}

Solution

Intake

Partially rewrite the Intake class so that there are 2 fields left and right, both of type DcMotor.

Create an enum called Direction whose values are the names of each direction the intake can be going in. It should have 2 fields, LEFT_POWER and RIGHT_POWER, both constant doubles. Write a constructor for the enum so that each direction has its corresponding left and right power values.

Write a method called runIntake which takes a Direction and sets the power of the Intake’s left and right motors to the Direction’s left and right power values, respectively.

public class Intake {
    // fields here

    public enum Direction {
        // write your code here
    }

    public void runIntake(Direction dir) {
        // write your code here
    }
}

Solution