Question

This week we want to build an inheritance hierarchy using targets. You now need to take...

This week we want to build an inheritance hierarchy using targets. You now need to take the Target class we have built and create a subclass that inherits from it. You can do whatever you want here, but it should be obvious how you changed the target. Some examples might include adding an additional ellipse to the target to increase the number of levels or changing up the colors. Once you have come up with one way to change the target, do a second. This should also be a subclass of the Target class we made above and the change made should be obvious. In total, you should have three classes to be submitted, the one we did together and the one you built.

//CODE THAT WAS PROVIDED

// --------------- imports ------------------------------

import wheelsunh.users.*;

import java.awt.Color;

public class Target extends ShapeGroup
{
    //---------------- instance variables ------------------------------
    // local "constant" variables define the locations of the inner
    //   circles relative to the level1 circle.
    protected final int level2X = 15, level2Y = 15;
    protected final int level3X = 25, level3Y = 25;
    protected final int level4X = 30, level4Y = 30;

    // local "constant" variables define the sizes of all circles
    protected final int level1Size = 80;
    protected final int level2Size = 50;
    protected final int level3Size = 30;
    protected final int level4Size = 20;

    // other local variables are used to references  Wheels objects
    // used to draw the target.
    protected Ellipse level1;
    protected Ellipse level2;
    protected Ellipse level3;
    protected Ellipse level4;
    // -----------------------------------------------------------------

    /**
     * Constructor for the TargetApp class.
     */
    public Target()
    {
        super();
        this.makeTarget( 0, 0 );
    }
    ////////////////////////////////////////////////////////////////////
    /**
     * 2 parameter constructor goes here:
     */
    // Write constructor with position parameters (2 ints)
    public Target( int x, int y)
    {
        super();
        this.makeTarget( x, y );
    }


    ////////////////////////////////////////////////////////////////////
    // -----------------------------------------------------------------

    /*
     * Notice that this method is commented out, that is intentional.  Since ShapeGroup has a setLocation method
     * that will move our shapes for us, we do not need to write one ourselves.  We will need to connect our shapes to
     * the ShapeGroup for this to work though.  That will happen down in makeTarget.
     */

    /**
     * setLocation( int x, int y ).
     * change the location of the target;
     * need to invoke setLocation on  4 ellipses that compose target.
     *
     * @param x int
     * @param y int
     */
//    public void setLocation( int x, int y ) {
//        /////////////////////////////////////////////////////////
//        // setLocation code here
//
//
//        ////////////////////////////////////////////////////////
//    }

    // -----------------------------------------------------------------

    
    /**
     * move( int dx, int dy ).
     * move the location of the target by dx and dy
     * newx = oldx + dx
     * newy = oldy + dy
     * use Target's setLocation method to actually change the location
     *
     * @param dx int
     * @param dy int
     */
    public void move( int dx, int dy ) {
        /////////////////////////////////////////////////////////
        // move code here
        super.setLocation( dx + super.getXLocation(), dy + super.getYLocation() );

        ////////////////////////////////////////////////////////
    }

    // -----------------------------------------------------------------
    
    /**
     * makeTarget.
     * encapsulates all the Wheels components needed to draw a target.
     *
     * @param x int
     * @param y int
     */
    public void makeTarget( int x, int y ) {
        // create the level1 circle
        level1 = new Ellipse( x, y );
        level1.setSize( level1Size, level1Size );
        super.add( level1 );

        // create the next level4 circle
        level2 = new Ellipse( x + level2X, y + level2Y );
        level2.setSize( level2Size, level2Size );
        level2.setColor( Color.BLUE );
        super.add( level2 );

        // create the next level4 circle
        level3 = new Ellipse( x + level3X, y + level3Y );
        level3.setSize( level3Size, level3Size );
        level3.setColor( Color.CYAN );
        super.add( level3 );

        // create the level4 circle
        level4 = new Ellipse( x + level4X, y + level4Y );
        level4.setColor( Color.BLACK );
        level4.setSize( level4Size, level4Size );
        super.add( level4 );
    }

    // -----------------------------------------------------------------

    /**
     * main program just invokes the class constructor.
     *
     * @param args String
     */
    public static void main( String[] args ) {
        Frame f = new Frame();
        Target t1 = new Target();
        t1.setLocation( 100, 100 ); //Notice here even though we do not have a setLocation, we can call the parent one

        ////////////////////////////////////////////////////////////////
        // Add much more Target creation code here
        //
        Utilities.sleep( 1000 );
        t1.move(100, 100);
        Utilities.sleep( 1000 );
        t1.move(100, 100);
        Utilities.sleep( 1000 );
        t1.move(100, 100);

        ////////////////////////////////////////////////////////////////
    }

} //End of Class TargetApp
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class TargetSubclass extends Target {
  
  
   protected final int level5X = 35, level5Y = 35;
   protected final int level5Size = 25;
   protected Ellipse level5;

   /*All the other methods and variables are inherited.We can overload the methods here if need be and provide
   * alternate implementations of the same.
   */
}

public class TargetSubclass2 extends Target{
  
   protected final int level6X = 35, level6Y = 35;
   protected final int level6Size = 29;
   protected Ellipse level6;

   /*All the other methods and variables are inherited.We can overload the methods here if need be and provide
   * alternate implementations of the same.
   */


}

Add a comment
Know the answer?
Add Answer to:
This week we want to build an inheritance hierarchy using targets. You now need to take...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a derived class from Organism. You must complete the constructors, and the method definitions that were...

    a derived class from Organism. You must complete the constructors, and the method definitions that were abstract methods in Organism. /** Organism.java * Definition for the Organism base class. * Each organism has a reference back to the World object so it can move * itself about in the world. */ public abstract class Organism {    protected int x, y;       // Position in the world    protected boolean moved;   // boolean to indicate if moved this turn   ...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle....

    In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle. Review Lab 5 before completing this lab and retrieve the MyRectangle class that you completed for this lab, since you will need it again in this lab. Before starting this lab, edit your MyRectangle class in the following way: • change the declaration of your instance variables from private to protected This will enable access of these variables by your MySquare subclass. Here is...

  • The assignment is to take the current program that draws a line and to add 4...

    The assignment is to take the current program that draws a line and to add 4 buttons at the bottom of the frame that will change the line drawn to a different color (ie. No button pressed draws a black line, Button labeled Red changes any new lines drawn to red and retains any previously colored lines, etc for all the buttons). This is the code I have so far but I'm having issues with the buttons. This is also...

  • Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you w...

    Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you will build the below hierarchy: Overview: You will implement the supplier code: Shape, Square, TSquare, and Triangle, which will be used by the Homework7Main program. This program will use the DrawingPanel to draw these objects onto a canvas. Specification: You will take advantage of inheritance with using a super class, Shape.java. (below) Shape is an abstract class, which means it can be extended, but...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

  • Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts...

    Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts an int parameter and changes the color based on that int. The valid colors are "red", "yellow", "green", "blue", "magenta" and "black". In your code, map each color to an integer (e.g. in my code 3 means green.) If the number passed to the method is not valid, change the color to red. In the bounceTheBall() method, where you test for collisions with top...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • Write a program that will define a runnable frame to have a ball move across the...

    Write a program that will define a runnable frame to have a ball move across the frame left to right. While that is happening, have another frame that will let the user do something like click a button or input into a textfield, but keep the ball moving in the other frame. When the ball reaches the right edge of the frame or when the user takes action - enters in textfield or clicks the button, end the thread. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT