• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused and don't know how approach Plot functions using abstract methods  RSS feed

 
Buda young
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
last Assignment of the semester and I'm just confused on how to approach project

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
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...).
 
Buda young
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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.
 
Buda young
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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.



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





 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. You need to define a new class for each of the mathematical functions, and each class needs to extend the class your teacher provided.

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.
 
Buda young
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is what I got so far ? I dont know how to make the jframe to work

 
Buda young
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i fix DrawLog to draw it out correct

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What isn't it doing right? Is there an error? Does it just not look the way you expected?
 
Buda young
Greenhorn
Posts: 19
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So which ones look wrong, and how do the graphs differ from what you expect?

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!