Question: Write Java code for the following problems and place it underneath the problem description. You should use good programming practices, include good names and spacing.
Write Java code for the following problems and place it underneath the problem description. You should use good programming practices, include good names and spacing. The harder it is to read and understand your code, the less points you will earn. You may code it in Eclipse to ensure your syntax is correct but put your solution in this document underneath the problem. You may have to make assumptions in some cases, especially if there is not enough information. If you do assume something, be sure to state the assumption. Note: An assumption does not guarantee that your solution is automatically correct, but it helps in determining partial credit.
Use the following code to answer the following problems.
A half-elf is half human and half elf. This following classes must implement two interfaces, Human and Elf.
public class HalfElf {
public static final int BASEHEIGHT = 3;
private String first;
private String last;
private int height;
private String ears;
public HalfElf() { }
public HalfElf(String first, String last) {
this.first = first;
this.last = last;
this.height = BASEHEIGHT;
}
public String getFirst() {
return first;
}
public String getName() {
return last;
}
public String toString() {
String fullname = first + " " + last;
return fullname;
}
public boolean equals(HalfElf person) {
if(this.first == person.first && this.last == person.last)
return true;
else
return false;
}
}
1. Write a Java interface to encapsulate the concept Human. The interface should include:
- Static constant TYPE set to the string Human
- An integer called HEIGHTADJ set to the value of 3
- Method header for setHeight() with no arguments
- Method header for getHeight() with no arguments and returns an integer
- A default method named showType() that will print Human to the console
2. Write a Java interface to encapsulate the concept Elf. The interface should include:
- Static constant TYPE set to the string Elf
- An integer called HEIGHTADJ set to the value of 3
- Method header for setHeight() with no arguments
- Method header for getHeight() with no arguments and returns an integer
- A default method named showType() that will print Elf to the console
3. Adjust the HalfElf code above to use the two interfaces:
- Have setEars() set the ears to Pointy
- Have getEars() return the ears string
- Have setHeight() set the height to base height + human height adjustment + elf height adjustment
- Have getHeight() return the height
- Have the showType() output the type as Half-elf: Human + Elf. Use the interface constants to build the string.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
