Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: need to create a program that adds multiples(confusing see inside more detail)

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need to create a program that adds multiples(confusing see inside more detail)

    I was assigned this problem for java class and i have spent well over ten hours working on this including theory and testing.
    so the problem goes like this the value for position 1 is 1, the value for position 2 is position 1 + 1*1, the value for position 3 is equal to position 2 + 2*1, the value for position 4 is equal to position 3 + 2*2 etc etc. the user needs to enter a position and you need to return the value here is an example of the math behind it
    Position Value addition to get the next value
    1 1 +1*1
    2 2 +2*1
    3 4 +2*2
    4 8 +3*1
    5 11 +3*2
    6 17 +3*3
    7 26 +4*1
    8 30 +4*2
    9 38 +4*3
    10 50 +4*4
    11 66 +5*1
    ]

    in other terms to get from 1 -> 2
    you add 1 + 1*1 = 2
    to get from 2 -> 3
    you add 2 + 2*1 = 4
    to get from 3 -> 4
    you add 4 + 2*2 = 8
    to get from 4 -> 5
    you add 8 + 3*1 = 11

    thank you in advance for your help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    Can you post your code and explain what your problem is?
    Post the program's output or any error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    Quote Originally Posted by Norm View Post
    Can you post your code and explain what your problem is?
    Post the program's output or any error messages.
    for clarification int l is the length, or the location the user wants to find the value of x and y are controlling variables and pos is the current position. val is the value.

    import java.util.Scanner;
    public class Problem07
    {
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner(System.in);
    		int val=2,pos=3,l,y=2,x=2;
     
    		System.out.println("Enter a Value");
    		l = in.nextInt();
    		if(l==1)
    			val=1;
    		else if(l==2)
    			val=2;
    		else
    		{
    			do
    			{
     
     
    			y=1;
    			if(x==y)
    			{
    					System.out.println("x is increased by 1");
    				x++;
    				System.out.println(" X = " + x);
    			}
     
    		do
    		{
    			System.out.println("The value is being increased by " + x + "  *  " + y);
    				val+=x*y;
    				System.out.println("The value is " + val);
    				y++;
    				System.out.println(" Y = " + y);
    				pos++;
    				System.out.println("Pos = " + pos);
    		}while(y<=x);
     
     
     
     
     
    			}
    			while(pos<=l);	
    		}
     
    		System.out.println(" value "+val);
     
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    The single letter l is a terrible name for a variable. It looks to much like a 1

    for clarification
    That is what comments in the code are for. If anyone copies the code for testing, your "for clarification" notes won't be in the code that is copied

    Can you show what the code does when it is executed?

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    Enter a Value
    5
    The value is being increased by 2  *  1
    The value is 4
     Y = 2
    Pos = 4
    The value is being increased by 2  *  2
    The value is 8
     Y = 3
    Pos = 5
    The value is being increased by 2  *  1
    The value is 10
     Y = 2
    Pos = 6
    The value is being increased by 2  *  2
    The value is 14
     Y = 3
    Pos = 7
     value 14
    The println statements are to view changes to the variables.I hope this helps. Thank you for the help with this

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    Is the answer wrong? You don't say if is wrong and show what it should be.
    If the answer is wrong then I'd say that your algorithm is not right.
    Can you list the steps in pseudo code for finding the solution?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    the answer for when a three is entered is 4 but an 8 was returned
    Enter a Value
    3
    The value is being increased by 2  *  1
    The value is 4
     Y = 2
    Pos = 4
    The value is being increased by 2  *  2
    The value is 8
     Y = 3
    Pos = 5
     value 8



    this is for when the value entered is 6 the answer should be 17 it is 14.
    Enter a Value
    6
    The value is being increased by 2  *  1
    The value is 4
     Y = 2
    Pos = 4
    The value is being increased by 2  *  2
    The value is 8
     Y = 3
    Pos = 5
    The value is being increased by 2  *  1
    The value is 10
     Y = 2
    Pos = 6
    The value is being increased by 2  *  2
    The value is 14
     Y = 3
    Pos = 7
     value 14

    this if for when the value entered is 7 the answer should be 26 the given answer is 20
    Enter a Value
    7
    The value is being increased by 2  *  1
    The value is 4
     Y = 2
    Pos = 4
    The value is being increased by 2  *  2
    The value is 8
     Y = 3
    Pos = 5
    The value is being increased by 2  *  1
    The value is 10
     Y = 2
    Pos = 6
    The value is being increased by 2  *  2
    The value is 14
     Y = 3
    Pos = 7
    The value is being increased by 2  *  1
    The value is 16
     Y = 2
    Pos = 8
    The value is being increased by 2  *  2
    The value is 20
     Y = 3
    Pos = 9
     value 20
    I appreciate your help with this

    There was no pseudo code but this was the prompt (paraphrased)
    1 2 4 8 11 17 26 30 38 50 66
    1 2 3 4 5 6 7 8 9 10 11
    The value of location 1 is 1. The value of location 2 is 1 plus the product of 1*1. The value of location 3 is the value of location 2 (2) plus 2*1. The value of location 4 is the value of location 3 (4) plus the product of 2*2. This pattern continues with the program adding the first multiple of the next number in the sequence until the second factor is equal to the first factor (3*3) then the first factor is increased by 1 (3*1, 3*2,3*3, 4*1 , 4*2) .Allow the user to enter a location n (length in my program) and calculate the value at that location using this pattern.

    I am questioning what my algorithm for this would be or how should I go about it. I am very new and thus very lost with this. Thank you for sticking with me

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    Look at the table you posted earlier and in the column labeled: addition to get the next value
    there appears a pattern in the equation. The first number is repeated a number of times equal to its value, the second number starts at one and increases by 1
    This reminds me of a nested loop.
    To see the pattern write a small simple program with a nested loop. On the inside print out the values of the outer loop:i and inner loop:j indexes: println(i + " "+ j).
    Have the inner loop stop when its index equals the value of the outer loop index
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    test program
    public class testLoop
    {
    	public static void main(String[] args)
    	{
     
     
    		for(int j = 1; j<=5;j ++)
    		{
    			for(int i=1; i<=j;i++)
    			{
    				System.out.println(j + "  *  " + i);
    			}
    		}
    	}
    }

    output
    --------------------Configuration: <Default>--------------------
    1  *  1
    2  *  1
    2  *  2
    3  *  1
    3  *  2
    3  *  3
    4  *  1
    4  *  2
    4  *  3
    4  *  4
    5  *  1
    5  *  2
    5  *  3
    5  *  4
    5  *  5
     
    Process completed.

    I need to find a way to stop this loop when the desired position is reached. thank you for the guidance.
    Tried this and it failed to kick me out of the loop
    public class testLoop
    {
    	public static void main(String[] args)
    	{
    	int pos = 0;
    	do
    	{
    			for(int j = 1; j<=5;j ++)
    		{
    			for(int i=1; i<=j;i++)
    			{
    				System.out.println(j + "  *  " + i);
    				pos++;
    			}
    		}
     
    	}while(pos<5);	
     
    	}
    }
    Last edited by thawigga; April 5th, 2012 at 08:16 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    Look at the break statement to get out of a loop
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    I figured it out. Thank you so much for this i appreciate all the help and you staying with me throughout my stupidity.
    Last edited by thawigga; April 5th, 2012 at 08:36 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: need to create a program that adds multiples(confusing see inside more detail)

    What do you mean by control? Using an if statement with the break statement would exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Scanner Issues: Read for more detail, thanks.
    By Staticity in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:38 AM
  2. [SOLVED] Flash animations inside a Java program
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 18th, 2011, 03:43 AM
  3. Button help, confusing error.
    By camboy8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 10:25 PM
  4. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM
  5. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 01:17 AM