Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Greenhorn
Posts: 19
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.

Bartender
Posts: 689
17
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

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.

Bartender
Posts: 2166
46
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

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
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
So this is what I got so far ? I dont know how to make the jframe to work

Buda young
Greenhorn
Posts: 19
How do i fix DrawLog to draw it out correct

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

Buda young
Greenhorn
Posts: 19

Bartender
Posts: 10575
66

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

Mike. J. Thompson
Bartender
Posts: 689
17
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.

 When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse? Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss