•

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

need help with this basic prob containing loops

calculator loop

5 replies to this topic

#1 kingslayer

kingslayer

CC Lurker

• New Member
• 3 posts

Posted 23 March 2012 - 01:28 PM

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:

2. Wallis product:

The program should have a main menu with three options:
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 {
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) {
//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
• 0

#2 Norm

Norm

• Senior Member
• 397 posts
• Location:Eastern Florida
• Programming Language:Java, C++, Assembly

Posted 23 March 2012 - 03:42 PM

Cross posted at need help with this basic program containing loops
and at
Need help with this basic Java program - Dev Shed
• 0

#3 kingslayer

kingslayer

CC Lurker

• New Member
• 3 posts

Posted 23 March 2012 - 03:54 PM

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

public class Approximation

{

public static void main(String [] args)

{

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) {

}

}
• 0

#4 Norm

Norm

• Senior Member
• 397 posts
• Location:Eastern Florida
• Programming Language:Java, C++, Assembly

Posted 23 March 2012 - 04:01 PM

Can you list the routines that should be added to the code and what steps each of those needs to take to solve their parts of the whole problem.
When you get a list of what is to be done, start working on one, and leave the others for later. Type in the code, compile it and execute it until it does what you want. When it works, move on to the next item in the list.

Please use code tags when posting code. Select the code and press the #icon above the input box. It will create something like this:
```public class Approximation

{

public static void main(String [] args)

{

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) {

}

}
```

However I didn't format the code by adding indentations which is something that you should do when posting code.
• 0

#5 kingslayer

kingslayer

CC Lurker

• New Member
• 3 posts

Posted 23 March 2012 - 04:19 PM

yeah I don't even really know, this was just a shot in the dark. To call me a beginner is an insult to beginners.
• 0

#6 Norm

Norm

• Senior Member
• 397 posts
• Location:Eastern Florida
• Programming Language:Java, C++, Assembly

Posted 23 March 2012 - 04:25 PM

Just think about the steps you would need to do if you were using pieces of paper to do the job or were trying to explain the steps to a very dumb assistant that was going to do the job.
Worry about how to write the code after you have worked out what needs to be done.
If you can not make a list of the steps, you won't be able to write any code to do them.

If this program is too complicated, try to find some simpler programs to write first to build up your knowledge of programming.
• 0

Also tagged with one or more of these keywords: calculator, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download