Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2008, 03:07 AM
Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
robrobrob is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-17-2008, 03:47 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,431
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-17-2008, 04:34 AM
Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
robrobrob is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-17-2008, 04:37 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,431
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-17-2008, 05:24 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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.

Last edited by Norm; 10-17-2008 at 05:27 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 03:13 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org