Social Icons

twitterfacebookgoogle pluslinkedinrss feed

Tuesday, August 23, 2011

Advanced Java questions -4

A program which will accept three sentences (one sentence per line) and print the words having Initial Caps within the sentences. Below is an example.

Here is an example. If the below three sentences are given to the program as input,

This is a Program
Coding test of Initial Caps
the program Will Test You

Then, the output would look like:

This
Program
Coding
Initial
Caps
Will
Test
You

Solution:
Pattern p = Pattern.compile("^[A-Z]");


Advanced Java questions -3

Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The program should display the day on which June 28, 1999 fell, and in this case the output will be MONDAY.

The output should be displayed in uppercase letters.

Suppose the following INPUT sequence is given to the program:

1999-5
1998-6

Then the output should be:

MONDAY
TUESDAY

Solution:


import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Scanner;


public class Solution {

 /**
  * @param args
  */
 public static void main(String[] args) {
  boolean condition = false;
  do {
   Scanner scanner = new Scanner(System.in);
   String value = scanner.nextLine();
   condition = value.equalsIgnoreCase("exit");
   if(!condition && value.contains("-")){
    calculate(value);
   }
   System.out.println("Count is: " + condition);
  } while (!condition);


 }

 private static void calculate(String value) {
  final String[] input = value.split("-");
  Calendar cl = Calendar.getInstance();
  cl.set(Integer.parseInt(input[0]), Integer.parseInt(input[1]), 28);
  String[] weekdays = new DateFormatSymbols().getWeekdays();
  System.out.println(weekdays[cl.get(Calendar.DAY_OF_WEEK)].toUpperCase());
  
 }

}


Advanced java questions -2

Write a program that prints the numbers between 258 and 393 (both inclusive) which do not end with 5. The program should print the output so as to have one value per line. The output would therefore follow the below format:


value1
value2
value3
.
..
.
so on
Solution:
public class Solution2 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int start = 258;
  int end = 393;
  for (int i = start; i < end; i++) {
   if(i%10 !=5){
    System.out.println(i);
   }
   
  }

 }

}


Advanced Java questions

A program which will accept a single pair of strings separated by a comma; the program should calculate the sum of ASCII values of the characters of each string. The program should then subtract the sum of the ASCII values of the second string from the sum of the ASCII values of the first string.

Suppose the following input is given to the program:

123ABC,456DEF

Then the sum of the ASCII values of the characters in '123ABC' is 348 and in '456DEF' it is 366. The Difference between these numbers is 348 – 366 = -18
The corresponding output to be printed by the program is:

-18

Solution:
import java.util.Scanner;



public class Solution {

 /**
  * @param args
  */
 public static void main(String[] args) {
  boolean condition = false;
  do {
   Scanner scanner = new Scanner(System.in);
   String value = scanner.nextLine();
   condition = value.equalsIgnoreCase("exit");
   if(!condition && value.contains(",")){
    calculate(value);
   }
  } while (!condition);

 }
 
 private static void calculate(String value){
   
   
   
   final String[] event1 = value.split(",");
   int ss = 0;
   for ( int i = 0; i < event1[0].length(); ++i ) {
          char c = event1[0].charAt( i );
          ss += (int) c;
          //System.out.println(skype);
         }
   int sd = 0;
   for ( int i = 0; i < event1[1].length(); ++i ) {
          char c = event1[1].charAt( i );
          sd += (int) c;
          //System.out.println(sd);
         }
   System.out.println(ss-sd);
  
 }

}