CMPS 200 Homework Assignment 3, Fall 2012
American University of Beirut · Introduction to Programming · 2 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 3 – Due Monday Oct 15, 2012
Reading Material
The String data type. pp 179-183 in section 3.3 of the text.
Command line arguments. Class notes.
Conditionals. Chapter 4 of textbook.
Exercises
1. Reverse. Write a method printReverse that accepts a string as argument and prints the characters in opposite
order. The signature of the method should be:
public static void printReverse(String str)
Use the method to write a program that takes an integer command line argument N followed by N strings and
prints each of the strings in its reverse order.
2. Reverse2. Write a method stringReverse that accepts a string as argument and returns a string with the
characters in opposite order (the method should not print anything to the console). The signature of the
method should be:
public static String stringReverse(String str)
Hint: The method should build a string one character at a time using the + operator, starting with the empty
string ””. Use this method to rewrite the program above that takes an integer command line argument N
followed by N strings and prints each of the string in its reverse order.
3. Vowels. Write a static method isVowel that returns true or false depending on whether its argument is a
vowel or a consonant:
public static boolean isVowel(char c)
Use the method above to write a program Vowels.java that takes a string argument on the command line
and prints the number of vowels in the string.
4. Quadrant. Write a program Quadrant 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
5. LeapYear. Write a program LeapYear 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. You may not use a conditional statement in your program. You will need to
write an appropriate Boolean expression that evaluates to the correct true/false answer.
6. 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 Leap5 that takes 5 positive integers as command line arguments and prints only
the years that are not leap.
7. BMI. The body mass index (BMI) is the ratio of the weight of a person (in kilograms) to the square of the height
(in meters). Write a program BMI.java that takes two command-line arguments, weight and height,
computes the BMI, and prints out the corresponding BMI category:
Starvation: less than 15
Anorexic: less than 17.5
Underweight: less than 18.5
Introduction to Computer Programming (CMPS 200)
Fall 2012
2 of 2
Ideal: greater than or equal to 18.5 but less than 25
Overweight: greater than or equal to 25 but less than 30
Obese: greater than or equal to 30 but less than 40
Morbidly Obese: greater than or equal to 40
8. SumPrimes. Write a program that takes a command line argument N and a sequence of N positive integers
and prints the numbers that are prime only, followed by their sum. Hint: Write a method with the following
signature, that returns a boolean value indicating whether an integer is prime or not.
public static boolean isPrime(int n)
Submission Instructions and Notes
As usual, submit your commented source code and sample runs in a zip file named s#_asst3_netid, where #
is your section number (between 1 and 9) and netid stands for your AUBnet user name.