Help me with a simple java method?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

minty ice
Posts: 11
Joined: Tue Aug 05, 2008 5:30 am UTC

Help me with a simple java method?

Postby minty ice » Sat Oct 11, 2008 4:39 pm UTC

Can you guys help me with two simple java methods? We just started to learn about loops in my java class and we have to code some stuff but I'm not quite sure how to do it.


We have to make 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.

ex: if the parameter m is 3 and n is 5, then it prints:

*****

******

*******

******

*****

I already wrote the code for making it increase from 3 to 5, but I can't get it to go from 5 to 3.

Here's my code:

Code: Select all

public void printStarLines (int m, int n)//prints n lines of m * characters increasing 1 each line
{
for(int i = 1; i <= n; i++, m++)
{
printStars(m);
System.out.println("");
}
}



The other method is a method called isPowerOfTwo that has an integer parameter n and returns true if n is a power of 2, false otherwise. I have no idea how to even go about doing this haha.


Any help would be very much appreciated :)

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Help me with a simple java method?

Postby Berengal » Sat Oct 11, 2008 7:25 pm UTC

Power of two is easy:

Code: Select all

boolean isPowerOfTwo(int n){
  for(int i = 1; i <= n; i *= 2){
    if (i == n) return true;
  }
  return false;
}
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

chibu
Posts: 15
Joined: Sat Oct 11, 2008 4:38 pm UTC

Re: Help me with a simple java method?

Postby chibu » Sat Oct 11, 2008 7:35 pm UTC

For your second Method, You'll probably want to make use of the Modular Division operator. It's used like +, -, *, and /. But the value given is the remainder after division.

For example:
5 % 2 = 1 and 7 % 4 = 3


So if you have an input number n, you will probably want to check if n % 2 is equal to zero. If it is, you'll want to divide n by 2 and check again. You should probably do that over and over again until n = 1 (which is actually 20). You will probably want to use some form of a loop for this as well. You will probably want to return a true or false value.

As for the first problem: You should probably make another loop where i starts at the number it ended with in the first loop and goes back down to one. HINT: declare int i before the first loop to make it retain its value.

Well, I hope this is of some help to you.

~ Chibu

User avatar
Why Two Kay
Posts: 266
Joined: Sun Mar 23, 2008 6:25 pm UTC
Location: Plano, TX
Contact:

Re: Help me with a simple java method?

Postby Why Two Kay » Sat Oct 11, 2008 8:06 pm UTC

Berengal wrote:Power of two is easy:

Code: Select all

boolean isPowerOfTwo(int n){
  for(int i = 1; i <= n; i *= 2){
    if (i == n) return true;
  }
  return false;
}


Wouldn't this also work?

Code: Select all

public static boolean testp2(int x)
   {
      if(Math.log(x) / Math.log(2) == Math.floor(Math.log(x) / Math.log(2)))
         return true;
      return false;
   }


And it would be faster/more efficient since there is no looping going on.

Edit: Removed extra parenthesis my IDE added for some reason.
Last edited by Why Two Kay on Sat Oct 11, 2008 10:14 pm UTC, edited 1 time in total.
tl;dr - I said nothing important.

chibu
Posts: 15
Joined: Sat Oct 11, 2008 4:38 pm UTC

Re: Help me with a simple java method?

Postby chibu » Sat Oct 11, 2008 8:17 pm UTC

Why Two Kay wrote:And it would be faster/more efficient since there is no looping going on.


Wow, I definitely agree. That is a really sweet solution to the question. I wouldn't have ever thought of that >_<

The only problem is that:

minty ice wrote:We just started to learn about loops in my java class


So, the problem should probably be solved in some way using loops, even though your way is better :P I always forget about logs. I'll have to start remembering them when I'm working on PE projects.

~ Chibu

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Help me with a simple java method?

Postby Rysto » Sat Oct 11, 2008 8:43 pm UTC

Floating point operations can be expensive, but I'm not sure how that would compare to a loop. The best way to test if an integer is a power of 2 is the following:

Code: Select all

boolean isPowerOfTwo(int x) {
    return x != 0 && (x & (x-1)) == 0;
}

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Help me with a simple java method?

Postby Berengal » Sat Oct 11, 2008 9:23 pm UTC

Using logarithms is faster if you're working on big numbers, but you should never trust floats in direct comparisons. Try the following:

Code: Select all

double d=0;
for(int i = 0; i < 10; i++) {
   System.out.printf("%f == %f: %b\n", d, 0.1*i, d == (0.1 * i));
   d += 0.1;
}

Also try changing the printf string to "%.20f == %.20f: %b\n".

Floats lie. Use integers when you need to know that the laws of mathematics hold true, and never compare floats to each other directly. When you need to do "a == b", do "abs(a - b) < e" where e is a sufficiently small value.

The bit-comparison is the best way, but it needs "if (x < 0) x = -x;".
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Why Two Kay
Posts: 266
Joined: Sun Mar 23, 2008 6:25 pm UTC
Location: Plano, TX
Contact:

