Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Fubarable

Thread: Java Project

  1. #1
    sam114 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Java Project

    Hi Everyone!

    I am new to java and having a hard time learning it. Can anyone please help me with this question?

    This question consists of making a class which could take in a M x 3 array of data (first column is the x-data, second column is the first set of y-data, and the third column is the second set of y-data).
    Also please note that there should be a method in the class which allows for the plotting of data.

    Below are the plot requirements:
    1. It should show "Calculated" results plotted in black lines from point to point
    2. It should consist Theoretical results plotted using red lines from point to point
    3. X and Y axes plotted. Make the axes thicker than the data lines
    4. For every 50 segments, display the x-values on the x axis, and the y values on the y-axis
    Hints:
    1. Think about mathematical relationships that could be useful for plotting based on the x,y locations on a frame
    2. Create a test suit to check and confirm your results.
    3. Look for objects and methods that exist in Java for required plotting

    Thanks a lot!
    :)
    Last edited by sam114; 08-26-2012 at 05:12 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Project

    If you are in true need of our help, please consider putting in the effort to post a complete question, one that doesn't require folks to go off to a link. Your efforts will be appreciated and will likely result in getting better and quicker answers.

    Also, rather than posting just requirements, you will want to post what you've tried so far to solve the assignment and any *specific* questions you may have. Usually just posting an assignment without showing your efforts will only allow us to post very general recommendations such as links to tutorials while specific questions will often get more specific and more helpful answers.
    Last edited by Fubarable; 08-26-2012 at 05:11 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Project

    Quote Originally Posted by sam114 View Post
    Hi Everyone!

    I am new to java and having a hard time learning it. Can anyone please help me with this question?
    Thanks for the update. Now to help you better, please let us know what you've done so far, and what part confuses you. Again, the more you can tell us about your problem, usually the better we can help you.

  4. #4
    sam114 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java Project

    Quote Originally Posted by Fubarable View Post
    Thanks for the update. Now to help you better, please let us know what you've done so far, and what part confuses you. Again, the more you can tell us about your problem, usually the better we can help you.
    Thanks,
    First I am having trouble in understand the question.
    This is what I understand, correct me if I am wrong please
    a) I should have some random data as x, y1,y2
    b) and then I need a way to plot these data in a graph. But how can we plot y1 and y2 data at the same time.
    c) Continued from b) so does this mean that the Calculated data can be taken as y1 and the Theoretical data can be considered as y2 ?
    Thanks again!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Project

    Quote Originally Posted by sam114 View Post
    Thanks,
    First I am having trouble in understand the question.
    This is what I understand, correct me if I am wrong please
    a) I should have some random data as x, y1,y2
    You should have some data I think with which to test your code. I don't see that it is specified as random though, and it is conceivable that your instructor will use a different set of data to test your code as well. Perhaps your code can be written to accept any matrix of data that conforms to certain rules, such as a 2-dimensional array of positive ints. I imagine that the x data would be monotonically increasing and uniformly spaced such as 5, 10, 15, 20, 25, 30, 35, 40,...

    b) and then I need a way to plot these data in a graph. But how can we plot y1 and y2 data at the same time.
    You would likely draw them in your JPanel's paintComponent method (if this is Swing), and would loop through each column of data to draw each point and connecting line. So I would loop through the y1 data and draw its plot (also using the x data to know where to put the points in the x dimension), then do the same with the y2 data column.

    c) Continued from b) so does this mean that the Calculated data can be taken as y1 and the Theoretical data can be considered as y2 ?
    Possibly. Per my reading of the instructions, this does not appear to be clear. Perhaps you can get clarification from your instructor?

  6. #6
    sam114 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java Project

    Thanks again. Can you please tell me if this code is okay as for the x data points and both y1,y2 data points in one plot?
    package verticalplot;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class VerticalPlot
    {
    public VerticalPlot()
    {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new VerticalPlotPanel());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }

    public static void main(String[] args)
    {
    new VerticalPlot();
    }
    }

    class VerticalPlotPanel extends JPanel
    {
    final int PAD = 20;

    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    int w = getWidth();
    int h = getHeight();

    // abcissas- bottom
    g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
    // labels
    Font font = getFont().deriveFont(14f);
    g2.setFont(font);
    FontRenderContext frc = g2.getFontRenderContext();
    String[] s = { "1", "5", "10", "15", "20" };
    for(int j = 0; j < s.length; j++)
    {
    float width = (float)font.getStringBounds(s[j], frc).getWidth();
    float height = font.getLineMetrics(s[j], frc).getAscent();
    float sx = PAD + j *(w - 2*PAD)/(s.length-1) - width/2;
    float sy = h - PAD + height + 4;
    g2.drawString(s[j], sx, sy);
    }
    // middle
    double y = PAD + (h-2*PAD)*2/3;
    g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
    // top
    y = PAD + (h-2*PAD)/3;
    g2.draw(new Line2D.Double(PAD, y, w-PAD, y));

    // ordinate
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
    // labels - top
    double vSpan = (h-4*PAD)/3;
    s = new String[] { "20", "10", "0", };
    double yInc = vSpan/(s.length-1);
    for(int j = 0; j < s.length; j++)
    {
    float width = (float)font.getStringBounds(s[j], frc).getWidth();
    float height = font.getLineMetrics(s[j], frc).getAscent();
    float sx = PAD - width - 2;
    float sy = (float)(PAD + j * yInc + height);
    g2.drawString(s[j], sx, sy);
    }
    // middle
    s = new String[] { "3", "2", "1" };
    y = PAD + (h-2*PAD)/3;
    for(int j = 0; j < s.length; j++)
    {
    float width = (float)font.getStringBounds(s[j], frc).getWidth();
    float height = font.getLineMetrics(s[j], frc).getAscent();
    float sx = PAD - width - 2;
    float sy = (float)(y + j * yInc + height);
    g2.drawString(s[j], sx, sy);
    }
    // bottom
    s = new String[] { "8", "6", "4", "2" };
    y = PAD + (h-2*PAD)*2/3;
    yInc = vSpan/(s.length-1);
    for(int j = 0; j < s.length; j++)
    {
    float width = (float)font.getStringBounds(s[j], frc).getWidth();
    float height = font.getLineMetrics(s[j], frc).getAscent();
    float sx = PAD - width - 2;
    float sy = (float)(y + j * yInc + height);
    g2.drawString(s[j], sx, sy);
    }

    // draw lines - bottom
    vSpan = (h-2*PAD)/3;
    g2.setPaint(Color.red);
    double x1 = PAD, y1 = h-PAD, x2 = PAD + (w-2*PAD)/2, y2 = y1 - vSpan;
    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    // middle
    g2.setPaint(Color.green.darker());
    y1 = h-PAD - 2*vSpan; x2 = w-PAD; y2 = y1 + 0.8 * vSpan;
    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    // top
    g2.setPaint(Color.blue);
    y1 = PAD + vSpan/2; y2 = PAD + vSpan;
    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    }
    }

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: Java Project

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Project

    Quote Originally Posted by sam114 View Post
    Thanks again. Can you please tell me if this code is okay as for the x data points and both y1,y2 data points in one plot?
    .....
    I can't really tell. I suggest that you break down the large method into smaller methods with logical names to make your code easier to understand, and that you use code tags (please see Darryl's links) when you post the new code. Is this code you've created or code you've found?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: Java Project

    Quote Originally Posted by Fubarable View Post
    Is this code you've created or code you've found?
    I can answer that.
    How to draw a chart with multiple y-axises (Swing / AWT / SWT forum at JavaRanch)

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Project

    Quote Originally Posted by DarrylBurke View Post
    Thanks Darryl.

    Then Sam your job is to scrap that code entirely. Use the concepts found in it, for sure, but you need to write your code from scratch using these concepts if you're going to make headway at all. And please don't ask us to help debug code that is not yours.

  11. #11
    sam114 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java Project

    Quote Originally Posted by Fubarable View Post
    Thanks Darryl.

    Then Sam your job is to scrap that code entirely. Use the concepts found in it, for sure, but you need to write your code from scratch using these concepts if you're going to make headway at all. And please don't ask us to help debug code that is not yours.
    Yes that's not my work.(I never said it was mine) What I was asking is, if that code was on okay code as for the x, y1, y2 thing I was talking about in the assignment. Since I dont even understand if that code is what I am looking for as I am new to java.
    Last edited by sam114; 08-26-2012 at 09:00 PM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Project

    Quote Originally Posted by sam114 View Post
    Yes that's not my work.(I never said it was mine) What I was asking is, if that code was on okay code as for the x, y1, y2 thing I was talking about in the assignment. Since I dont even understand if that code is what I am looking for as I am new to java.
    The goal of the assignment is for you to gain an understanding of Java and to use it in your code, and that's where I think you should start. The Swing tutorials have great resources to help you with this and they can be found here: Using Swing Components
    DarrylBurke likes this.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: Java Project

    Quote Originally Posted by sam114 View Post
    randomNumber = generator.nextDouble();
    if(randomNumber <= prob){
    N0=N0-1;
    But how should I write the code for part b ; (i tried this
    if(randomNumber > prob){
    N0=1/N0;
    }
    If you don't bother to go through links posted to help you, you're not likely to be offered further help.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Creating a project in eclipse from existing project
    By Suraiya in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 09:14 AM
  2. JAVA Project
    By kappasig84 in forum New To Java
    Replies: 6
    Last Post: 12-13-2009, 09:28 PM
  3. Java Project
    By Smirre in forum New To Java
    Replies: 16
    Last Post: 11-17-2008, 08:37 PM
  4. how to get the project in java.....
    By thirumurugan.sethu in forum New To Java
    Replies: 2
    Last Post: 10-07-2008, 07:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •