Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! I’m working on a Java exercise which uses polymorphism and inheritance so th

ID: 3671677 • Letter: H

Question

Hi! I’m working on a Java exercise which uses polymorphism and inheritance so that I can use an external JAR file called BlobZX.JAR and start creating polygons for an asteroid game, however I can’t seem to understand how to approach it and what I might be doing wrong. The JAR file contains a number of classes like Blob, PolyBlob, and SandBox (a configured JFrame), and the PolyBlob class extends the Blob class. I’ve been working on creating a class called Asteroid which extends PolyBlob so that I can create asteroids that have different properties like a different speed, size, shape, and location, but I’m not sure how to change some things like the number of sides or the vertices and angles. I also have created a class called TestDriver to use the asteroid class and create 20 asteroids, but I’m changing the rotation to be between -.1 and .1 and changing the velocity for x and y to be between -3 and 3. Thank you so much to anyone who can help! I’m including the JAR file, zip file, the exercise's instructions, and an image of what the outcome is supposed to look like to help show what I’m trying to use. This is my code so far:

package asteroid;
//importing to get the .java files of blob, polyblob, and sandbox
import blobzx.*;
import java.util.*;

//creating asteroid class
public class Asteroid extends PolyBlob{

private static Random random = new Random();

public Asteroid( int idx, int jdx, double rot ){

//Constructing a polyblob for flow mode with input velocity and rotation values
super( -100, -100, rot );
//setting motion vector for the polyblob
setDelta(idx, jdx);

//set the number of sides to be between 5 and 9
int numberSides = random.nextInt(4) + 5;

//change the vertex distance from the origin to be between 5 and 15
int distance1 = random.nextInt(10) + 5;
int distance2 = random.nextInt(10) + 5;
setLoc(distance1, distance2);

//set diameter to be between 10 and 30 pixels
int asteroidSize = random.nextInt(20) + 10;
setSize(asteroidSize);

//choosing angle for each vertex by diving 2pi by the number of sides for region
//then choosing a random angle for each region
double vertexAngleRegion = numberSides / (Math.PI * 2);
double angle[ i ] = (i * vertexAngleRegion) + (Math.random() * vertexAngleRegion);

//next I want to get the relative x and y coordinates for each vertex and their angle
//by using BlobUtils.rotatePoint(int distance, double angle)
//and then I want to stuff all my results into x and y arrays to use for setPolygon(int[]x, int[]y)
}
}
//creating test driver class which contains the main method
public class TestDriver extends Asteroid{

private static Random random = new Random();

public static void main(String[] args) {
//setting sandbox to have flow mode with 15 frames per second
Sandbox sb = new Sandbox();
setSandBoxMode(SandBoxMode.FLOW);
setFrameRate(66);

//using a for loop to create 20 asteroids
for (i=0; i<20; i++){

//setting asteroid velocity to be between -3 and 3
int idx = random.nextInt(6) - 3;

//generating a new random number if idx is 0 to keep idx from being 0
if(idx =0){
int idx = random.nextInt(6) - 3;
}
int idy = idx;

//adding each asteroid
Asteroid a1 = new Asteroid (x, y, idx, idy);
sb.addAsteroid(a1);
SandBox.simulate( sb );
}
}
}

https://webcourses.ucf.edu/courses/1165884/files/51118295/download?wrap=1

https://webcourses.ucf.edu/courses/1165884/files/51118294/download?wrap=1

Instructions:

1. Create an Asteroid.java class file in your project for your program. This class must satisfy the following requirements:

(a) it must extend the PolyBlob class, so that it inherits from PolyBlob, which in turn inherits from Blob. The class will will have only a constructor and no other methods.

(b) the constructor must take these three input parameters as arguments: (i) an int that represents the x-component of the asteroid's velocity vector; (ii) an int that represents the y-component of the asteroid's velocity vector; and (iii) a double that represents the angular rotation rate.

(c) the constructor must set the asteroid to start at the offscreen location (-100, -100), since we will be using "flow" mode, as discussed in lecture.

(d) the constructor must also initialize the asteroid's velocity vector with the velocity component values that the constructor receives as input.

(e) the constructor must create a random simple polygon (no lines crossing) shape for the asteroid that has between 5 and 9 sides and is between 10 and 30 pixels in diameter, as discussed in lecture. When displayed, the shape must not have any lines that cross.

2. Create a separate AsteroidField.java test driver class to create a field of asteroids. This class must satisfy the following requirements:

(a) it must implement the BlobGUI interface, as explained in lecture.

(b) it must have a one-line main() method that instantiates the class, as follows:


Public static void main( String[ ] args ) {

New AsteroidField();

}


(c) the constructor for the class must perform the following actions:

(i) create a sandbox object;

(ii) set the sandbox to "flow" mode;

(iii) set the sandbox to run at 15 frames per second; and

(iv) initialize the sandbox by passing "this" (the AsteroidField object) to the sandbox's init() method.

(d) the class must contain a generate() method, which is required by the BlobGUI interface. The generate() method must perform the following actions:

(i) it must create 20 asteroids using the velocity components and rotational values described here;

(ii) it must randomly choose x and y velocity components for each asteroid, where the x and y components are chosen independently of each other and where each of these values is an integer that may range from -3 to +3, but where zero values are disallowed, all as discussed in lecture;

(iii) it must randomly choose a rotation value of either -.1 or +.1 for each asteroid, with equal probability. Values in between -.1 and +.1 are not permitted; and

(iv) it must add each asteroid to the sandbox.

  

3. Run and debug your program in NetBeans until it does what it is supposed to do.

If there are problems accessing the links (which I assume there will be because it wouldn't let me post them correctly), then i believe that my question is similar to that of the question asked and answered here https://www.chegg.com/tutors/Java-Programming-questions/Hi-Im-working-on-a-Java-exercise-which-uses-polymorphism-and-inheritance-so-that-I-can-use-an-external-JAR-file-called-BlobFXJAR-and-start-creating-polygons-for-an-asteroid-game-however-I-cant-seem-to-understand-how-to-approach-it-and-what-I-might-be-doin--JR8BJ/

I would have just accessed the other files in that link but they are marked as (locked) and I cannot open them.

Explanation / Answer


AsteroidGame.java

import blobmx.BlobGUI;
import blobmx.SandBox;
import blobmx.SandBoxMode;
import java.util.Random;

public class AsteroidGame implements BlobGUI {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        AsteroidGame game = new AsteroidGame();
    }

    private final SandBox sb;
    private final Random random = new Random();
  
    public AsteroidGame(){
        sb = new SandBox();
        sb.setSandBoxMode(SandBoxMode.FLOW);
        sb.setFrameRate(66);
        sb.init(this);
    }

    @Override
    public void generate() {
        Rocket rocket = new Rocket(0,0, sb);
        sb.addBlob(rocket);
      
        for (int i = 0; i < 10; i++) {
            int ranDelx = 0;
            while (ranDelx == 0){
                ranDelx = -3 + random.nextInt(7);
            }
            int ranDely = 0;
            while (ranDely == 0){
                ranDely = -3 + random.nextInt(7);
            }
          
            double rot = .1;
            int b = random.nextInt(2);
            if (b == 0){
                rot = -rot;
            }
          
            Asteroid asteroid = new Asteroid(ranDelx, ranDely, rot);
            sb.addBlob(asteroid);
        }
      
    }

  
}


Asteroid.java

import blobmx.BlobUtils;
import blobmx.PolyBlob;
import java.awt.Point;
import java.util.Random;

public class Asteroid extends PolyBlob {
    private static final Random random = new Random();

