Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8356113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:05:08+00:00 2026-06-09T10:05:08+00:00

This is a exercise that i found in a book on java. I am

  • 0

This is a exercise that i found in a book on java. I am not able to solve it.

 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));
     }
    }
    /** Implement paintComponent to draw axes, labels, and
    * connecting points
    */
    protected void paintComponent(Graphics g) {
    // To be completed by you
     }
    }

Test the class with the following functions:
f(x) = x2;
f(x) = sin(x);
f(x) = cos(x);
f(x) = tan(x);
f(x) = cos(x) + 5sin(x);
f(x) = 5cos(x) + sin(x);
f(x) = log(x) + x2;

For each function, create a class that extends the AbstractDrawFunction
class and implements the f method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:05:12+00:00Added an answer on June 9, 2026 at 10:05 am

    Implementing the subclasses is the easy part. Simply extend the class and implement the method; I suppose you already know and understand how to do this. If not, look up “derived classes” in your book.

    The paintComponent part is a little harder, but only if it assumes that you scale the function to scale. It looks like the method should draw the graph in a 200 x 200 window, with the function ranging from -100 to 100. So no scaling, but you won’t see much of your sine and cosine functions.
    The fact that you don’t have to scale also means that drawing the axes is easy; note that the coordinate system runs from -100 to 100, that should give you enough clues.

    Take care with tan! It is not defined for all the input values. The same holds for one of the other functions, and that’s probably why they’re in the exercise.

    There is a little pitfall in that the method uses Polygon. A Polygon can simple be drawn with a call to Graphics.drawPolygon, but that method will close it: the last point will be connected with the first.
    There are some workarounds for this, like adding extra points and forcing that extra line to be drawn exactly over an axis. But they won’t work for all formulae, and I don’t think it’s what you’re supposed to be working on. The exercise probably just uses Polygon so you can call drawPolygon for the actual rendering.

    To add the JPanel to the JFrame, use JFrame.add( subclass ) or JFrame.setContentPane( subclass ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading 'the art of prolog' book and I found an exercise that reads
I am doing some exercises in my object-oriented javascript book, I notice that this:
This is a thought exercise, not a particular problem, but I'd like to hear
I am studying the SCJP, and while studying I have found an exercise that
This is an interview question that I am using as a programming exercise. Input:
I searched the site but it seems like this exercise from the book Learn
I decided to look a bit into python. I found this book began to
I am working on an exercise based on this image. I have found the
I am doing currently a sample exercise and i found one weird observation that
This is a mental excercise that has been bothering me for a while. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.