Вы находитесь на странице: 1из 5

Java

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;

public class Palindrome {

private String pal;

public Palindrome(String initPal) {


pal = initPal.toUpperCase();
}

public boolean isPalindrome() {

if (pal.length() <= 1) { // it is one char palindrom

return true;
}

// Get the first and last char.


char first = pal.charAt(0);
char last = pal.charAt(pal.length()-1);

if (Character.isLetter(first) &&
Character.isLetter(last)) {

if (first != last) { //if not false

return false;
}
else {

Palindrome sub = new Palindrome(


pal.substring(1,pal.length()-1));
return sub.isPalindrome(); //call recrusive
}
}
else if (!Character.isLetter(first)) { // first is not a char

Palindrome sub = new Palindrome(pal.substring(1));


return sub.isPalindrome();
}
else { //last is not a char

Palindrome sub = new Palindrome(


pal.substring(0,pal.length()-1));
return sub.isPalindrome();
}
}

/**
* @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.

How would you represent this variable? (Please show by code.)


Give an example on how this variable would be used by other classes.

Representation in terms of class :

package sesha;

public class Option {


private OptionType optionTyp =null;

public OptionType getOptionTyp() {

return optionTyp;
}

public void setOptionTyp(String ch) {


if(ch !=null &&
OptionType.UpNOutOptionType.equalsIgnoreCase(ch)) {
optionTyp = new UpNOutOptionType(ch);
}
else if(ch !=null &&
OptionType.DownNOutOptionType.equalsIgnoreCase(ch)) {
optionTyp = new DownNOutOptionType(ch);
}
else if(ch !=null &&
OptionType.Plain.equalsIgnoreCase(ch)) {
optionTyp = new Plain(ch);
}
}
interface OptionType {
public final static String UpNOutOptionType = "U";
public final static String DownNOutOptionType = "D";
public final static String Plain = "P";
String getOptionTypeName();
}
abstract class BaseOptionType implements OptionType {
String type = null;
BaseOptionType(String str) {
type = str;
}

}
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";
}
}
}

usage of option type by other class :

package sesha;

import sesha.Option.DownNOutOptionType;
import sesha.Option.UpNOutOptionType;

public class OptionClient {


public void performActionBasedOnType(Option p) {
if(UpNOutOptionType.class.isInstance(p.getOptionTyp())) {
//perform differnt action based on Up-and-out type
}
elseif(DownNOutOptionType.class.isInstance(p.getOptionTyp
())) {
//perform differnt action based on Down-and-out
type
}
}
}

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.

public class ReportRunner {


public method runReportA(Calendar fromDate, Calendar toDate) {
try {
Report rpt = new Report(label);
System.out.println("Start running Report A: fromDate = " + format(fromDate) + "; toDate = " +
format(toDate));
results = rpt.runReport(fromDate, toDate, true, true);
if (results == null) {
System.out.println("No results!");
} else {
System.out.println("Finished running report A");
}
} catch (Exception e) {
...
}
}

public method methodB (Calendar fromDate, Calendar toDate) {


try {
Report rpt = new Report(label);
System.out.println("Start running Report B: fromDate = " + format(fromDate) + "; toDate = " +
format(toDate));
results = rpt.runOpenDealsReport(fromDate, toDate, false, true);
if (results == null) {
System.out.println("No results!");
} else {
System.out.println("Finished running report A");
}
} catch (Exception e) {
...
}
}

public method methodC(String dealNumber) {


try {
Report rpt = new Report(label);
System.out.println("Start running Report C: deal_number = " + dealNumber);
results = rpt.runReport(dealNumber, true, true);

if (results == null) {
System.out.println("No results!");
} else {
System.out.println("Finished running report A");
}
} catch (Exception e) {
...
}
}

private String format(Calendar cal) {


...
}
}

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.

Вам также может понравиться