CMPS 200 Homework Assignment 7, Fall 2012

American University of Beirut · Introduction to Programming · 2 pages

All CMPS 200 files
CMPS 200 Homework Assignment 7, Fall 2012, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 200 Homework Assignment 7, Fall 2012, page 2

More CMPS 200 files

Show the text of this homework
Faculty of Arts & Sciences Department of Computer Science CMPS 200—Introduction to Programming Assignment 7 – Due Monday Nov 12, 2012 1. MinMax Rewrite the MinMax program from last week so it reads its input from the user interactively at runtime. The program should ask the user to enter N integers. C:> java MinMax 3 Enter integer 1: 8 Enter integer 2: 90 Enter integer 3: -1 Minimum: -1 Maximum: 90 2. Discount Write a program Discount.java that reads in a list of prices from standard input and writes on the standard output a formatted table consisting of three columns as shown below. The first column consists of the prices as read from standard input (in US dollars). The second column prints a discounted price that represents a 1/3 discount on the price. The third column prints the equivalent value in Lebanese Liras rounded to the nearest LL 50 (assume a conversion rate $1 = LL 1508). Your output should have 2 digits after the decimal in the first 2 columns and no decimal in the third. It should also print column headers and makes sure the columns are lined up properly. An example invocation of the program might produce: > java Discount < prices.txt Price Discounted LL Equivalent Price $ 80.00 $53.33 LL 80450 $140.00 $93.33 LL 140750 3. CheckerBoard Write a program CheckerBoard.java that takes a command line input N and plots an N-by-N checkerboard. Always color the lower left square red. Draw the circles in red and black. Below is the desired output for N = 5, 10, and 25. Write a variation on the program that reads three command line parameters and draws three corresponding checkerboards. 4. Barcode For faster sorting of letters the U.S. Postal System encourages companies that send large volumes of mail to use a barcode denoting the zip code of the destination. The barcode is defined as follows: Each decimal digit in the 5-digit zip code is encoded using a sequence of three half-height and two full-height bars, as per the table below. Introduction to Computer Programming (CMPS 200) Fall 2012 2 of 2 The barcode starts and ends with a full-height bar (called the guard rail) and includes a checksum digit (after the five-digit zip code), computed to make the sum of the six digits a multiple of 10. For the 5-digit zip the barcode consists then of 32 bars in total. For example the barcode below encodes the zip 12345. a. Write a program Barcode.java that takes a 5-digit zip code and prints the corresponding barcode on the screen (Use: for half bars, and | for full bars). For example, > java Barcode 95014 ||:|:::|:|:||::::::||:|::|:::||| b. Write another version of this program that draws the bar code in a canvas to produce a compelling visual output as in the example above for the 12345 zip code. 5. ChangingColor Write a program that draws a square and changes its color to a random color every half a second. 6. BouncingBall Modify the program BouncingBall.java so that the ball leaves the domain of the animation if it hits the right half of the top boundary. For example, in the picture below, the ball should leave the domain instead of bouncing back off the top. When the ball leaves the domain the animation should stop. 7. Squares Write a program Squares.java that draws filled black and white squares of random size at random positions in the unit square, producing images like those below. Your program should take four command-line arguments: the number of squares, the probability that each square is black, the minimum side length, and the maximum side length. 200 1 0.02 0.02 200 1 0.02 0.1 500 0.5 0.02 0.1 50 0.25 0.2 0.4 8. GuessTheNumber. Write an interactive program that prompts the user to think of an integer between one and a million and then asks the user to truthfully respond with true or false to statements of the form "My number is less than or equal to x?" for various values of x, until the program can deduce the chosen integer. Your program should use a strategy known as binary search for guessing. Binary search chooses the middle of the search interval as the value of x at every iteration. 9. DocumentStats Write a program that reads from standard input and counts the number of words and the number of characters (without spaces) in the input. For example, the file AUBMissionStatement.txt (download from Moodle) which contains the text of AUB’s mission statement would generate the following statistics: > java DocumentStats < AUBMissionStatement.txt Number of Words: 105 Number of characters: 613