185k views
0 votes
You have been handed a mysterious piece of data by an unknown person. Judging by his shifty eyes and maniacal laughter you don't think he can be trusted. Complete the below method to tell you the type for this unknown data. The example already implemented can be adapted for other wrapper classes as well. Complete the method to analyze String, Character, and Double. Examples: whatAmI(1) -> "Integer" 1 public String whatAmI(E e) { 3 //.getClass() returns the runtime class of an object //.equals() determines whether two objects are equivalent //by calling a new static instance of a wrapper object we can analyze //it's class type and compare it to the variable passed in if(e.getClass().equals(new Integer(1).getClass()) { return "Integer"; }else{ return "Who knows!"; Given a word, use a stack to reverse the word and return a new string with the original word in reverse order. You cannot use the reverse string methods in the String class. Examples: reverseword("hello") -> "olleh" reverseword("hallo") -> "ollah" i public String reverseWord(String word) Stack character> stack = new Stack ;

User Jkaufmann
by
2.9k points

1 Answer

2 votes

Answer:

Java Code given below with appropriate comments for better understanding

Step-by-step explanation:

1.

public String whatAmI(E e) {

//.getClass() returns the runtime class of an object

//.equals() determines whether two objects are equivalent

//by calling a new static instance of a wrapper object we can analyze

//it's class type and compare it to the variable passed in

if(e.getClass().equals(new Integer(1).getClass())) {

return "Integer";

}

else if(e.getClass().equals(new String("1").getClass())) {

return "String";

}

else if(e.getClass().equals(new Double(1.0).getClass())) {

return "Double";

}

else {

return "Who knows!";

}

}

2.

public String reverseWord(String word) {

Stack<Character> stack = new Stack<Character>();

for (int i = 0; i < word.length(); i++) {

stack.push(word.charAt(i));

}

String result = "";

while (!stack.isEmpty()) {

result += stack.pop();

}

return result;

}

User Fahad Abid Janjua
by
3.3k points