CMPS 200 Homework Assignment 5, Fall 2011

American University of Beirut · Introduction to Programming · 2 pages

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

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

CMPS 200 Homework Assignment 5, Fall 2011, 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 5 – Due Wed Nov 9, 2011 Notes and Announcements • Reading material for this week: Sections 5.1 – 5.3 • The assignment is due this week on Wed at 8:00 am but you should try to finish it before the holiday. Problem 1. SumPrimes Redo the SumPrimes program from last week 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) Problem 2 - RemoveSpaces Write a method removeSpaces that accepts a string as argument and returns a string with the white space characters removed from it (the method should not print anything to the console). The signature of the method should be: public static String removeSpaces(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 write a program above that takes an integer command line argument N followed by N strings and prints each of the strings without spaces. Hint: The strings must be quoted on the command line. Problem 3 - Palindromes Write a method isPalindrome that accepts a string as argument and returns true or false indicating if the string is a palindrome or not. A palindrome is string that can be read the same way forward and backward. Your method must handle upper and lower case characters (the string “Madam” is a palindrome). The signature of the method should be: public static boolean isPalindrome(String str) You are not allowed to generate a new string in the your implementation of this method. Rather, you should walk through the string to determine if is a palindrome or not. Hint: Use the method Character.toLowerCase() and/or one or more of the other methods on variables of type char; see page 280 of your textbook. Use this method to write a program Palindromes that takes an integer command line argument N followed by N strings and prints the strings that are palindromes. Problem 4 – 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) Again here, you are not allowed to generate a new string in your implementation of this method. 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. Introduction to Programming (CMPS 200) Fall 2011 2 of 2 Problem 5. 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. Problem 6. 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.java that takes a command line integer parameter N and prints an estimate of π using N random points as described above. How good are the estimates with N = 10, 100, 10000? Problem 7. 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 a binary representation of the form may be computed as . 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. 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 or notes you may wish to add. The files must be zipped in a file that adheres to the following naming convention asst5_<netid>, where <netid> stands for you AUBnet user name. • 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. Your zip file must be uploaded to Moodle.