Social Icons

twitterfacebookgoogle pluslinkedinrss feed Online Resume

Tuesday, August 23, 2011

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());
  
 }

}


Post a Comment