Java 8: Practice Problems

From Deep Blue Robotics Wiki
Revision as of 21:23, 3 November 2016 by SammyZhang (Talk | contribs) (Object-Oriented Programming)

Jump to: navigation, search

You can use these exercises to practice your coding skills. If you can complete all of these exercises, then you are ready for build season.

Java Basics

Write a program to print out the day of the week (as a String) on a given date (month/day/year). Note that a normal year has 365 days. The months have 31, 28 (or 29 on a leap year), 31, 30, 31, 30, 31, 31, 30, 31, 30, and 31 days respectively. A leap year occurs whenever the year is divisible by 4 (but not 100). You may want to use the current date as a reference point. Your program should work for both past dates and future dates.

Sample Input: 1/4/2042

Sample Output: Saturday

Object-Oriented Programming

Create a class called Cypher. Its constructor should take a 26-letter String as its parameter. This String is the key to the cypher: the first letter in the String corresponds to 'A', the second letter corresponds to 'B', etc. The class should have methods to encrypt and decrypt a String using the cypher as a reference. For simplicity, all letters will be capitalized, spaces should remain unchanged, and no punctuation will be included. Please do not use 26 if-statements or a giant switch statement.

Sample Test Code:

public static void main(String[] args) {
    Cypher cypher = new Cypher("ZABCDEFGHIJKLMNOPQRSTUVWXY");
    System.out.println(cypher.encrypt("HELLO WORLD"));
    System.out.println(cypher.decrypt("GDKKN VNQKC"));
}

Sample Output: GDKKN VNQKC

HELLO WORLD

FRC Basics

Program the drivetrain on one of the old robots to drive using one of the logitech dual-action controllers. The robot should toggle between tank and arcade drive when button 1 on the controller is pressed. You can use the robot's I/O list for reference. Note that holding down the toggle button should not rapidly switch between modes; it should only switch modes one time when the button is pressed.

Command-Based Programming

Fully program one of the old robots. Use RobotBuilder to generate the framework. All basic functionality should be completed (i.e. full manual-control of the actuators and the sensor values displayed on SmartDashboard), but you do not need to worry about the more complicated functionality (e.g. vision, autonomous, PID, etc). You can use the robot's I/O list for reference, but you should design your own operator interface.

PID Control

Program an old robot to drive and turn autonomously using position and angle PID. Then tune the constants and test an auto mode that drives forward 5 feet, does a 360 degree turn, and drives backward 5 feet (to its original position). You can use the robot's I/O list for reference.

More Practice

If you have completed all of these exercises but still want more practice, these resources also have some good coding problems:

Project Euler

HP CodeWars