Академический Документы
Профессиональный Документы
Культура Документы
Question 1
Write a method to determine if a given string is a palindrome (e.g. "Madam I'm Adam", "A
man, a plan, a canal, Panama")
package sesha;
return true;
}
if (Character.isLetter(first) &&
Character.isLetter(last)) {
return false;
}
else {
/**
* @param args
*/
public static void main(String[] args) {
Palindrome p1 = new Palindrome("Madam, I'm Adam.");
System.out.println(p1.isPalindrome());
Palindrome p2 = new Palindrome("A man, a plan, a canal,
Panama");
System.out.println(p2.isPalindrome());
Question 2
You want to add a new variable to an existing class, Option, to represent the option type.
The option type is a 1-character string; e.g. U = Up-and-out , D = Down-and-out, V = Plain,
and is provided by an external system.
Your system will use the Option objects in different ways depending on the option type.
package sesha;
return optionTyp;
}
}
class UpNOutOptionType extends BaseOptionType {
public UpNOutOptionType(String ch) {
super(ch);
}
public String getOptionTypeName() {
return "Up-and-out";
}
}
class DownNOutOptionType extends BaseOptionType {
public DownNOutOptionType(String ch) {
super(ch);
}
public String getOptionTypeName() {
return "Dwon-and-out";
}
}
class Plain extends BaseOptionType {
public Plain(String ch) {
super(ch);
}
public String getOptionTypeName() {
return "Plain";
}
}
}
package sesha;
import sesha.Option.DownNOutOptionType;
import sesha.Option.UpNOutOptionType;
Question 3
Please explain the difference between inheritance and interface. Give examples of when you
have used each in the past and the reason.
Inheritance is used to inherit the common functionalities from the base class abstract or
concrete class, the new class will get the common base class functionality rather writing it
again as duplicate. Where as interface provides a way to define a protocol to communicate
with the object. It doesn’t have implementation and by implementing interface it is option to do
multiple inheritances.
I used in my last project for Product class. Base Product class which have common
functionalities and the other product types such as Equity, Warrant, Option, and Future are as
their own functionalities and inherits the common from the base. Had defined a common
interface which exposes methods to the client to get product details by sending various
product identifiers irrespective of who is the implementation class.
Question 4
I would like to use an object at run-time that can be instantiated from 1 of several classes.
The actual class is determined at run-time. How would you do this with reflection?
What are the alternatives?
Object p = new String();
if( String.class.isInstance(p)) {
String p1 = (String) p;
System.out.println("p1 is instance of String");
}
// alternative way
if (p instanceof String) {
String p2 = (String) p;
System.out.println("p2 is instance of String");
}
Question 5
You come across the following class. Would you change it? If so, please show how. If not,
please list the reasons.
if (results == null) {
System.out.println("No results!");
} else {
System.out.println("Finished running report A");
}
} catch (Exception e) {
...
}
}
Changes :
Rather defining various methods just has one method which takes input param has hasmap which
has all input values to the method. The second parameter execute knows what method needs to be
executed and it gets what details from input. This way we can expose single point of contact to various
clients. When any implementation changes or need to add new function existing client should not effect.
Interface ReportRunner {
public void runReport(HashMap input, String execute);
}
public class ReportRunnerImpl implement Report
public void runReport(HashMap input, String execute) {
if(execute.equals(“runReportA”) {
runReportA(HashMap input);
}
}
private void runReportA(HashMap input) {
Date from =(Date) input.get(“FromDate”);
Date to =(Date) input.get(“ToDate”);
Report r = new Report(lable);
r.runReport(fromDate, toDate, true, true);
}
Suggestion: It is report generate engine and the clients no need to wait for the response then we can execute the
reports through asynchronous calls. Through means of Multithreading or messaging.