Assignment 2: Input, Variables and Expressions, Drawing.

Introduction to Computer Program Design: Assignment 2

days Due 6 Oct 7pm

ALERT! This is a Pass/Fail (P/F) Assignment

Goals

The goal of this assignment is for you to be able to construct programs in Java that
  • use methods that call methods in the same class
  • use conditionals (if statements)

Online part

Start with the online part as it will help you consolidate your knowledge for the programming part.

This part is compulsory and you must pass all the online questions related to this assignment by the deadline to pass this assignment (There is no 24 hours grace period for these questions.).
The Challenge questions are optional but would help your learning.

right Go to the Online questions now and answer all the Assignment 2 questions.

Programming part

Note that for the programming part, you have to complete 2 programs. Each program has a Pass/Fail level that everyone must complete, and an optional Challenge level.

To Submit:

  • ParameterisedShapes.java
  • KeyValidator.java

right Do not rename these files. You must use the provided template files and not remove the setupGui and main methods.
right Remember to submit both of these files. When you have submitted them, check that you can read the files listed on the submission page, and complete the submission process.

Zip file

Program 1: Parameterised Shapes

ParameterisedShapes: Pass level

You must complete this part even if you want to do the Challenge level.

You are to complete 3 methods that all participate in drawing fancy rectangles:
  • A fancy rectangle consists of three horizontal stripes that each contains a circle. The circles can be either filled or just outlined.
  • Each stripe has its own height and colour.
  • The diameter of each circle is 20% of the height of the stripe.
    • For the top stripe, the circle is centered in the left-most third of the stripe.
    • For the second stripe, the circle is centered in the middle third of the stripe.
    • For the bottom stripe, the circle is centered in the right-most third of the stripe.
  • The rectangle is 2/3 as high as it is wide (ratio 2:3) OR The width of the flag is 1.5 times the height of the flag.

Below is an example of such rectangle where the circles are filled.

right The vertical lines are not part of the rectangle. They just illustrate how all columns are the same width (one third of the total width of the rectangle).

fancyRectFilled.png

Below is another example of such rectangle- here the circles are outlined.

fancyRectOutlined.png

You are to complete:

  • doFancyRect method so that:
    • it asks for the height of each stripes, and whether the circles are filled or outlined,
    • it then calls drawFancyRect with the necessary arguments.
    • tip Note that we have written part of the code for you but it is not complete.
  • drawFancyRect:
    • it needs parameters,
    • it must call drawStripe three times,
    • it draws a black rectangle to outline the complete rectangle.
  • drawStripe also needs parameters. It draws a stripe of the given width, height and colour with a circle at a given position.

ParameterisedShapes: Challenge level

Write methods for drawing the flag of China: Note, this is really hard

Chinese flag

You must define methods to:
  • ask for the position and the dimension of the flag then call the other methods for each of the five stars.
  • calculate the direction of a star
  • draw a star at some position, with a specified size, and pointing in a specified direction.
  • You may define other methods too.
tip You will need to find the size and positions of the stars from the web. Remember this is a Challenge question, so we expect the drawing of the flag to be correct and precise.

right Remember to add a button in setupGUI method to call your drawChinaFlag method.

Program 2: Key Validator

Validator: Pass level

ALERT! You must complete this part even if you want to do the Challenge level.

Many computer-based systems require the user to have some kind of key (a user code, a password, an ID, etc) for access. This is usually some string of characters (letters and/or digits and/or punctuation). It is often important for the key to have some particular format, and many systems will check that a user's key has a valid format.

The KeyValidator program asks the user to enter a key word and a name and then tests whether the key is valid or invalid, according to a set of rules. (Note, these rules are simple, and are made up for this assignment; they are NOT a good set of rules for a password!)

tip Look carefully at the documentation in the String class. You will find quite a few of the methods to be helpful, especially the length(), endsWith(...), and contains(...) methods.

Your program must check if the key is valid according to this set of rules:

  1. The key must be at least 8 characters long, AND at most 16 characters long, AND
  2. The key must not end with the special characters '#' or '$', AND
  3. The key must not have a hyphen ('-') character anywhere, AND
  4. The key must have at least one Upper case character and at least one Lower case character.

Complete the validateKey method so that it checks the key against the rules and either

  • reports that the key is valid, or
  • reports ALL the rules that the key failed.

Note that the validateKey method has one parameter - the key to check. The method should NOT ask the user to enter the key - that is already done for you by the checkKey method.

Validator: Challenge level

For the challenge, your program must do the same as the pass/fail level, except that it must also check these additional rules:
  1. The key must have a mix of numbers and letters (at least one of each)
  2. The key must not contain the letters of the user's name in sequence, even if separated by other characters.
    Eg, if name is "Peter", the key cannot be "xPyyetzzeXrY"

right Make sure you write a separate method for the challenge and add a button to call it. This is to assist in the marking.