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 4056330
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:48:49+00:00 2026-05-20T14:48:49+00:00

This is for a homework problem. I’m having a heck of a time trying

  • 0

This is for a homework problem. I’m having a heck of a time trying to create xPoints and yPoints using getX() and getY() please help.

package shapes;

import java.awt.Color;
import java.awt.Graphics;

public class Triangle extends Rectangle {

    //private int[] xPoints = {50, 100, 150};
    //private int[] yPoints = {200, 100, 200};
    private int[] xPoints = {(getX()/2), getX(), (getX()+(getX()/2))};
    private int[] yPoints = {(getY()+getY()), getY(),(getY()+getY())};

    public Triangle(int x, int y, int w, int h, Color lineColor, Color fillColor, boolean fill) {
      super(x, y, w, h, lineColor, fillColor, fill);
    }  

    public void draw(Graphics g) {
        // Be nice. Save the state of the object before changing it.
        Color oldColor = g.getColor();
        if (isFill()) {
            g.setColor(getFillColor());
            g.fillPolygon(xPoints, yPoints, 3);
        }
        g.setColor(getLineColor());
        g.drawPolygon(xPoints, yPoints, 3);
        //g.drawOval(getX(), getY(), getWidth(), getHeight());
        // Set the state back when done.
        g.setColor(oldColor);
    }
    public int getArea() {
      //return area;
      return getWidth()*getHeight();
    }
    /**
    * Returns a String representing this object.
    */
    public String toString() {
        //return "Triangle: \n\tx = " + getX() + "\n\ty = " + getY() + 
               //"\n\tw = " + getWidth() + "\n\th = " + getHeight();
        return "Triangle";
    }
}

// HERE IS THE SHAPE.JAVA FILE…

package shapes;

import java.awt.Color;
import java.awt.Graphics;

public abstract class Shape {
    private int x, y;//,w,h;
    private Color lineColor;

    public Shape(int x, int y, Color lineColor) {
        this.x = x;
        this.y = y;
        this.lineColor = lineColor;
    }

    public abstract void draw(Graphics g);
    public abstract boolean containsLocation(int x, int y);


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }


    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }
}
  • 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-05-20T14:48:50+00:00Added an answer on May 20, 2026 at 2:48 pm

    First off, let’s think about what you’re trying to do in the opening line.

    public class Triangle extends Rectangle {
    

    Last I checked, a Triangle wasn’t a type of Rectangle. Perhaps you should modify this to something like

    public class Triangle extends Shape {
    

    Or something more appropriate. Look in the shapes class to find the exact one.

    Now, let’s consider what you’re doing in the class.

    private int[] xPoints = {(getX()/2), getX(), (getX()+(getX()/2))};
    private int[] yPoints = {(getY()+getY()), getY(),(getY()+getY())};
    

    Are you sure you need to define these here? I’ll bet you the Shape class already has arrays for xPoints and yPoints. Look in there for inspiration.

    Also, if Shape DOES have xPoints and yPoints, and they’re trying to use them, they won’t be able to! Because you’re defining Triangle.xPoints, Shape.xPoints will get “shadowed”… meaning xPoints in Shape will point to it’s own xPoints, not the one you defined, resulting in a very quick NullPointerException.

    Now, with a Rectangle, it’s easy to define it as X,Y + W,H. It looks like this:

    X,Y       X+W,Y
    +---------+
    |         |
    |         |
    +---------+
    X,Y+H    X+W,Y+H
    

    Now how are you going to represent your Triangle like that? Looks like you need to put some thought into how to represent your Triangle. Maybe there’s some information in the Shape class that can help you here too.

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

Sidebar

Related Questions

Disclaimer: this is a (frustrating) homework related problem. I'm having odd results when I
I have this homework problem where I need to use regex to remove every
The answer to this sample homework problem is "1,000,000", but I do not understand
This is a homework problem that I have. I have been doing some research
This is a homework problem - so I would appreciate if you could tell
This is not a homework problem, though it is a work problem. Where months
This is a homework problem. Let A[] is an array of integers and integer
This is a homework problem. I want to write a function to convert a
The following is a homework problem for an assembly language class. We're to create
I am stumped on this homework problem. I think I have the right answer

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.