1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple program to sketch graph in JAVA

  1. Jun 4, 2013 #1

    CAF123

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    See code below:

    3. The attempt at a solution
    I am simply trying to get the code to work: When I try to compile, the terminal says that apparantly the package ptolemy.plot.* package does not exist and so does not recognise statements like PlotFrame etc.. Why is this?

    Code (Text):
    import java.io.Console;
    import java.io.*;
    import java.lang.Math;
    import java.lang.Boolean;
    import ptolemy.plot.*;

    public class Edisc {
        public static void main (String args[]) {
       
        Console.myConsole = System.console();

        Plot disc = new Plot();
        disc.setTitle("Graph of the axial E field from a charged disc");
        disc.setXLabel("distance along z");
        disc.setYLabel("E field");

        PlotFrame myFrame = new PlotFrame("E vs z", disc);
        myFrame.setSize(800,600);

        int dataSet = 0;
       
        double q = 6.0e-9;
        double R  = 10;
        double eps = 8.85e-12;
       
       
       
        int points = 100;

        double E[] = new double[points];
        double z[] = new double[points];
       
        for(int i = 0; i < (points-1); i++) {
        z[i] = (100*(int)i/points);
       
        double Eqn = q*((2*(Math.PI())*eps*(Math.pow(R,2))))^(-1)*(1 - z[i]*((Math.sqrt((Math.pow(R,2)) + (Math.pow(z[i],2))))))^(-1);

        disc.addPoint(dataSet, z[i], Eqn, true);

        myFrame.setVisible(true);
        }

    }

    }
     
     
  2. jcsd
  3. Jun 5, 2013 #2

    CAF123

    User Avatar
    Gold Member

    The problem is that I didn't realise I had to install the package. I have downloaded the compressed zip file (ptplot5.8) however, I am having some difficulties configuring the path name.

    I have put this in the jdk file, but I am not sure where to put it from here. Are there any necessities as to where this file should go? I feel there must be since the path names I have tried still give me errors and there seems to be no help on the internet.

    Many thanks.
     
  4. Jun 5, 2013 #3

    Mark44

    Staff: Mentor

    It's been more than 15 years since I did anything with Java, so I'm not very up-to-date on this language. As a guess, I would say that the Ptolemy package should go into the same directory as your other Java files, such as the ones you're already importing.

    If you do a search on "configure java" that might bring up some information that you can use.
     
  5. Jun 5, 2013 #4

    CAF123

    User Avatar
    Gold Member

    Thanks Mark44, I tried this, but apparantly when I tried to move the file there 'File not found or no read permission'
     
  6. Jun 5, 2013 #5

    jedishrfu

    Staff: Mentor

    if the ptolemy package is in a jar file then you can run your program:

    java -cp ptolemy.jar;. Edisc

    the -cp option adds the ptolemy.jar and current directory to the classpath

    In general all my programs are part of a package so I'd suggest you do the same something like as the first line (and you need to move your program to src/my/cool/app/Edisc.java or however your code is setup):

    package my.cool.app;
     
  7. Jun 5, 2013 #6

    jedishrfu

    Staff: Mentor

    One other note on your program:

    In Java, its bad style to use caps for variables. Programmers use all caps for CONSTANTS, and CamelCase for class names with variables and methods always starting with a lowerCase letter.
     
  8. Jun 7, 2013 #7

    CAF123

    User Avatar
    Gold Member

    @jedishrfu:
    So, I have the following classpath written at runtime:
    javac -classpath .;C:\ProgramFiles\Java\jdk1.7.0_21\ptplot5.8\plot\plot.jar Edisc.java

    Can you tell me what is wrong with this? I have spent the whole morning today trying to configure this package and the compiler just won't except it. I have spoken to another person as well and they are lost too (after trying things for most of the morning)

    Many thanks.
     
  9. Jun 7, 2013 #8

    jedishrfu

    Staff: Mentor

    The first thing I see is the javac for runtime it should be just java

    java -cp .;"C:\Program Files\Java\jdk1.7.0_21\ptplot5.8\plot\plot.jar" Edisc

    the javac command is used for compiling and java for running.

    On Windows doesn't "Program Files" have an embedded space in it? This always causes problems with commandlines as they are parsed using spaces as the argument separator unless quotes are used.

    One other caveat, on Linux and other Un*x machines single quotes and double quotes have very different behaviors that you should be aware of. Basically double quotes allow you to use environment parameters within the quotes whereas single quotes doesn't.

    so for example:

    echo "Hello $USER" would echo the user account id whereas echo 'Hello $USER" would echo the string "Hello $USER"
     
    Last edited: Jun 7, 2013
  10. Jun 12, 2013 #9

    CAF123

    User Avatar
    Gold Member

    I managed to get it compiled and it is now running. One problem I am having now is that the graph is being instantaneously created and then immediately disappearing altogether.

    Can you see any indications in the code below which might be the cause of this? I put in a System.exit(0) statement because I was losing control of the terminal after running the program. However, this seems to make the graph disappear (see comment in code at end).

    Code (Text):

    import java.io.Console;
    import java.io.*;
    import java.lang.Math;
    import java.lang.Boolean;
    import ptolemy.plot.*;

    public class Edisc1 {
        public static void main (String args[]) {
       
        Console myConsole = System.console();

        Plot disc = new Plot();
        disc.setTitle("Graph of the axial E field from a charged disc");
        disc.setXLabel("distance along z");
        disc.setYLabel("E field");

        PlotFrame myFrame = new PlotFrame("E vs z", disc);
        myFrame.setSize(10,20);

        int dataSet = 0;
       
        double q = 6.0e-6;
        double R  = 10;
        double eps = 8.85e-12;
       
       
       
        int points = 500;

        double E[] = new double[points];
        double z[] = new double[points];
       
        for(int i = 0; i < (points-1); i++) {
        z[i] = (100*(int)i/points);
       
        double Eqn = (q/((2*(Math.PI)*eps*(Math.pow(R,2))))*(1 - (z[i]/(Math.sqrt((Math.pow(R,2)) + (Math.pow(z[i],2)))))));

        disc.addPoint(dataSet, z[i], Eqn, true);
       
       
        }
        System.out.println("Graph created");
        myFrame.setVisible(true);

        System.exit(0); //With this, I get no graph but access to terminal.  Without, I get graph, but no access.

       

        }

    }
     
  11. Jun 12, 2013 #10

    jedishrfu

    Staff: Mentor

    When you exit your program of course your graph will disappear.

    Why not run it as a background process with the & at the (works for macos and linux)
     
  12. Jun 12, 2013 #11

    Mark44

    Staff: Mentor

    As you note, the graph is being created, and then quickly disappears as the program ends. One way to keep this from happening is to have the program wait for some input, using System.in.read(). This will prevent the program from ending until you enter something. It doesn't matter what you enter - all you're doing is making the program wait until you hit a key.
     
  13. Jun 12, 2013 #12

    CAF123

    User Avatar
    Gold Member

    I am not sure I understand what you mean here. Could you elaborate?
    EDIT: Do you mean write for the run command: java -cp...... Edisc1 & ?

    Do you mean in place of 'System.exit(0);' I put in 'System.in.read();'? If so, I tried this and I have the compilation error: Unreported exception IOException; must be caught or declared to be thrown.

    What does this mean?
     
    Last edited: Jun 12, 2013
  14. Jun 12, 2013 #13

    jedishrfu

    Staff: Mentor

    With respect the the ampersand, yes add it to the end and in Macos or Linux it will run your app in a separate process allowing you to use the command session. For windows use the start command as in:

    start java -cp ....

    and the windows prompt should come back

    The trick Java programmers use is this code in the main method:

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    Edisc1 app = new Edisc1();
    }
    });
     
    Last edited: Jun 12, 2013
  15. Jun 12, 2013 #14

    CAF123

    User Avatar
    Gold Member

    I am familiar with what you describe regarding putting the & at the end since we use Linux in my university. However, I am using Windows at home. I tried what you describe and it doesn't appear to work on Windows. Is there any way to find out what the 'equivalent trick' is in Windows?

    Edit: I just caught your edit - I'll try it out and get back. Thanks
     
  16. Jun 12, 2013 #15

    CAF123

    User Avatar
    Gold Member

    I used the command: start java -cp.... and this created another window, (but I was unable to type in it) then...it disappears too. (as well as the graph).
    What is going on?
    (I didn't type in the code that you write in your previous post, starting from javax.swing...)
    (Maybe I should also mention that I am having no problem with regaining access to the terminal - that's fine, provided I have the system.exit. It is when I remove this that I lose access to the terminal)

    EDIT: Never mind, got it thanks!
     
    Last edited: Jun 12, 2013
  17. Jun 12, 2013 #16

    Mark44

    Staff: Mentor

    No, not in place of - before.

    What you want to do is to keep the console window open long enough to be able to look at the graph. When you're done looking, press any key.

    The code would look something like this:
    Code (Text):

    char tmp = (char) System.in.read();
     
    This statement reads all of the keys pressed until the Enter key is pressed, and will store the first character read into tmp. You don't really care about the character - the code just causes your Main function to wait for user input.
     
  18. Jun 12, 2013 #17

    jedishrfu

    Staff: Mentor

    Actually this is the old fashioned way to keep a window open, Java programmers now use the swing technique I mentioned which has the added advantage of delayed window opening to let the program initialization quiesce first.
     
  19. Jun 12, 2013 #18

    Mark44

    Staff: Mentor

    It's been a good 15+ years since I did any Java programming...
     
  20. Jun 13, 2013 #19

    jedishrfu

    Staff: Mentor

    I like that. I first learned Java at work when it first came out using Java 1.0 December of 1995.
     
    Last edited: Jun 13, 2013
  21. Jun 13, 2013 #20

    Mark44

    Staff: Mentor

    Right about then is when I was doing my Java coding. As I recall, there wasn't much in the way of debugging capabilities at the time, and there weren't any of the extension libraries, like Swing and such.

    I wrote about a half-dozen applets that ran in web pages, that created images based on user input.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Simple program to sketch graph in JAVA
  1. Java programming (Replies: 4)

  2. Java program (Replies: 3)

Loading...