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

Thread: need help with this basic program containing loops

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with this basic program containing loops

    Hey all, any help you could give me would be greatly appreciated. I'm pretty new and obviously very terrible at this, so thanks in advance.


    These are the instructions I was given:

    Write a Java program to calculate an approximation of π using the first n terms in the formulae given below:
    1. Madhava-Leibniz series:

    f4Plb.png

    2. Wallis product:

    OA5mM.jpg




    The program should have a main menu with three options:
    1. Madhava-Leibniz formula
    2. Wallis product
    3. Exit the program
    The program should NOT exit unless the user enters ‘3’. Any other integer input that is not [1-3] should prompt the user to re-enter the correct number in the range.
    If the user enters either 1 or 2, the program should ask for the number of terms n from the user (you are not expected to perform an error check on n, assume valid input). The program should display the n results of the series.
    Important:
    · The program should go back to the main menu after performing the calculations for either option 1 or 2.
    · The program should inform the user if the number entered for menu selection outside the bounds of [1,3] and go back to the main menu (NOT exit!). Exit is on input 3 only.
    Analyze the above formulas. Note that Wallis product calculates π/2 . Also, note that for Wallis product, the lower bound is 1 and for Madhava-Leibniz series, lower bound is 0. Upper bound for both will be user input, n.
    Calculate the result by hand for a few n inputs to test your program – do not rely on the correctness of your program without actually verifying the output.
    You should be using loops for the menu and for approximation calculations.





    And some further instructions on making the menu
    do {
    //Main menu prompt: print menu options
    System.out.println("Make a selection for the program to execute pi calculations: \n" +
    "\t" + "1. Madhava-Leibniz Formula \n" +
    "\t" + "2. Wallis Product \n" +
    "\t" + "3. Exit the program");

    //get user input
    input = scanner.nextInt();

    //one option is to set up an inner while loop here - "an error message loop"
    //and loop until user gives you the input you want: 1, 2 or 3
    //basically: until correct input is entered, do NOT allow to proceed
    while(input not in [1 ,3] interval) {
    // error message: Invalid input;
    //solicit user input
    }

    //once you passed through the error checking loop, you can know that input is either 1, 2 or 3.

    if(input == 1) {
    //Madhava-Leibniz series
    //when testing your menu, print to the screen : Option 1
    }
    else if(input == 2) {
    //Wallis series - print feedback to screen
    }

    //quit when the user inputs 3 - since the input passed through error message loop, it is either 1, 2 or 3
    //otherwise, if it was 1 or 2, the loop will send program control back to the top : the menu option
    } while(input is NOT 3);

    //print "exit" message to the user : Thank you for using the Pi Approximation Calculator


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help with this basic program containing loops


  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with this basic program containing loops

    This is what I have so far, almost embarassed to post it

    public class Approximation


    {

    public static void main(String [] args)

    {

    //Main menu prompt: print menu options

    system.out.println("Make a selection for the program to execute pi calculations: \n" +
    "\t" + "1. Madhava-Leibniz Formula \n" +
    "\t" + "2. Wallis Product \n" +
    "\t" + "3. Exit the program");
    //get user input
    input = scanner.nextInt();

    while(input not in [1,3] interval)

    { system.out.println("Invalid input);
    //solicit user input

    }

    if(input == 1) {


    }



    }

Similar Threads

  1. BASIC program help
    By ryanquanz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2011, 10:42 AM
  2. Some basic for loops I'm stuck on.
    By HeroFlame in forum Loops & Control Statements
    Replies: 8
    Last Post: November 9th, 2011, 07:52 PM
  3. Need help coding this program with for loops
    By jwall2 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 7th, 2011, 12:53 AM
  4. Java Basic - explanation of for loops
    By dubell in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2011, 07:55 PM
  5. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM