Java - program with SHM

1. Oct 19, 2012

CAF123

1. The problem statement, all variables and given/known data
I have to write a JAVA program that will compute and display (via the ptplot package) the solutions for the damped harmonic oscillator (x against t) in some given range for t. The solutions vary depending on how gamma and omega are related. The instructions given were:
1) Read in values of omega, gamma and the number of points to plot from the terminal
2) Calculate and plot the amplitude vs time using the ptplot package
3) Reprompt for new values of omega, gamma and no. of points
4) Add the results of this new calculation to the existing graph as a new series

3. The attempt at a solution
Writing up how to draw the graph is fine, I am just getting a bit confused about how to go about coding the loop (i.e the bit about reprompting for new values)
I remember someone saying they had to enclose the code in a 'while' loop, but I am not sure what the boolean condition would be here.
What I have written so far is something like:
while (some boolean statement) {
// In my program, I have the code that should be under this while statement correctly, here I will just write what I am doing - Ask user for values of omega,gamma and no.of points and parse into double form

if (gamma>2omega) {
//Here I write in the code to draw this graph etc..

After this I get stuck- How to reprompt user for more values?
What is the boolean statement?
How to plot graphs on existing graphs?

Any help here would be greatly appreciated and if anything is unclear, I will explain again.

Many thanks

2. Oct 19, 2012

gabbagabbahey

I would suggest an infinite while loop with one of the following two options:

(1) at the start of your program execution you tell the user some key to press to end the program (say "X" for example) at anytime, and if you read such an input at anytime in your while loop, you exit processing with a System.exit(0); statement

(2) After each graphing operation, you ask the user if they would like to continue and prompt them to enter (Y/n). If "n" then exit the program, if yes, then go to next iteration.

Option 2 would go something like this:

Code (Text):
Scanner input = new Scanner(System.in);
while(true) {
processOmegaInput(input);
processGammaInput(input);
processPointsInput(input);
plotNewSeries(omega, gamma, points);
System.out.println("Would you like to continue? (Y/n)");
switch(input.next().toLowerCase()) {
case "y":
continue;
case "n":
System.exit(0);
default:
System.out.println("Invalid choice - Exiting program");
System.exit(0);
};
}

3. Oct 21, 2012

CAF123

What should the (boolean) condition be on the while loop? (I.e where you write while (true)) what should I write in place of true?

Should I incorporate the three cases (three cases being γ< 2ω, γ=2ω, γ>2ω) in the while loop separated by two 'else if' statements.
(to be more clearer, if the first case is not satisfied, go onto the next case ( in an else if )and then the next case (in an else if)
And then only then, after I have written all the code for how to draw the graphs for each of the three cases, do I prompt the user for new values?
I presume then if the user says yes, then the while loop gets run again (is this why we have the while (true) loop?) until the user types no?

Finally, how do I draw the graphs on top of each other? I have a piece of code in my notes about drawing graphs from multidatasets but I am not sure where this should be incorporated into the program.

Many thanks and again, if anything is unclear, I will explain.

4. Oct 21, 2012

gabbagabbahey

What's wrong with simply leaving it as while(true)? What does that do?

In my above example, you would need to define and implement 4 methods: processOmegaInput, processGammaInput, processPointsInput, and plotNewSeries. To me, the logical place to code for how to plot Series for different input values (like γ< 2ω) would be inside the plotNewSeries method. That is where I suggest you put your if statements or switch statement.

With each iteration of the while loop, the user get prompted for new values, the user input gets checked (you will need to write code to do this inside each of the 3 process___input methods, in case the user tries to tell you that Gamma="Steven Harper", for example), and then a new Series gets plotted using the values input. You want the while loop (and the program) to continue so long as the user keeps telling it to continue, and that's what the point of the switch statement in my example.

Try incorporating the code in your notes inside the plotNewSeries method and post your complete code if you get stuck.