Results 1 to 14 of 14
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Need Assistance with a program

    I am doing can exercise where i have to find the largest integer n such that n^3 < 12,000. Below is the code i wrote, i believe i wrote an infinite loop.
    Could you assist me please by showing me how i could end this loop as I have tried various ways and they dont seem to work and tell me if i have done things properly.

    import java.lang.*;
    import java.util.*;

    public class Exercise04_13
    {
    public static void main(String[] args)
    {

    int n = (int) Math.random() * 100;
    int nCubed = (int) Math.pow(n,3);

    while( nCubed < 12000 )
    {

    if (nCubed == 10648)
    {
    System.out.print("The largest integer, given that n^3 < 12000 is: " + n);
    }

    else
    {
    n = (int) Math.random() * 100;
    }
    }

    }
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Need Assistance with a program

    Hi abi, welcome to the forums.

    You are right in your assumption, your code does generate an infinate loop. The reason for this is that your while loop loops whilst nCubed is less than 12000. As the value of nCubed does not change within the loop, if nCubed is intially set to below 12000 then this will loop indefinately.

    Regards.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Need Assistance with a program

    I should add that the value of n is always going to be 0.
    Java Code:
    int n = (int) Math.random() * 100;
    The above code converts Math.random to an integer prior to multiplying this by 100. Instead place the whole equation in brackets.
    Java Code:
    int n = (int) (Math.random() * 100);
    Please also use the [code] tags for any code, it makes it easier to read.

    Regards.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Need Assistance with a program

    First, what are the requirements of the exercise? Are you expected to creep up on the value by successively by incrementing a value starting at 1? Or could you just take the cube root of 12000 and then truncate the fractional part? I am not certain why you are using random numbers at all. Since the answer is 22 and 22 cubed is 10648 it looks like you already have the answer encoded in your program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need Assistance with a program

    Thanks. I hadn't realized that. The smallest things escape you don't they. Thanks for the welcome too.
    I want the value of the nCubed to change within the loop as well, how can i get it to do that. Without that happening the code would be pointless, if you know what Iam trying to say.

  6. #6
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need Assistance with a program

    All the exercise says is find the largest integer n such that n^3 <12000 using a while loop.
    Arent we supposed to use random no.s? I was using the answer to make the loop definite but it doesnt seem to be working as you may have noticed.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Need Assistance with a program

    Well, based on what you just posted, you don't need a random number. Just continue to increment an initial value say of zero by 1. Then cube it and compare to 12000. If it is greater than 12000, then the previous value is the largest integer which when cubed is less than 12000. Otherwise, try the next sequential number. You can use a break statement to exit the loop when you have found the number.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need Assistance with a program

    something like this

    int n = 0;
    int nCubed = (int) Math.pow(n,3);

    while( nCubed < 12000)
    {
    n += 1;}

    if (nCubed == 10648)
    {
    System.out.print("The largest integer, given that n^3 < 12000 is: " + n);
    }

    else
    {
    ++n;
    }

    its faulty but I wanna know if I am getting what your trying to say. Also, am i on the right track? I tried this and iam not getting any output at all.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Need Assistance with a program

    Not quite. First, forget about including the if statement with 10648 in your code as it is not necessary. You are not updating nCubed at all so it will never change and the loop will go on forever. All you need in your while loop is
    Java Code:
    n += 1;
    nCubed = n * n * n;
    As soon as nCubed is greater than or equal to 12000, the loop will terminate. This means that the value n must be 1 greater than the value you really want so you need to subtract 1 from it.

    Then print out that answer.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need Assistance with a program

    Thanks man, I got it now. Although, I am still confusing myself with the algorithm. Must be because I am tried that I am not thinking properly. I'll look at it again in the morning.
    I did get it to work though.

    int n = 0;
    int nCubed = n * n * n;

    while( nCubed <= 12000)
    {
    n += 1;
    nCubed = n * n * n;

    }
    System.out.print("The largest integer, given that n^3 < 12000 is: " + ( n - 1 ));

  11. #11
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need Assistance with a program

    I was wondering if you know how to convert the deci values for ASCII characters to display as the characters themselves in the output?

    e.g.

    if i put System.out.print( 65 ) will that by default show an A??

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Need Assistance with a program

    Nope! Check out the Character and String class methods. There should be some conversion methods there.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need Assistance with a program

    FYI turns out you have to cast it s.o.p((char)65)

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Need Assistance with a program

    Yep! Totally forgot about the cast.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Battleship Program Coding Assistance
    By nekaneka19 in forum New To Java
    Replies: 7
    Last Post: 03-21-2012, 06:26 PM
  2. Assistance with Basic authentication program
    By Fargo94 in forum New To Java
    Replies: 4
    Last Post: 05-17-2011, 09:22 AM
  3. Ladder Program (Need Assistance)
    By Grenade in forum Jobs Discussion
    Replies: 1
    Last Post: 12-14-2010, 10:21 PM
  4. Assistance needed ASAP: Postpix program
    By Debonairj in forum New To Java
    Replies: 18
    Last Post: 07-27-2010, 01:37 PM
  5. Replies: 7
    Last Post: 11-23-2009, 08:42 AM

Posting Permissions

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