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:

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.

Mike. J. Thompson wrote:What is it you don't get?

You've been told to implement classes that evaluate those mathematical functions. Each of those classes must inherit from the class supplied by your tutor, and implement the abstract method f(...).

Ok I know I have to draw lines on a x and y grid but I'm really not sure how. we just went over abstract methods yesterday and we never went over graphics so I still don't fully understand abstract methods and how to draw on in a swing component.

A class with at least one abstract method must be abstract. An abstract class can't be instantiated.

Any class extending abstract class must either

- be abstract itself

- provide implementation for all abstract methods.

So, you have to make a subclass of

`AbstractDrawFunction`class and then write method

`double f(double x)`with actual body.

So, you have to draw sin(x). So just write a method that returns a sine of a number(

`Math.sin(double)`- but you might want to scale it a little).

Then you should write somethig in method

`paintComponent(Graphics g)`. This method is called when the component is drawn.

As it is in your abstract class it should contain general code for plotting any of your functions.

What information do you have available inside this method?

You have a

`Polygon`and

`Graphic`.

`Graphic`is a class with many methods that can draw and paint things on components. If only it contained a method that could draw polygons.

A quick look at the API and voilà! It does have one.

It is

`drawPolygon(Polygon p)`.

But there is a little problem with that one. It connects the last points with the first one.

So the graph looks like this:

But wait! There is another method with similiar name that will not connect the last point to the first one! Unfortunately it doesn't take an instance of

`Polygon`, but it takes something you can get from the

`Polygon`.

Can you find it? Take a look here.

Pawel Pawlowicz wrote:Abstract method is a really simple thing. It's just a method without an implementation.

A class with at least one abstract method must be abstract. An abstract class can't be instantiated.

Any class extending abstract class must either

- be abstract itself

- provide implementation for all abstract methods.

So, you have to make a subclass ofAbstractDrawFunctionclass and then write methoddouble f(double x)with actual body.

So, you have to draw sin(x). So just write a method that returns a sine of a number(Math.sin(double)- but you might want to scale it a little).

Then you should write somethig in methodpaintComponent(Graphics g). This method is called when the component is drawn.

As it is in your abstract class it should contain general code for plotting any of your functions.

What information do you have available inside this method?

You have aPolygonandGraphic.

Graphicis a class with many methods that can draw and paint things on components. If only it contained a method that could draw polygons.

A quick look at the API and voilà! It does have one.

It isdrawPolygon(Polygon p).

But there is a little problem with that one. It connects the last points with the first one.

So the graph looks like this:

But wait! There is another method with similiar name that will not connect the last point to the first one! Unfortunately it doesn't take an instance ofPolygon, but it takes something you can get from thePolygon.

Can you find it? Take a look here.

ok ok this is what I got out of what you told me also

The classes you write should only need one method, f(double d). That method will calculate the function. name each of your classes after the function it implements.

Also, there is a method in the class your teacher provided that you need to add the implementation for. This method needs to draw the polygon, and you have already been given strong hints how to do that.

Mike. J. Thompson wrote:What isn't it doing right? Is there an error? Does it just not look the way you expected?

exactly some of them don't look the like the ones on google I never toke trigonometry

graph sin

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+sin

graph cos

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+cos

graph x^2

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+x%5E2

graph tan

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+tan

graph cos (x)+ 5sin(x)

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+cos+(x)%2B+5sin(x)

graph 5cos (x)+ sin(x)

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+5cos+(x)%2B+sin(x)

graph log

https://www.google.com/search?espv=210&gs_ivs=1&q=trigonometry#q=graph+log+

Buda young wrote:exactly some of them don't look the like the ones on google I never toke trigonometry

So, what DO they look like? They could be a lot flatter or more pronounced depending on the scales you're using.

Do you actually

__know__that they're wrong, or do you just suspect it? I presume you're not getting errors because you haven't answered Mike's 2nd question with a "yes".

But primarily, I think your basic problem is that you've written

*far too much code*without

__testing__it.

Back up. Take one function (I'd suggest

`sin`, because it's probably the most basic one), and test THAT. And don't even

*think*about writing code for another one until you KNOW that it works.

*Every time*.

HIH

Winston

PS: I broke up a couple of your code lines because they are far too long, and it makes your thread difficult to read. Please read the link. Thanks.

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Do you know what radians are and how they relate to degrees?

Look at the values you're returning in your f() methods and ask yourself why are are returning what you are, and whether that meets the requirements.

And as Winston said, do this one function at a time until you get it right.