CMPS 200 Homework Assignment 5, 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 5 – Due Wednesday Oct 31, 2012
Notes and Announcements
•
Reading Material: Chapter 5.
Exercises
1. SumPrimes. Redo the SumPrimes program from Assignment 3 but using a while loop inside the isPrime
method. The program 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:
public static boolean isPrime(int n)
2. Palindromes2. Write a method isPalindromePhrase that accepts a string as argument and returns
true or false indicating if the string is a palindrome or not. In this version of the method,
whitespaces, commas, and apostrophes are ignored. For example, “Lonely Tylenol” and “Madam, I’m
Adam” are both palindromes. The signature of the method should be:
public static boolean isPalindromePhrase(String str)
You are not allowed to generate a new string in your implementation of this method, and your
iteration must be expressed with a while loop.
Use this method to write a program Palindromes2 that takes an integer command line argument N
followed by N strings and prints the strings that are palindromes according to this extended definition.
3. KthRoot. The kth root of a number x can be found by an iterative method that can be described as
follows. Start with an initial estimate t of the kth root (this may be the number x itself, for example)
and obtain an improved estimate by computing the expression 1/k (x / tk-1 + (k-1) * t ). The process is
repeated until the root is found. Write a method kthRoot that takes two arguments: a double x and
an integer k and computes and prints the kth root of x. Write an appropriate main() to test your
method.
4. MonteCarloPi. Monte Carlo simulations are techniques that consist of choosing sample experiments at
random from a large set and then making deductions on the basis of the probabilities estimated from
the result of these experiments. In this problem, you are to implement a Monte Carlo method for
estimating the value of π.
Consider the first quadrant of a unit circle centered at (0,0). This quarter circle lies inside a unit square.
A point with coordinates (x,y) is inside the quarter circle iff x2 + y2 <= 1. The area of the quarter circle
region can be estimated by picking, at random, points (x,y) that lie in the unit square, and for each
point determining whether the point lies in the region. The fraction of points that fall in the region
should give an estimate of π/4 (ratio of the area of the region and the area of the enclosing unit
square). Multiplying by 4 gives an estimate of π.
Write a program MonteCarloPi that takes a command line integer parameter N and prints an
estimate of π using N random points as described above. Run your program with N = 10, 100, 10000,
and comment on the values you get.
5. BinaryToDecimal. Write a method binaryToDecimal that takes a string as argument (the string consists
of a sequence of ’0’ and ‘1’ characters representing the binary representation of a number) and returns
an integer with the decimal value of the number. The decimal value of a number with the binary
representation of the form
may be computed as
.
Introduction to Computer Programming (CMPS 200)
Fall 2012
2 of 2
Use the method above to write a program that takes a command line argument N followed by N strings
representing binary number representations and prints their decimal values.
6. MakeChange. Write a program that reads a command line integer N (number of Lebanese pounds in
thousands) and prints out the best way (fewest number of bills) to make change using 100, 50, 20, 10,
5 and one thousand bills.
> java MakeChange 92
1 50,000
2 20,000
2 1,000
Hint. Your program should dispense as many bills of 100 thousands as possible, then 50 thousands,
then 20 thousands, then 10 thousands, then 5 thousands, a finally one thousand