1. (12 points) Write a static method reversed which takes a string as input and
ID: 3809085 • Letter: 1
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 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
Explanation / Answer
1.
static boolean isPalindrome(String s){
if (s.equals(reversed(s))){
return true;
}
else{
return false;
}
}
static String reversed (String s){
StringBuffer sb = new StringBuffer(s);
return sb.reverse().toString();
}
2.
first invocation will call three method deffined in A because it matches the types exactly
2nd invocation will call three method deffined in B it matches the types exactly.
3rd invocation will call three method deffined in B because 2nd int type can be promoted to double but reverse is not true.
4th invocation will call three method deffined in A because char can be promoted to int.
5th invocation will call three method deffined in B because 1st type is casted to char and it matches the types exactly.
6th invocation is invalid and wont compile because the type does not matches any of the methods even after promoting. int can not be promoted to char.
3.
public class Square {
private float x;
private float y;
public Square() {
this.x = 1.0f;
this.y = 0.0f;
}
public Square(float x, float y) {
this.x = x;
this.y = y;
}
public boolean isPointInsideSquare(float p1, float p2) {
float x1 = x;
float y1 = y;
float x2 = x + 1.0f;
float y2 = y;
float x3 = x;
float y3 = y + 1.0f;
float x4 = x3 + 1.0f;
float y4 = y3;
// Now total area 4 subtriangles fornmed by the point inside a
// square is equal to which is the area of unit square
// then the point lies within square else not
if (Square.areaofSubTrainagle(x1, y1, x2, y2, p1, p2)
+ Square.areaofSubTrainagle(x2, y2, x3, y3, p1, p2)
+ Square.areaofSubTrainagle(x3, y3, x4, y4, p1, p2)
+ Square.areaofSubTrainagle(x1, y1, x4, y4, p1, p2) == 1.0) {
return true;
}
return false;
}
public static float areaofSubTrainagle(float m1, float n1, float m2,
float n2, float m3, float n3) {
return (float) Math.abs(((m1 * (n2 - n3) + m2 * (n3 - n1) + m3
* (n1 - n2)) / 2.0));
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}
4.
static int countTouching(Square[] s, float x1, float y1) {
int count = 0;
for (int i = 0; i < s.length; i++) {
if (s[i].isPointInsideSquare(x1, y1)) {
count = count + 1;
}
}
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.