# Thread: Plot functions using abstract methods

1. Member Join Date
Mar 2014
Posts
3
Rep Power
0

## 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();
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());

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 06:29 AM.  Reply With Quote

2. Senior Member Join Date
Feb 2014
Posts
447
Rep Power
7

## 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,  Reply With Quote

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

## 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

graph cos

graph x^2

graph tan

graph cos (x)+ 5sin(x)

graph 5cos (x)+ sin(x)

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));
// this.setLayout(new BorderLayout());
}

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);
}
}```  Reply With Quote

abstract classes, java, jframe, plot, swing 