Re: Help me with a simple java method?

Postby Why Two Kay » Sat Oct 11, 2008 10:15 pm UTC

Rysto wrote:Floating point operations can be expensive, but I'm not sure how that would compare to a loop. The best way to test if an integer is a power of 2 is the following:

Code: Select all

boolean isPowerOfTwo(int x) {
    return x != 0 && (x & (x-1)) == 0;
}


I never even reasoned that such a property held true, nice.
tl;dr - I said nothing important.

pma
Posts: 4
Joined: Sat Sep 27, 2008 8:17 pm UTC

Re: Help me with a simple java method?

Postby pma » Sat Oct 11, 2008 11:14 pm UTC

Berengal wrote:Power of two is easy:

Code: Select all

boolean isPowerOfTwo(int n){
  for(int i = 1; i <= n; i *= 2){
    if (i == n) return true;
  }
  return false;
}

It would be better to loop down from n, dividing by 2. That way falsity would be reached faster.

minty ice
Posts: 11
Joined: Tue Aug 05, 2008 5:30 am UTC

Re: Help me with a simple java method?

Postby minty ice » Sun Oct 12, 2008 12:18 am UTC

Thanks for the help guys! :)

Vempele
Posts: 69
Joined: Wed Nov 14, 2007 8:05 am UTC

Re: Help me with a simple java method?

Postby Vempele » Mon Oct 13, 2008 12:35 pm UTC

Rysto wrote:Floating point operations can be expensive, but I'm not sure how that would compare to a loop. The best way to test if an integer is a power of 2 is the following:

Code: Select all

boolean isPowerOfTwo(int x) {
    return x != 0 && (x & (x-1)) == 0;
}

Will fail if x == -2147483648, so make that x > 0.
const int ALMOST_FIFTY = 80;

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Help me with a simple java method?

Postby Rysto » Tue Oct 14, 2008 4:35 pm UTC

D'oh! I usually use that on unsigned integers.

chibu
Posts: 15
Joined: Sat Oct 11, 2008 4:38 pm UTC

Re: Help me with a simple java method?

Postby chibu » Tue Oct 14, 2008 7:44 pm UTC

Dude, couldn't you do something like:

Code: Select all

boolean isPowerOfTwo(int x) {
     s = Integer.toBinaryString(x);
     return (x > 0 && s.indexOf('1') == s.lastIndexOf('1'))
}


I haven't actually tried it (don't have java installed on my fiance's box), and it's probably slower than the Rysto's method, but I think it's a pretty cool concept. Anyway, I suppose this isn't actually helpful the the initial question though :P

~ Chibu

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Help me with a simple java method?

Postby Berengal » Tue Oct 14, 2008 8:15 pm UTC

That's what the binary and does: ensures only one bit is set (because 00...00100...00 - 1 = 00...00011...11)
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Help me with a simple java method?

Postby Rysto » Tue Oct 14, 2008 9:49 pm UTC

Clever, but you can do one better by not bothering to convert it to a string first. I know that there are Java functions that take integers and return the index of the first/last set bit, but I can't remember where they're defined.

User avatar
Dropzone
Posts: 405
Joined: Sun Dec 30, 2007 10:12 pm UTC
Location: North Lincs., UK

Re: Help me with a simple java method?

Postby Dropzone » Wed Oct 15, 2008 2:23 am UTC

Here. But if you're going to go that route, you might as well just do:

Code: Select all

boolean isPowerOfTwo(int x) {
    return x > 0 && Integer.bitCount(x) == 1;
}

User avatar
Grop
Posts: 1994
Joined: Mon Oct 06, 2008 10:36 am UTC
Location: France

Re: Help me with a simple java method?

Postby Grop » Wed Oct 15, 2008 8:09 am UTC

Hi minty ice, concerning your first problem, I think it is an error to modify m: if you left it unmodified, you could compare i and m in order to know that you have reached the mth line.

What's more, you seem to be confused about what is n, and what is m: you want to draw 2*m-1 lines, not n lines. The fact you actually draw 5 lines when n is 5 and m is 3 is a coincidence: in this case 2*m-1 and n are equal.

Your loop should therefore look like this:

Code: Select all

for (int i = 0; i < 2*m-1; i++) {
//do something
}

chibu
Posts: 15
Joined: Sat Oct 11, 2008 4:38 pm UTC

Re: Help me with a simple java method?

Postby chibu » Wed Oct 15, 2008 2:55 pm UTC

Dropzone wrote:Here. But if you're going to go that route, you might as well just do:

Code: Select all

boolean isPowerOfTwo(int x) {
    return x > 0 && Integer.bitCount(x) == 1;
}



Yeah, I know. Basically, I just had it in my head so I typed it out. In short though, minty ice is learning about loops so none of these solutions are really helpful to the assignment anyway :P


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests