Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Plot functions using abstract methods

    Assignment 13 – Plot functions using abstract methods
    (Plot functions using abstract methods) Write an abstract class that draws the diagram for a function. The class is defined as follows:
    Java Code:
    public abstract class AbstractDrawFunction extends JPanel {
      /** Polygon to hold the points */
      private Polygon p = new Polygon();
      
    
      protected AbstractDrawFunction() {
        drawFunction();
      }
    
      /** Return the y-coordinate */
      abstract double f(double x);
    
      /** Obtain points for x-coordinates 100, 101, ..., 300 */
      public void drawFunction() {
        for (int x = -100; x <= 100; x++) {
          p.addPoint(x + 200, 200 - (int)f(x));
        }
      }
    
      @Override /** Draw axes, labels, and Graphics g) connect points */
      protected void paintComponent({
        // To be completed by you
    
      }
    }
    Test the class with the following functions:

    a. f(x) = x2;
    b. f(x) = sin(x);
    c. f(x) = cos(x);
    d. f(x) = tan(x);
    e. f(x) = cos(x) + 5sin(x);
    f. f(x) = 5cos(x) + sin(x);
    g. f(x) = log(x) + x2;

    For each function, create a class that extends the AbstractDrawFunction class and implement the f method. Figure below displays the drawing for the first three functions.



    So this is what I got so far ? I don't know how to make the jframe to work so that I displays the panel

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.LayoutManager;
    import java.awt.Polygon;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    
    public abstract class AbstractDrawFunction extends JPanel {
    	/** Polygon to hold the points */
    	private Polygon p = new Polygon();
    
    	protected AbstractDrawFunction() {
    	
    	JFrame frame = new JFrame();
    		frame.setSize(600, 600);
    		frame.setTitle("plotting");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    
    		JPanel panel = new JPanel();
    		frame.add(panel);
    		panel.setLayout(new BorderLayout());
    
    		this.setSize(900, 600);
    		this.setName("Plotting");
    		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocation(null);
    		this.setVisible(true);	
    		this.setLayout(new BorderLayout());
    
    		add(new  drawSin(){}, BorderLayout.WEST);
    
    		add(new  drawCos() {}, BorderLayout.CENTER);
    
    		add(new  drawX2() {}, BorderLayout.EAST);
    
    		drawFunction();
    
    	}
    	public static void main(String[] args){
    
    		drawSin.drawSin();
    		drawCos.drawCos();
    		drawX2.drawX2();
    	}
    
    	/** Return the y-coordinate */
    	abstract double f(double x);
    
    
    
    	/** Obtain points for x-coordinates 100, 101, ..., 300 */
    	public void drawFunction() {
    		for (int x = -100; x <= 100; x++) {
    			p.addPoint(x + 200, 200 - (int)f(x));
    		}
    	}
    
    	@Override /** Draw axes, labels, and connect points */  
    	protected void paintComponent(Graphics g) {  
    		// To be completed by you  
    		g.drawLine(10, 100, 380, 100);
    		g.drawLine(200, 30, 200, 190);
    
    		g.drawLine(380, 100, 370, 90);
    		g.drawLine(380, 100, 370, 110);
    		g.drawLine(200, 30, 190, 40);
    		g.drawLine(200, 30, 210, 40);
    
    		g.drawString("X", 360, 80);
    		g.drawString("Y", 220, 40);
    
    		g.setColor(Color.red);
    		g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
    
    	}  
    
    
    
    
    
    }
    
    
    abstract class drawSin extends AbstractDrawFunction{
    
    	public double  f(double x) {
    		return Math.sin(x);
    
    	}
    	public static Component drawSin() {
    		// TODO Auto-generated method stub
    		System.out.println("hello sin");
    
    		return null;
    
    
    	}
    
    }
    
    
    abstract class drawCos extends AbstractDrawFunction{
    	double f(double x) {
    		return Math.cos(x);
    	}
    
    	public static void drawCos() {
    		// TODO Auto-generated method stub
    		System.out.println("hello cos");
    
    
    
    	}
    
    
    }
    
    abstract class drawX2 extends AbstractDrawFunction{
    	double f(double x) {
    		return Math.getExponent(x);
    	}
    
    	public static void drawX2() {
    		// TODO Auto-generated method stub
    		System.out.println("hello x2");
    
    	}
    
    
    }
    I was just trying things out . so am I on the right path ? and does anyone know what I'm doing wrong here. I can't get the panel with the x and y to display.
    Last edited by kingslovesjava; 05-01-2014 at 05:29 AM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Plot functions using abstract methods

    Hi,

    I didn't understand how everything should work in your eyes. Your main method only calls the 3 static functions drawXYZ which only contain a single System.out.println command. So there cannot be any drawing.

    You placed the code to create a frame inside the constructor. But the constructor of your base class is also called by your super class. So when you create a new drawSin instance, it would run into the same constructor which would end in a loop. (Ok, you didn't run into this problem, because you didn't create any instance.

    Your draw-classes are all still abstract. But the only abstract method was the double f(double x) method. This is implemented so you no longer need them to be abstract.

    I would simply move the creation of the frame into the main method. The constructor just has to initialize the poligon so it should call the drawFunction and the size of the panel only.

    You could read about the layouts. I tested it on my system and I simply removed the layout but I only showed one panel.

    After you did all this and you get some first graphical results: You should check the results of your function. Sin/Cos do not give you the result that you expect. There are more required modifications.

    With kind regards,

    Konrad

  3. #3
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Plot functions using abstract methods

    so this is where I am so far


    some of them are off and I never toke trigonometry

    graph sin
    https://www.google.com/search?espv=2...ry#q=graph+sin

    graph cos
    https://www.google.com/search?espv=2...ry#q=graph+cos

    graph x^2
    https://www.google.com/search?espv=2...#q=graph+x%5E2


    graph tan
    https://www.google.com/search?espv=2...ry#q=graph+tan


    graph cos (x)+ 5sin(x)


    graph 5cos (x)+ sin(x)



    graph log
    https://www.google.com/search?espv=2...y#q=graph+log+

    so what I asking is if some one can help me fix them to display right

    Java Code:
    package plot;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Polygon;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    class AbstractDrawFunctionFrame extends JFrame {
    	public AbstractDrawFunctionFrame() {
    		setLayout(new GridLayout(3, 2, 3, 3));
    		// add(new drawSin());
    		add(new DrawX2());
    		add(new DrawSin());
    		add(new DrawCos());
    		add(new DrawTan());
    		add(new DrawCosFiveSin());
    		add(new DrawFiveCosSin());
    		add(new DrawLog());
    		// this.setLayout(new BorderLayout());
    		//add(new drawCos(), BorderLayout.CENTER);
    		//add(new drawX2(), BorderLayout.EAST);
    	}
    
    	public static void main(String[] args) {
    		AbstractDrawFunctionFrame frame = new AbstractDrawFunctionFrame();
    		frame.setSize(900, 900);
    		frame.setName("Plotting1");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }
    
    abstract class AbstractDrawFunction extends JPanel {
    	private static final boolean DrawX2 = false;
    	/** Polygon to hold the points */
    	private Polygon p3 = new Polygon();
    
    	public AbstractDrawFunction() {
    		drawFunction();
    	}
    
    	/** Return the y-coordinate */
    	abstract double f(double x);
    
    	/** Obtain points for x-coordinates 100, 101, ..., 300 */
    	public void drawFunction() {
    		
    		if(DrawX2){
    			for (int x = -250; x <= 150; x++) {
    				p3.addPoint(x + 200, 10 - (int) f(x));
    			}
    			
    	
    		}
    		for (int x = -250; x <= 150; x++) {
    			p3.addPoint(x + 200, 100 - (int) f(x));
    		}
    	}
    	
    	
    
    	@Override
    	/** Draw axes, labels, and connect points */
    	protected void paintComponent(Graphics g) {
    		// To be completed by you
    		super.paintComponent(g);
    
    		// Draw x axis
    		g.drawLine(10, 100, 380, 100);
    
    		// Draw y axis
    		g.drawLine(200, 30, 200, 190);
    
    		// Draw arrows on x axis
    		g.drawLine(380, 100, 370, 90);
    		g.drawLine(380, 100, 370, 110);
    
    		// Draw arrows on y axis
    		g.drawLine(200, 30, 190, 40);
    		g.drawLine(200, 30, 210, 40);
    
    		// Draw x, y
    		g.drawString("X", 360, 80);
    		g.drawString("Y", 220, 40);
    
    		g.setColor(Color.RED);
    		g.drawPolyline(p3.xpoints, p3.ypoints, p3.npoints);
    	}
    }
    
    class DrawSin extends AbstractDrawFunction {
    	public double f(double x) {
    		return 50 * Math.sin((x / 100.0) * 2 * Math.PI);
    	}
    }
    
    class DrawCos extends AbstractDrawFunction {
    	public double f(double x) {
    		return 50 * Math.cos((x / 100.0) * 2 * Math.PI);
    	}
    }
    
    class DrawX2 extends AbstractDrawFunction {
    	double f(double x) {
    		return x * x;
    	}
    }
    
    class DrawTan extends AbstractDrawFunction {
    	double f(double x) {
    		return 50 * Math.tan((x / 100.0) * 2 * Math.PI);
    	}
    }
    
    class DrawCosFiveSin extends AbstractDrawFunction {
    	double f(double x) {
    		return (50 * Math.cos((x / 100.0) * 2 * Math.PI)) + ((50 * Math.sin((x / 100.0) * 2 * Math.PI)));
    	}
    }
    
    class DrawFiveCosSin extends AbstractDrawFunction {
    	double f(double x) {
    		return (50 * Math.cos((x / 100.0) * 2 * Math.PI)) + ((50 * Math.sin((x / 100.0) * 2 * Math.PI)));
    	}
    }
    
    class DrawLog extends AbstractDrawFunction {
    	double f(double x) {
    		return 50 * Math.log((x / 100.0) * 2 * Math.PI)+ ((x / 100.0) * 2 * Math.PI)*((x / 100.0) * 2 * Math.PI);
    	}
    }

Similar Threads

  1. Do you have to overwrite all abstract methods?
    By Xyexs in forum New To Java
    Replies: 6
    Last Post: 08-21-2013, 10:06 PM
  2. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  4. where can I find description functions of methods
    By manhtungtnk28@gmail.com in forum AWT / Swing
    Replies: 16
    Last Post: 11-17-2009, 05:40 PM
  5. JSP functions and Java Bean methods
    By lisa.lipsky in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 07-07-2009, 07:29 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
  •