CMPS 200 Homework Assignment 8, Fall 2011
American University of Beirut · Introduction to Programming · 3 pages
More CMPS 200 files
- Homework 11 · Fall 2018HomeworkView
- Homework Last Assignment · Fall 2018HomeworkView
- Homework Asst10 · Fall 2017HomeworkView
- Homework 9 · Spring 2017HomeworkView
- Homework Assignment 2 · Fall 2016HomeworkView
- Homework Assignment 5 · Fall 2016HomeworkView
- Homework Assignment 6 · Fall 2016HomeworkView
- Homework 1 · Spring 2016HomeworkView
Show the text of this homework
Faculty of Arts & Sciences
Department of Computer Science
CMPS 200—Introduction to Programming
Assignment 8 – Due Mon Nov 28, 2011
Notes and Announcements
Reading material for this week: Color and Picture classes; Arrays (covered in chapter 7 of the text).
The lab instructors have posted additional office hours on Moodle. You are encouraged to use these
office hours.
Exam 2 will take place on Saturday Dec 2nd, 9:00-10:30am. Exam will be closed book.
Problem 1. PasswordChecker
A password is a sequence of characters that can be used for authentication purposes. Passwords are
widely used in computer systems and networks; and they are often used to authenticate the identity of
an automated data processing (ADP) system user.
A valid password is a personal password that will authenticate the identity of an individual when
presented to a password system.
In this problem you are asked to check the validity of passwords through the development of some
routines that will play the role of a password system. Basic factors shall be considered in the design,
implementation, and use of a password system used to authenticate the identity of a person. In this
problem we only focus on two main factors, comprising 4 distinct rules:
Length Range Factor: Length Range is the set of acceptable lengths of passwords, expressed as a
minimum length through a maximum length.
o Rule 1: acceptable number of characters in a valid password must be more than 6 and
less than 15.
Composition Factor: Composition is the set of acceptable characters which may be used in a
valid password.
o Rule 2: valid password should not be only alphabetic characters
o Rule 3: valid password should not be only numeric characters
o Rule 4: valid password should not be a series of alphabetic characters ending with ‘99’
Passwords are alphanumeric characters with the ASCI code in the following ranges:
48 – 57 for numeric values (0, 1, …, 9)
65 – 90 : upper case alphabetic characters (A, B, …, Z)
97 – 122: lower case alphabetic characters (a, b, …, z)
Write a program, PasswordChecker, which asks the user to enter a password and then checks its validity.
The program should print “valid password” if the password follows the rules presented above. If the
password violates any of the rules, the program should print “not a valid password” along with the rules
that are violated. The program should keep repeating until the user enters a valid password.
Sample session
Please enter your password: CGI
not a valid password (rule(s): 1,2 violated)
Please enter your password: a199
not a valid password (rule(s): 1 violated)
Please enter your password: 99
not a valid password (rule(s): 1,3 violated)
Please enter your password: CGI99
not a valid password (rule(s): 1,4 violated)
Please enter your password: CMPS200
valid password
Introduction to Programming (CMPS 200)
Fall 2011
2 of 3
Program 2. Similar
Write a program Similar.java that take two file names as command line arguments and prints true or
false depending on whether or not the files have the same contents. Your program should check the files
character by character and ignore difference in the case of the characters that are letters. Write
appropriate test files to check your program.
Program 3. GrayImage
Write a program Smooth.java that takes the name of an image as a command line argument and
displays a grayscale version of the image. You may use the monkey.jpg image (posted on moodle) to
check your program.
Program 4. Smooth
Write a program Smooth.java that takes the name of an image as a command line and displays a
smoothed version of the image where every pixel color is replaced by the average of the colors of its 4
neighbors.
Program 5. AlternatingSum
Write a static method aSum() with the signature below that computes and returns the alternating sum of
the elements in an array:
public static int aSum(int[] x)
Write a method getInput() that takes an array of strings and returns an array of integers (assuming all
the strings can be properly parsed as integers).
public static int[] getInput(String[] x)
Use the two methods above to write a program ArraySum.java that prints out the alternating sum of
its arguments. The main method should read a sequence of strings from the command line.
Problem 6. Reverse
In this program you are to write two versions of a method that takes an array of integers and reverses it
(i.e., puts the last element first, the next to last element second, etc.). The first version of the method
reverses the array in place, i.e. it modifies the contents of the array that is passed in. The second version
creates a new array and returns it without modifying the contents of the array that is passed in. The
signatures of the methods are:
public static void reverseInPlace(int[] a)
public static int[] reverseCopy(int[] a)
Write a main() method that tests these two versions of reverse. The main method should read a
sequence of integers from the command line, call the methods above, and print out the appropriate
arrays.
Introduction to Programming (CMPS 200)
Fall 2011
3 of 3
Problem 7. PulseHeart
Write a method:
public static void heart(double size, Color c)
that draws a heart-shaped figure similar to the one below (you can draw the shape as a diamond with
two circles to the upper left and upper right sides). size refers to the diameter of the circular part of
the shape, and c is the color used for the drawing.
Write a method:
public static void pulseHeart(double size)
that draws a heart with its color changing every 100 milliseconds. You may want to change the color
continuously (say the color goes from blue to red in 10 steps, so that the animation cycles every
second). You should use the Color data type in this program.
Program 8. ArrayOps
Write the following methods that operate on 1D arrays.
// generate and return an array of doubles with n elements filled with random numbers from 0 to 1
public static double[] generate1D(int n)
// generate and return a 1D array of doubles filled with random numbers between min and max
public static double[] generate1D(int n, int min, int max)
// returns the sum of all the entries of a 1D array
public static double sum(double[] a)
// returns the sum of the entries that are less than the average of the array elements
public static double sumLessThanAverage(double[][] a)
Write a main() method that calls the methods above to test them.