    public Asteroid(int idx, int jdx, double rot) {
        super(-100, -100, rot);
        setDelta(idx, jdx);
      
        int numSides = 5 + random.nextInt(5);
      
        int[] vertex = new int[numSides];
        for (int i = 0; i < numSides; i++){
            vertex[i] = 5 + random.nextInt(11);
        }
      
        double region = (2 * Math.PI)/numSides;
        double[] angle = new double[numSides];
        for (int i = 0; i < numSides; i++){
            angle[i] = (i * region) + (Math.random() * region);
        }
      
        int[] x = new int [numSides];
        int[] y = new int [numSides];
        for (int i = 0; i < numSides; i++) {
            Point p = BlobUtils.rotatePoint(vertex[i], angle[i]);
            x[i] = p.x;
            y[i] = p.y;
        }
      
        setPolygon(x,y);
    }
  
}


Missle.java

import blobmx.Blob;
import blobmx.BlobProximity;

public class Missle extends Blob implements BlobProximity {

    private final int speed = 5;
    private final int size = 5;

    public Missle(int x, int y, double theta) {
        super(0,0);
        this.setSize(size);
        this.setLoc(x,y);     
      
        int dx = (int) Math.round( speed * Math.cos(theta));
        int dy = (int) Math.round( speed * Math.sin(theta));
      
        this.setDelta(dx, dy);
    }
}


Rocket.java


import blobmx.BlobAction;
import blobmx.BlobProximity;
import blobmx.BlobUtils;
import blobmx.PolyBlob;
import blobmx.SandBox;
import java.awt.Point;
import java.awt.event.KeyEvent;

public class Rocket extends PolyBlob implements BlobAction, BlobProximity {

    private final int[] polygonx = new int[]{10, -10, -5, -10};
    private final int[] polygony = new int[]{0, -7, 0, 7};
  
    private final int[] referenceX;
    private final int[] referenceY;
  
    private double angle = 0.0;
    private final double delta = 0.15;
    private final double speed = 5.0;
  
    private final double twoTimesPi = 2 * Math.PI;
  
    private final SandBox sandBox;
  
    public Rocket(int x, int y, SandBox sb) {
      
        super(300, 300, 0);
      
        referenceX = polygonx;
        referenceY = polygony;
        sandBox = sb;
      
        this.setPolygon(polygonx, polygony);
    }

    @Override
    public void keyAction(KeyEvent keyEvent) {
        int keyCode = keyEvent.getKeyCode();
      
        switch (keyCode){
            case KeyEvent.VK_UP:
                MoveForward();
                break;
            case KeyEvent.VK_LEFT:
                RotateLeft();
                break;
            case KeyEvent.VK_RIGHT:
                RotateRight();
                break;
            case KeyEvent.VK_SPACE:
                Launch(sandBox);
                BlobUtils.playSound();
                break;
        }
    }
  
    private void RotateRight(){
        this.angle += delta;
      
        if (angle > twoTimesPi){
            angle -= twoTimesPi;
        }
      
        turn();
    }

    private void RotateLeft(){
        this.angle -= delta;

        if (angle < twoTimesPi){
            angle += twoTimesPi;
        }

        turn();
    }
  
    private void turn(){      
        for (int i = 0; i < referenceX.length; i++) {
            Point p = BlobUtils.rotatePoint(referenceX[i], referenceY[i], angle);

            polygonx[i] = p.x;
            polygony[i] = p.y;
        }
    }
  
    public void MoveForward() {
        Point p = this.getLoc();
      
        int xloc = p.x;
        int yloc = p.y;
      
        xloc = xloc + (int) Math.round(speed * Math.cos(angle));
        yloc = yloc + (int) Math.round(speed * Math.sin(angle));
      
        this.setLoc(xloc, yloc);
      
    }
  
    public void Launch(SandBox sb) {
        int boundingRadius = this.getSize()/2;
     
        Point currentLocation = this.getLoc();
      
        Point launchPoint = BlobUtils.rotatePoint(boundingRadius + 5, angle);
      
        int distx = currentLocation.x + launchPoint.x;
        int disty = currentLocation.y + launchPoint.y;
      

        Missle missle = new Missle(distx, disty, angle);
      
        sb.addBlob(missle);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote