0

I am working on a Fluid Mechanics problem and I decided to solve the problem using Java instead of excel because I just wanted the experience. I have been stuck on this one part for the past 4 hours though. I have an array I made in one class and I need to get all that data over to another class so that I can graph it. At this point I am really just looking for the answer. My mind is shot right now and I just want to get this done with.

I have found others with similar problems, but I can not make sense of the solutions that they were given and right now my brain is fried, which is why I would prefer if someone were able to just show me exactly what to type, but any help is appreciated.

public class Eight_c {

public static void main(String [] args) {
    Eight_c ball = new Eight_c();

    ...

    int count = 0;
    double[] y = new double[101];
    double[] x = new double[101];


     // Calculates the Distance the ball has traveled using increments of .1s
     for(double t = 0; t<=time; t=t+.1) {

         y[count] = t;
         x[count] = d;

        V1 = ball.Velocity(a, V2, dt);
        Fd = ball.Drag_Force(V2);
        a = ball.Acceleration(Fd, m);
        d = ball.Distance(V2, a, dt) + d;
    ...

Above is where I have created the two arrays, x and y.

Below is where they need to go.

public class Graph {

public static void main(String [] args) {

    double [] x;
    double [] y;

    // create your PlotPanel (you can use it as a JPanel)
      Plot2DPanel plot = new Plot2DPanel();

      // add a line plot to the PlotPanel
      plot.addLinePlot("Distance vs Time", x, y);

      // put the PlotPanel in a JFrame, as a JPanel
      JFrame frame = new JFrame("a plot panel");
      frame.setContentPane(plot);
      frame.setVisible(true);
    }
}
3
  • You can only have one main in a java program. Commented Sep 7, 2014 at 21:43
  • Try BlueJ for starting with Java. Commented Sep 7, 2014 at 21:47
  • @EbbeM.Pedersen You can have any number of classes with a main method. But, you could argue that you have that many programs. Unless there is something overly creative going on, a program starts with one and only it will be run. Commented Sep 7, 2014 at 21:51

1 Answer 1

1

main() is the entry point of a program. You can't have 2 entry points. So, you need the main method of the first class to call the method of the second class, and give it x and y as arguments:

public class Graph {

    public void render(double[] x, double[] y) {

        // create your PlotPanel (you can use it as a JPanel)
        Plot2DPanel plot = new Plot2DPanel();

        // add a line plot to the PlotPanel
        plot.addLinePlot("Distance vs Time", x, y);

        // put the PlotPanel in a JFrame, as a JPanel
        JFrame frame = new JFrame("a plot panel");
        frame.setContentPane(plot);
        frame.setVisible(true);
    }
}

and in the main method of Eight_c:

// create a Graph object:
Graph graph = new Graph();

// ask it to render x and y:
graph.render(x, y);

I find it quite strange to start using Swing if you don't know what methods and objects are, and how to pass arguments to methods. That's a bit like trying to fly an Airbus when you have not learnt how to walk yet.

Read an introductory book about Java and programming in general. Exercise and practice with simple, console-based programs. Then only start using Swing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Thank you, Thank you. I also do know objects, classes and passing arguments, not as well as I should, but I am taking a java course on my campus. I'm also using swing because I found the code for class Graph online and it happened to work exactly like I needed it to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.