Question
1. (12 points) Write a static method reversed which takes a string as input and returns a new string with the characters of the input
1. (12 points) Write a static method reversed which takes a string as input and returns a new string with the characters of the input in reverse order. Write another static method isPalindrome which determines if a string is the same forwards as it is backwards (e.g. racecar and sooloos). You must have isPalindrome call reversed.
2. (8 points) Suppose we have the following method definitions and method invocations. For each method invocation, determine which method is invoked, if any. Briefly support your choice by showing the type promotion as it applies to the arguments. If no method is applicable, explain why.
A: public static double three(char a, int b, int c);
B: public static double three(char a, double b, double c);
three(a, 123, 123);
three(a, .25, 0.5);
three(a, 123, 2.0);
three(a, b, c);
three((char) 97, 3.1, 123);
three(123, 123, 123);
3. (25 points) Write a complete definition of a class unit (one-by-one) square in two-dimensional space conforming to the following requirements.
Define a constructor which accepts two floating point numbers representing the upper-left corner (x, y) as parameters.
Define a default constructor creating a with an upper-left corner of (1, 0).
Define an equals method.
Define accessors for x and y.
Define a method contains which takes two floating point arguments representing the point (x, y) and determines if this point falls within the square (e.g. x and y are both within the bounds of the square).
4. (20 points) Write a method countTouching which takes an array of squares (as implemented in the previous question), and two floating point arguments representing the point (x, y), and returns how many of the squares contain the given point.
5. (35 points) The following interface for a game defines a method isAlive which determines if the entity has a non-zero number of health, and a method hurt which takes some amount of damage to be applied to an entity.
public interface Damageable {
public boolean isAlive();
public void hurt(int amount);
}
Complete the implementation below representing a basic non-playable-character. All that is left is to implement the hurt method. You should ensure that the health of the NPC never goes below zero.
public class BasicNPC implements Damageable {
private int health;
public BasicNPC(int maxHealth) { this.health = maxHealth; }
public boolean isAlive() { return health > 0; }
}
Write a second implementation of Damageable called ArmoredNPC. It should represent a non-playable
character that wears armor. The armor has a dampening factor, which reduces the damage received by
some amount. For example, a dampening factor of 1 will reduce damage from 12 to 4. The NPC cannot 3
change its armor, so the dampening factor should be supplied at construction time.
public class ArmoredNPC
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