Results 1 to 15 of 15

Thread: Can somebody help me finish this

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Can somebody help me finish this

    File PowersOf2.java contains a skeleton of a program to read in an integer from the user and print out that many powers of 2, starting with 20.

    1.Using the comments as a guide, complete the program so that it prints out the number of powers of 2 that the user requests. Do not use Math.pow to compute the powers of 2! Instead, compute each power from the previous one (how do you get 2n from 2n–1?). For example, if the user enters 4, your program should print this:

    Here are the first 4 powers of 2:
    1
    2
    4
    8

    2.Modify the program so that instead of just printing the powers, you print which power each is, e.g.:

    Here are the first 4 powers of 2:
    2^0 = 1
    2^1 = 2
    2^2 = 4
    2^3 = 8
    // ****************************************************************
    // PowersOf2.java
    //
    // Print out as many powers of 2 as the user requests
    //
    // ****************************************************************
    import java.util.Scanner;

    public class PowersOf2
    {
    public static void main(String[] args)
    {
    int numPowersOf2; //How many powers of 2 to compute
    int nextPowerOf2 = 1; //Current power of 2
    int exponent; //Exponent for current power of 2 -- this
    //also serves as a counter for the loop

    Scanner scan = new Scanner(System.in);

    System.out.println("How many powers of 2 would you like printed?");
    numPowersOf2 = scan.nextInt();

    //print a message saying how many powers of 2 will be printed
    System.out.println("Here are the first " + numPowersOf2 + " powers of 2: ");

    //initialize exponent -- the first thing printed is 2 to the what?
    exponent = 0;

    while (exponent < numPowersOf2);
    {
    //print out current power of 2
    System.out.println("2^" + -- exponent + " = " + nextPowerOf2);

    //find next power of 2 -- how do you get this from the last one?
    nextPowerOf2 = nextPowerOf2 * 2;

    //increment exponent
    exponent++;

    }
    }
    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Can somebody help me finish this

    I guess this is a little bit easier and works quicker
    Code:
    import java.util.* ;
    
    public class PowersOf2
    {
      public static void main ( String args[] )
      {
        Scanner s = new Scanner( System.in ) ;
        System.out.println( "How many powers of 2 would you like printed?" ) ;
        int max = s.nextInt() ;
        for ( int i = 0 ; i < max ; i++ )
        {
          System.out.println( Math.pow( 2, i ) ) ;
        }
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Can somebody help me finish this

    He did say to not use Math.pow


  4. #4
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Can somebody help me finish this

    Again, a direct excercise from the Lab Manual to "Java Software Solutions" by Lewis, Loftus, and Cocking. This was copied word for word, from page 56 to be exact. If you have specific questions about something, we'll answer, otherwise, do your own homework.
    Last edited by lunchboxtheman; Oct 25th, 2006 at 09:07 PM.

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Can somebody help me finish this

    Code:
     import java.util.* ; 
    
    public class PowersOf2 {  
       public static void main ( String args[] ) 
      {  
         Scanner s = new Scanner( System.in ) ;  
         int pow = 0;
         System.out.println( "How many powers of 2 would you like printed?" ) ;  
         int max = s.nextInt() ;  
      for ( int i = 0 ; i <= max ; i++ ) 
      {
        pow += (pow == 0)? 1: pow;
        System.out.println( pow ) ; 
      } 
     } 
    }
    i did not test this though, and i think there are areas where it can be improved.

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Can somebody help me finish this

    Quote Originally Posted by lunchboxtheman
    Again, a direct excercise from the Lab Manual to "Java Software Solutions" by Lewis, Loftus, and Cocking. This was copied word for word, from page 56 to be exact. If you have specific questions about something, we'll answer, otherwise, do your own homework.
    Hehehe, I had nothing better to do, so I figured to answer with c0d.
    He was having difficulties with the logic and approach I guess.

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Can somebody help me finish this

    Quote Originally Posted by oceanebelle
    Code:
     import java.util.* ; 
    
    public class PowersOf2 {  
       public static void main ( String args[] ) 
      {  
         Scanner s = new Scanner( System.in ) ;  
         int pow = 0;
         System.out.println( "How many powers of 2 would you like printed?" ) ;  
         int max = s.nextInt() ;  
      for ( int i = 0 ; i <= max ; i++ ) 
      {
        pow += (pow == 0)? 1: pow;
        System.out.println( pow ) ; 
      } 
     } 
    }
    i did not test this though, and i think there are areas where it can be improved.
    Code:
        for ( int i = 0 ; i < max ; i++ )
    Or you can rewrite the pow method
    Code:
    import java.util.* ;
    
    public class PowersOf2
    {
      public static void main ( String args[] )
      {
        Scanner s = new Scanner( System.in ) ;
        System.out.println( "How many powers of 2 would you like printed?" ) ;
        int max = s.nextInt() ;
        for ( int i = 0 ; i < max ; i++ )
        {
          System.out.println( pow( 2, i ) ) ;
        }
      }
    
      public static long pow ( int a, int b )
      {
        long ret = 1 ;
        for ( int i = 0 ; i < b ; i++ )
        {
          ret *= a ;
        }
        return ret ;
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Can somebody help me finish this

    Quote Originally Posted by lunchboxtheman
    Again, a direct excercise from the Lab Manual to "Java Software Solutions" by Lewis, Loftus, and Cocking. This was copied word for word, from page 56 to be exact. If you have specific questions about something, we'll answer, otherwise, do your own homework.
    WOW, you did good research about this
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Can somebody help me finish this

    Quote Originally Posted by ComputerJy
    WOW, you did good research about this
    I bet he does his own homework.

  10. #10
    New Member
    Join Date
    Oct 2009
    Posts
    1

    Re: Can somebody help me finish this

    // ****************************************************************
    // PowersOf2.java
    //
    // Print out as many powers of 2 as the user requests
    //
    // ****************************************************************
    import java.util.Scanner;

    public class PowersOf2
    {
    public static void main(String[] args)
    {
    int numPowersOf2; //How many powers of 2 to compute
    int nextPowerOf2 = 1; //Current power of 2
    int exponent;
    int pow=0;
    //Exponent for current power of 2 -- this
    //also serves as a counter for the loop
    Scanner scan = new Scanner(System.in);

    System.out.println("How many powers of 2 would you like printed?");
    numPowersOf2 = scan.nextInt();

    //print a message saying how many powers of 2 will be printed
    //initialize exponent -- the first thing printed is 2 to the what?
    System.out.println("You requested to print to the " + numPowersOf2+ "th power");
    exponent = 0;

    while ( exponent < numPowersOf2 )
    {
    //print out current power of 2
    System.out.println("2^"+ pow++ + " = " + nextPowerOf2);
    //find next power of 2 -- how do you get this from the last one?
    nextPowerOf2 = nextPowerOf2 * 2;
    //increment exponent
    exponent++;
    }
    }
    }

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Can somebody help me finish this

    One thing I notice is that the pow and exponent variables appear to be doing exactly the same thing. As for calculating the next value based on the previous one, I think nextPowerOf2 = nextPowerOf2*2 should work. If not, you can always calculate the actual value based on the value of pow/exponent (instead of calculating based on the previous value).


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    Lively Member
    Join Date
    Jul 2008
    Posts
    105

    Re: Can somebody help me finish this

    could you try this code

    int y = 0;
    for(int x = 0;x < input-1;x++){
    y = 2 ^ x;
    }

    ^^

    just trying to help

    ^^

  13. #13
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Can somebody help me finish this

    looooooooooool. did you try this code before posting it!
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  14. #14
    Lively Member
    Join Date
    Jul 2008
    Posts
    105

    Re: Can somebody help me finish this

    no ^^

    but based on what example he posted i think its correct ??

  15. #15
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Can somebody help me finish this

    Well, 1st of HE posted the question 3 years ago. He might have graduated by now and is no longer doing java

    2nd of all your code does nothing related to his question. In case you're a bit lost this is the Java forum not VB forum
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width