Results 1 to 5 of 5
  1. #1
    robrobrob is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    robrobrob is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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.
    Last edited by Norm; 10-17-2008 at 04:27 AM.

Similar Threads

  1. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 06:00 AM
  2. Need help with homework.
    By JavaNewbie0000 in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 03:06 PM
  3. thread homework
    By integralli in forum Threads and Synchronization
    Replies: 6
    Last Post: 06-21-2008, 04:34 PM
  4. Some input on a homework
    By alcohole4life in forum New To Java
    Replies: 1
    Last Post: 05-23-2008, 04:18 PM
  5. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •