CMPS 200 Homework Assignment 3, Fall 2011

American University of Beirut · Introduction to Programming · 3 pages

All CMPS 200 files
CMPS 200 Homework Assignment 3, Fall 2011, page 1

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

CMPS 200 Homework Assignment 3, Fall 2011, page 2 CMPS 200 Homework Assignment 3, Fall 2011, page 3

More CMPS 200 files

Show the text of this homework
Faculty of Arts & Sciences Department of Computer Science CMPS 200—Introduction to Programming Assignment 3 – Due Monday Oct 24, 2011 Notes and Announcements • Reading material for this week: Section 3.2; Chapter 4. • Don’t forget about the “Programming Clinic” open to all on Friday 2:00-4:00 in Bliss 210. This is intended to provide additional help and you are encouraged to take advantage of it as needed. • The email addresses of the Lab Instructors are as follows. George Sakr: ges07@aub.edu.lb. Samah Karim: swk10@aub.edu.lb; Gorune Ohannessian: hko00@aub.edu.lb. Khalid Dassouki: ke17@aub.edu.lb. Problem 1 - Grid Source file: Grid.java Output: standard output (screen monitor) Write a program, Grid, which accepts two integers from the command line; name the first one rows and the second cols. Pass these two integers to a method named printGrid that outputs a comma-separated grid of numbers where rows represents the number of rows of the grid and cols represents the number of columns. The numbers count up from 1 to (rows x cols). The output is displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. Assume that rows and cols are greater than 0. Sample Run 1: C:> java Grid 3 6 1, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18 C:> Sample Run 2: C:> java Grid 5 3 1, 6, 11 2, 7, 12 3, 8, 13 4, 9, 14 5, 10, 15 C:> Sample Run 3: C:> java Grid 4 1 1 2 3 4 C:> Sample Run 4: C:> java Grid 1 3 1, 2, 3 C:> Introduction to Programming (CMPS 200) Fall 2011 2 of 3 Problem 2 – Lebanese Flag Source file: LebFlag.java Output: standard output (screen monitor) Below are two ASCII Art pictures of the Lebanese flag. ASCII art is a graphic design technique that consists of pictures pieced together from the 95 printable characters defined by the ASCII Standard. The flag to the left is composed of 32 rows and 65 columns. The flag to the right is composed of 24 rows and 49 columns. In general, the number of columns will always be twice the number of rows plus 1. Figure 1: 32x65 Figure 2: 24x49 The ASCII Art pictures of the Lebanese flag have the following restrictions and details: 1. The number of rows, r, should be a multiple of 4. 2. 25% of r should be allocated for the upper part of the flag, 25% of r should be allocated for the lower part of the flag, and the remaining 50% should be allocated for the middle part of the flag. 3. The boundaries of the flag should be composed of the letter ‘M’. 4. The tree part of the flag should be composed of the letter ‘Z’. The first row (of the tree) should contain exactly one ‘Z’ and it should be placed exactly at the midpoint of the flag width. • The second row should contain 2 ‘Z’s more than the first row. • The third row should contain 4 ‘Z’s more than the second row • The forth row should contain 2 ‘Z’s more than the third row • The fifth row should contain 4 ‘Z’s more than the fourth row • etc. 5. The last three rows in the tree are always composed of 3,5, and 7 ‘Z’s regardless of the flag dimensions Write a java program, LebFlag.java, which asks the user to enter the number of the rows and outputs the appropriate ASCII Art picture of the Lebanese flag. Introduction to Programming (CMPS 200) Fall 2011 3 of 3 Problem 3 – NRandom Write a program that takes an integer N as a command-line argument and uses Math.random() to print N uniform random values between 0.0 and 1.0, and then prints their average value. Problem 4 – Fibonacci Write a program that takes an integer command line argument N and computes and prints the first N Fibonacci numbers as described in Exercise 3 on page 148 of your textbook. Problem 5 – PowersOfTwo Write a program that takes a command line argument integer M and prints all the positive integers that are powers of 2 and less than or equal to M. Problem 6 – LeapYear Write a program that takes a positive integer command line argument representing a year number and prints true if the year is leap and false otherwise. A leap year is a year divisible by 4 but not by 100, or is divisible by 400. Problem 7 – Leap5 Write a method that returns a boolean value if its argument is a leap year: public static boolean isLeap(int year) and use it to write a program that takes 5 positive integers as command line arguments and prints only the years that are not leap. Problem 8 – Quadrant Write a program that takes the x-y coordinates of a point in a Cartesian plane (assumed to be integers). The program should output a message indicating whether the point is the origin, is located on the x axis, is located on the y axis, or appears in a particular quadrant. Examples: (0,0) is the origin (4,0) is on the x-axis (0,-3) is on the y-axis (-2,3) is in the second quadrant Submission Instructions • As usual, your submission must consist of a zip archive that contains .java files and a single Word document that contains sample runs, answers to questions, etc. The files must be zipped in a file that adheres to the following naming convention asst2_<netid>, where <netid> stands for you AUBnet user name. These zipped files will be processed automatically so please make sure you use this naming convention. The single zipped file must be uploaded to Moodle. Please make sure there are no folders or subfolders in your submitted zip file. When the zip is opened, you should only see .java files and a word file at the top level. Please check your zip before submission.