Assignment 2: Input, Variables and Expressions, Drawing.
Introduction to Computer Program Design: Assignment 2
Due 6 Oct 7pm This is a Pass/Fail (P/F) AssignmentGoals
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)
Resources and links
- Online Part: Online questions
- Programming part:
- Download Zip file
- Submit your TWO java files.
- Marks and Feedback.
- Java Documentation
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. 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
setupGui
and main
methods.
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.
-
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. - 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 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.
drawChinaFlag
method.
Program 2: Key Validator
Validator: Pass level
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!)
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:
- The key must be at least 8 characters long, AND at most 16 characters long, AND
- The key must not end with the special characters '#' or '$', AND
- The key must not have a hyphen ('-') character anywhere, AND
- The key must have at least one Upper case character and at least one Lower case character.
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.
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:- The key must have a mix of numbers and letters (at least one of each)
- The key must not contain the letters of the user's name in
sequence, even if separated by other characters.
Eg, ifname
is "Peter", the key cannot be "xPyyetzzeXrY"