# Homework help...

Printable View

• 10-17-2008, 02:07 AM
robrobrob
Homework help...
Here are the original directions:

For this lab you will develop and test a Class called MathTasks with the following functionality. Include in your Lab Log sample runs on all provided test data.
1. a method called sumFor that has an integer parameter n and computes the sums of all the integers from 1 to n using a for construct. (hint: We already wrote the code in class – use your notes) Test on: 3,4,5,6,7
2. a method called sumWhile that does the same thing as sumFor, except that it is written using a while construct rather than a for construct. Test on: 3,4,5,6,7
3. a method called sumDoWhile that does the same thing as sumFor and sumWhile, except that it is written using a do_while construct rather than a for or while construct. Test on: 3,4,5,6,7
4. a method called sumIJ with two integer parameters i and j and computes the sums of all the integers from i to j using the most appropriate loop construct. Test on: (1,6),(3,4),(2,7)
5. a method called printPositiveEven that has an integer parameter n and prints all positive even numbers smaller or equal to n. Test on: 3,14,25, 32
6. a method called printAllMultiplesOfThree that has an integer parameter n and prints all positive numbers smaller or equal to n that are multiples of 3. Test on: 4,9,17,26,31
7. a method called printStars that has an integer parameter n and prints a line with n * characters. Test on: 3,4,5,6
ex: if the parameter n is 5 it prints *****
if the parameter n is 8 it prints ********
8. a method called printStarLines that has two integer parameters m and n and prints m lines. The first line has n * characters, the next one n+1, and so on. Test on: (2,5),(4,2),(5,5)
ex: if the parameter m is 4 and n is 6, then it prints:
******
*******
********
*********
9. a method called printStarLinesDecreasing that has two integer parameters m and n and prints 2*m-1 lines. The first line has n * characters, the next one n+1, and so on until the mth line has n+m-1, the (m+1)th line has n+m-2 and so on until the last one has n * characters. Test on: (2,5),(4,2),(5,5)
ex: if the parameter m is 3 and n is 5, then it prints:
*****
******
*******
******
*****
10. a method called isPowerOfTwo that has an integer parameter n and returns true if n is a power of 2, false otherwise. Test on: 3,4,8,12,32,33,100, 256,1024,1025

I need some help with the last two. Can anyone help me out? thanks!
• 10-17-2008, 02:47 AM
Fubarable
Quote:

Can anyone help me out?
Sure, but first you may wish to ask a specific question and your code showing your attempt at a solution.
• 10-17-2008, 03:34 AM
robrobrob
I got #9 to work but am still lost on 10...not even sure how to start it....not allowed to use sqrt cause it has to be an integer.
• 10-17-2008, 03:37 AM
Fubarable
Sure you can use sqrt, and in fact it's a very good idea to use sqrt here. If you call sqrt and convert the result to an int by casting, what can you do with the result to test the question? Think on that for a bit. In fact you may want to play with the test numbers with a piece of paper and a calculator (the Windows calculator would work just fine). Sqrt the test numbers, convert to int (i.e., 2.20392 becomes 2, 8.82933 becomes 8) and see how this helps you.
• 10-17-2008, 04:24 AM
Norm
Quote:

if n is a power of 2,
Describe in english what that means? Name the first 5 powers of 2.
What arithmetic steps would you need to do to determine if a number is a power of 2?
A quick test for false would be: is n odd.