Question

In: Computer Science

Create the following class to represent the sprite objects in the game. Sprite Name: String x:...

Create the following class to represent the sprite objects in the game.

Sprite

  • Name: String
  • x: x location
  • y: y location

      +   Sprite (String);

      +   Sprite (String, int, int);     

      +   setName (String): void

      +   setX(int): void

      +   setY(int): void

      +   getName (): String

      +   getX (): int

      +   getY (): int

      +   collide (Sprite): boolean

      +   toString (): String

Create ten (10) game objects as Sprite type and stored in an array. All the games objects location (x & y) must be set in range 1 to 800 randomly.

The collide method is a method to test the collision between the current sprite with another sprite (from parameter). This method will check the distance in between two sprites based on the current x and y location using Pythagoras theorem. If the distance of 2 objects is less than 10, then the method will return true. Otherwise false will be returned.

Create a driver class called TestSprite which will:

  • Display all the game sprite objects with a loop
  • Use the games objects from the array, test all the instance methods.

Solutions

Expert Solution

Here are the 2 classes - Sprite.java and TestSprite.java

=================================================================

public class Sprite {

    private String name;
    private int x;
    private int y;

    public Sprite(String name) {
        this.name = name;
        x = y = 0;
    }

    public Sprite(String name, int x, int y) {
        this.name = name;
        this.x = x;
        this.y = y;
    }

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

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

    public String getName() {
        return name;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean collide(Sprite aSprite) {

        int distance = (x - aSprite.getX()) * (x - aSprite.getX()) +
                (y - aSprite.getY()) * (x - aSprite.getY());

        return distance <= 100;

    }


    public String toString() {
        return "Sprite Name: " + name + ", Location (" + x + "," + y + ")";
    }

}

======================================================================

import java.util.Random;
public class TestSprite {
    public static void main(String[] args) {

        Sprite[] sprites = new Sprite[10];
        Random r = new Random();
        sprites[0] = new Sprite("Mercury", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[1] = new Sprite("Venus", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[2] = new Sprite("Earth", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[3] = new Sprite("Mars", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[4] = new Sprite("Jupiter", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[5] = new Sprite("Saturn", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[6] = new Sprite("Uranus", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[7] = new Sprite("Neptune", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[8] = new Sprite("Pluto", r.nextInt(800) + 1, r.nextInt(800) + 1);
        sprites[9] = new Sprite("Moon", r.nextInt(800) + 1, r.nextInt(800) + 1);

        System.out.println("Sprite Details: ");
        for (int i = 0; i < sprites.length; i++) {
            System.out.println("Name: " + sprites[i].getName() + " X: " + sprites[i].getX() + ", Y: " + sprites[i].getY());
        }

        System.out.println("\nCollision Status:");
        for (int i = 0; i < sprites.length - 1; i++) {
            for (int j = i + 1; j < sprites.length; j++) {
                if (sprites[i].collide(sprites[j])) {
                    System.out.println(sprites[i] + " collides with " + sprites[j]);
                }
            }
        }

    }
}

=======================================================================


Related Solutions

Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class.   HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize the common...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT