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 8 of 8

Thread: DoubleQuadruple

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default DoubleQuadruple

    I dont know how to write this code I am new to Java and one of the labs is: write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10 Remember to initialize the loop control variable before the program enters the loop. Here is the half written program that they give you. and you are suppose to write the while part. i just cant figure out how to do it.? Can someone show me how this is done? I would really apprectiate it.

    // DoubleQuadruple.java - This program prints the numbers 0 through 10 along
    // with these values doubled and quadrupled.
    // Input:  None.
    // Output: Prints the numbers 0 through 10 along with their doubles and quadruples. 
     
    public class DoubleQuadruple
    {
    	public static void main(String args[])
    	{
     
    		String head1 = "Number: ";
    		String head2 = "Doubled: ";
    		String head3 = "Quadrupled:  ";				
    		int numberCounter; 	// Numbers 0 through 10.
    		int doubled;		// Stores the double of a number.
    		int quadrupled; 	// Stores the quadruple of a number.
    		final int NUM_LOOPS = 10; // Constant used to control loop.
     
    		// This is the work done in the housekeeping() method
    		System.out.println("0 through 10 doubled and quadrupled" + "\n");
     
    		// This is the work done in the detailLoop() method
     
                    // Initialize loop control variable.
    		// Write your counter_controlled while loop here.
    		    // Calculate double.
    		    // Calculate quadruple.
    		    System.out.println(head1 + numberCounter);
    	            System.out.println(head2 + doubled);
    		    System.out.println(head3 + quadrupled);
    		    // Next number.
     
    		// This is the work done in the endOfJob() method
                    System.exit(0);
    	} // End of main() method.
     
    } // End of DoubleQuadruple class.
    Last edited by helloworld922; October 5th, 2011 at 02:48 AM.

  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: DoubleQuadruple

    Do you understand how to use a While Loop, and what it does? Attempt to write the while loop first, and then I can help you with your logic.
    Simplicity calls for Complexity. Think about it.

  3. The Following User Says Thank You to Staticity For This Useful Post:

    NewAtJava (October 4th, 2011)

  4. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: DoubleQuadruple

    the while loop variable I initiated was numberCounter=0;
    so then I put while( numberCounter<=10){
    doubled=numberCounter*2;
    quadrupled=numberCounter*4;
    then the system out statements
    numberCounter++;
    end while

  5. #4

    Default Re: DoubleQuadruple

    Kenneth Walter
    Software Developer
    http://kennywalter.com

  6. #5
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: DoubleQuadruple

    in the course im taking they go through the while loops but in this particular problem they want you to use what they have given you not with what i can come up with. and so my problem is figuring out how to use the countercontrol while loop using the information they have already entered.

  7. #6

    Default Re: DoubleQuadruple

    While the counter is less than 10:
    double the counter and store it in another variable
    quadruple the counter and store that in another variable
    print the info
    increment the counter by 1
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  8. The Following User Says Thank You to kenster421 For This Useful Post:

    NewAtJava (October 4th, 2011)

  9. #7
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: DoubleQuadruple

    the while loop variable I initiated was numberCounter=0;
    so then I put while( numberCounter<=10){
    doubled=numberCounter*2;
    quadrupled=numberCounter*4;
    then the system out statements
    numberCounter++;
    end while
    im not sure if you read this but I cannot use anymore variables than the ones they have given.

  10. #8

    Default Re: DoubleQuadruple

    Quote Originally Posted by NewAtJava View Post
    the while loop variable I initiated was numberCounter=0;
    so then I put while( numberCounter<=10){
    doubled=numberCounter*2;
    quadrupled=numberCounter*4;
    then the system out statements
    numberCounter++;
    end while
    im not sure if you read this but I cannot use anymore variables than the ones they have given.
    Look closely at my last post, you'll notice a difference in the psuedo-code I have given from the code you have in the above quote. Determine if you want 0 through 10 all inclusive or 0 to 10 (not including 10)
    Kenneth Walter
    Software Developer
    http://kennywalter.com