Comp Sci Java Program for Damped Harmonic Oscillator Solutions | Ptplot Package

  • Thread starter Thread starter CAF123
  • Start date Start date
  • Tags Tags
    Java Program Shm
Click For Summary
The discussion focuses on creating a Java program to compute and display solutions for a damped harmonic oscillator using the ptplot package. The program requires user input for omega, gamma, and the number of points, and it should plot the amplitude versus time while allowing for multiple series to be added. Participants suggest using a while loop to continuously prompt the user for new values, with options for exiting the loop based on user input. The importance of structuring the code to handle different cases of gamma and omega relationships is emphasized, along with the need for separate methods to process input and plot data. The conversation concludes with advice on integrating code for plotting multiple datasets within the program's structure.
CAF123
Gold Member
Messages
2,918
Reaction score
87

Homework Statement


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

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
 
Physics news on Phys.org
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:
Scanner input = new Scanner(System.in);
while(true) {
    System.out.println("Please enter Omega");
    processOmegaInput(input);
    System.out.println("Please enter Gamma");
    processGammaInput(input);
    System.out.println("Please enter number of points");
    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);
    };
}
 
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.
 
CAF123 said:
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?

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

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)

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.
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?

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.

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.
Try incorporating the code in your notes inside the plotNewSeries method and post your complete code if you get stuck.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 16 ·
Replies
16
Views
13K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K