Java Program for Damped Harmonic Oscillator Solutions | Ptplot Package

  • Context: Comp Sci 
  • Thread starter Thread starter CAF123
  • Start date Start date
  • Tags Tags
    Java Program Shm
Click For Summary

Discussion Overview

The discussion revolves around creating a Java program to compute and display solutions for a damped harmonic oscillator using the ptplot package. Participants are exploring how to implement user input for parameters, manage program loops, and plot multiple series on the same graph.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant outlines the requirements for the program, including reading user input for omega, gamma, and the number of points, and plotting the results.
  • Another participant suggests using an infinite while loop with options for the user to continue or exit the program, providing two methods for user interaction.
  • Questions arise about the appropriate boolean condition for the while loop, with some participants considering whether to use 'while (true)' or a more specific condition.
  • There is discussion on how to structure the program to handle different cases based on the relationship between gamma and omega, with suggestions to use if-else statements for each case.
  • Participants express uncertainty about how to incorporate graph plotting for multiple datasets and where to place this logic within the program structure.

Areas of Agreement / Disagreement

Participants generally agree on the need for a loop to handle user input and plotting but have differing views on the specifics of the loop condition and structure. The discussion remains unresolved regarding the best approach to implement the program logic and handle user interactions.

Contextual Notes

Participants mention the need for input validation and the potential for user errors, such as entering non-numeric values for gamma. There is also a focus on how to manage plotting multiple series on the same graph, which may depend on the specifics of the ptplot package.

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
2K
  • · Replies 16 ·
Replies
16
Views
13K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K