Question
The programming language is Java and all of the Classes I was given are in bold. ALIEN CLASS import imagePackage.RasterImage; import java.awt.BasicStroke; import java.awt.Color; import
The programming language is Java and all of the Classes I was given are in bold.
ALIEN CLASS
import imagePackage.RasterImage;
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.geom.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.PrintStream;
// ALIEN is a COMPOSITION of Point2D // ALIEN is an AGGREGATION of Color
public class Alien { // functionality to draw an Alien, given a position and Graphics2D object private Point2D.Double pos; private Color col; private double width; private double height; public Alien(Point2D.Double pos) { this.pos = new Point2D.Double(pos.getX(),pos.getY()); this.col = Color.BLACK; this.width = 50; this.height = 40; } public Alien(Point2D.Double pos, double width, double height, Color col) { this.pos = new Point2D.Double(pos.getX(),pos.getY()); this.width = width; this.height = height; this.col = col; } public Alien(Alien other) { this.pos = new Point2D.Double(other.pos.getX(),other.pos.getY()); this.col = other.col; this.width = other.width; this.height = other.height; } public void setColor(Color col) { this.col = col; } public void setPos(Point2D.Double pos) { this.pos = new Point2D.Double(pos.getX(), pos.getY()); } public void drawAlien(Graphics2D gfx) { int[] xPoints = new int[6]; int[] yPoints = new int[6]; int lx = (int) this.pos.getX(); int ly = (int) this.pos.getY(); int cx = (int) (lx + this.width/2); int cy = (int) (ly + this.height/2); int rx = (int) (lx + this.width); int ry = (int) (ly + this.height); xPoints[0] = lx; yPoints[0] = ly; xPoints[1] = cx; yPoints[1] = cy; xPoints[2] = rx; yPoints[2] = ly; xPoints[3] = rx; yPoints[3] = cy; xPoints[4] = cx; yPoints[4] = ry; xPoints[5] = lx; yPoints[5] = cy; gfx.setColor(this.col); gfx.setStroke(new BasicStroke(2.0f)); gfx.fillPolygon(xPoints, yPoints, xPoints.length); } @Override public String toString() { String result = "Alien: pos= " + this.pos + ", size= " + this.width + ", " + this.height; return result; }
public static void main(String[] args) { /* * In this task, you are to create an Alien class that stores the * * */ PrintStream out = System.out; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); RasterImage gameBoard = new RasterImage(640,480); gameBoard.show(); Graphics2D g = gameBoard.getGraphics2D(); Alien a1 = new Alien(new Point2D.Double(100, 100)); Alien a2 = new Alien(new Point2D.Double(200, 100),100,150,Color.RED); Alien a3 = new Alien(a1); a3.setColor(Color.ORANGE); a3.setPos(new Point2D.Double(100, 300)); a1.drawAlien(g); a2.drawAlien(g); a3.drawAlien(g); } }
ANGRYALIEN CLASS
import imagePackage.RasterImage;
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.geom.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.PrintStream;
public class AngryAlien extends Alien {
public static void main(String[] args) { // Use this to test creation (instantiation) and display of an individual // AngryAlien // OR - use the main method in BattleField to test
} }
DOCILEALIEN CLASS
import imagePackage.RasterImage;
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.geom.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.PrintStream;
public class DocileAlien extends Alien {
public static void main(String[] args) { // Use this to test creation (instantiation) and display of an individual // DocileAlien // OR - use the main method in BattleField to test
} }
NEUTRALALIEN CLASS
import imagePackage.RasterImage;
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.geom.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.PrintStream;
public class NeutralAlien extends Alien {
public static void main(String[] args) { // Use this to test creation (instantiation) and display of an individual // NeutralAlien // OR - use the main method in BattleField to test
} }
BATTLEFIELD CLASS
import imagePackage.RasterImage;
import java.awt.Color; import java.awt.Composite; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.Shape; import java.awt.Stroke; import java.awt.Toolkit; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.io.PrintStream;
public class BattleField {
public static void main(String[] args) { /* * Exercise: * Create an Array of Aliens, positioned according to a bounding box * * */
final int ROWS = 3; final int COLS = 10; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); RasterImage gameBoard = new RasterImage(640,480); gameBoard.show(); gameBoard.setTitle("BattleField"); Graphics2D g = gameBoard.getGraphics2D();
Alien[][] aliens = new Alien[ROWS][COLS]; // 2D array of Aliens
for (int col=0; col for (int row=0; row
// create and initialize position of Aliens Point2D.Double pos = new Point2D.Double(col*60.0, row*60.0); aliens[row][col] = new Alien(pos); aliens[row][col].drawAlien(g);
}
} Hero shooter = new Hero(200,350); shooter.draw(g);
//
} }
Exercise 03 (create a family of Aliens using Inheritance): In this task, you are required to create three different subclasses of the Alien class. For example: Alien AngryAlien NeutralAlien DocileAlien The idea is that each class will represent a different type of Alien. Now, for the most part, what differs about each Alien will be the way that each is drawn. For this task, create three new classes: AngryAlien, NeutralAlien, DocileAlien Each of these Alien sub-classes should inherit most of the functionality of the Alien class, 1. The AngryAlien is RED, NeutralAlien is GREEN, and DocileAlien is a. Note, you will need to make calls to superclass constructor (using super), to b. You can then use Alien's setcolor method, or modify the colour field however, you will need to modify the constructors so that, by default: BLUE create the Alien part of each directly (if modifying, ensure that you have the right access to set that field) 2. Each of these classes overrides the toString method So that a suitable text representation of each class object can be outputted as a string to the console a. 3. Each of these classes overrides the drawAlien method a. So that each alien has a different physical appearance on the screen b. Stick to the same dimensions as with the previous lab, but create quite different looking aliens for each subclass (different shapes) Have a look at the way a polygon is defined in the original Alien class, try to use a polygon to define your alien objects c. d. You may test each by creating a client class, or creating and displaying an individual alien in the main method of each of these subclasses 4. Finally, create a custom constructor for the BattleField class (one that accepts an integer for the number of different subclasses of Allen to consider). This constructor creates the same type of Alien array, however, randomly creates several different types of Alien. HINT: you may create an array of type Alien, and then instantiate a specific type of Alien to be assigned: i. E.g. aliens[13new AngyAlien(.); aliens[2]11] -new DocileAlien( .)i ii. OR you can use the integer passed to define a range for a random integer (e.g. from 0-2) that can be used in a switch statement to instantiate and add one of the three possible Alien subclass objects to the aliens array. Remember, an object of a child class (subclass) can be legally substituted in for an object of a parent class (superclass) ** is not important where you instantiate specific types of aliens in the grid, just that there are some locations that have different aliens. Note: the draw method of BattleField should call the appropriate version of drawAlien depending on the object. [this should happen automatically (the original version of draw should not need to be modified for this Exercise) - this is an example of polymorphism - i.e. the Alien objects all take on different behaviour, depending on the sub-classes actually instantiated] Exercise 03 (create a family of Aliens using Inheritance): In this task, you are required to create three different subclasses of the Alien class. For example: Alien AngryAlien NeutralAlien DocileAlien The idea is that each class will represent a different type of Alien. Now, for the most part, what differs about each Alien will be the way that each is drawn. For this task, create three new classes: AngryAlien, NeutralAlien, DocileAlien Each of these Alien sub-classes should inherit most of the functionality of the Alien class, 1. The AngryAlien is RED, NeutralAlien is GREEN, and DocileAlien is a. Note, you will need to make calls to superclass constructor (using super), to b. You can then use Alien's setcolor method, or modify the colour field however, you will need to modify the constructors so that, by default: BLUE create the Alien part of each directly (if modifying, ensure that you have the right access to set that field) 2. Each of these classes overrides the toString method So that a suitable text representation of each class object can be outputted as a string to the console a. 3. Each of these classes overrides the drawAlien method a. So that each alien has a different physical appearance on the screen b. Stick to the same dimensions as with the previous lab, but create quite different looking aliens for each subclass (different shapes) Have a look at the way a polygon is defined in the original Alien class, try to use a polygon to define your alien objects c. d. You may test each by creating a client class, or creating and displaying an individual alien in the main method of each of these subclasses 4. Finally, create a custom constructor for the BattleField class (one that accepts an integer for the number of different subclasses of Allen to consider). This constructor creates the same type of Alien array, however, randomly creates several different types of Alien. HINT: you may create an array of type Alien, and then instantiate a specific type of Alien to be assigned: i. E.g. aliens[13new AngyAlien(.); aliens[2]11] -new DocileAlien( .)i ii. OR you can use the integer passed to define a range for a random integer (e.g. from 0-2) that can be used in a switch statement to instantiate and add one of the three possible Alien subclass objects to the aliens array. Remember, an object of a child class (subclass) can be legally substituted in for an object of a parent class (superclass) ** is not important where you instantiate specific types of aliens in the grid, just that there are some locations that have different aliens. Note: the draw method of BattleField should call the appropriate version of drawAlien depending on the object. [this should happen automatically (the original version of draw should not need to be modified for this Exercise) - this is an example of polymorphism - i.e. the Alien objects all take on different behaviour, depending on the sub-classes actually instantiated]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started