Uploaded by hussainizhar343

MID Home Take Exam

advertisement
2021
MID Take Home Exam
Submitted to:
Ma’am Madiha Liaqat
Submitted by:
Izhar Hussain(19-SE-12)
University
University of Engineering and
Technology, Taxila
Object Oriented Programming
MID EXAM
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
Department of Software Engineering
3rd Semester Mid Examination
Date: 11/01/2021
Subject: Object Oriented Programming Marks: 10
Outcomes Assessed:
CLO-3: Apply appropriate techniques to write programs.
Question#01.
Write a program to compute how many days are spanned by two given days. The program will include a
method called day-span that returns the number of days between and including its two inputs. The
example inputs to day-span represent dates in 1994 (a non-leap year); each input is a two-element list.
whose first element is a month name and whose second element is a legal date in the month. Assume
that the first input date is earlier than the second. For Example
Input
Output
(January 3) (January 9)
7
(January 30) (February 2)
4
(June 7) (August 25)
80
(January 1) (December 31)
365
NOTE: PLEASE ADD COMMENTS TO YOUR CODE.
Solution:
Explanation:
The program will include a
method called day-span that returns the number of days between and including its two inputs. The
example inputs to day-span represent dates in 1994 (a non-leap year); each input is a two-element list.
whose first element is a month name and whose second element is a legal date in the month. Assume
that the first input date is earlier than the second.
Source Code:
// Java program to find number of
// days between two given dates
import java.text.*;
import java.util.*;
public class date {
// Calculate number of days
public long days_span(Date one,Date two) {
long difference= (one.getTime()-two.getTime())/86400000;
return Math.abs(difference-1);
}
public static void main(String[] args)throws Exception {
Scanner input = new Scanner(System.in);
DateFormat formatDate = new SimpleDateFormat("MMMM dd yyyy", Locale.US);
// A date has day 'd', month 'm' and year 'y'
System.out.println("Enter First Date = ");
String Date1 = input.nextLine();
System.out.println("Enter Second Date = ");
String Date2 = input.nextLine();
input.close();
//Enter the date as "January 12" etc.
Date Date1P = formatDate.parse(Date1+"January 02");
Date Date2P = formatDate.parse(Date2+"February 01");
date myDate = new date();
long days = myDate.days_span(Date1P, Date2P);
//Output of the code will be given as:
System.out.println("\t"+"INPUT"+" "+"\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"OUTPUT");
System.out.println("("+Date1+")"+" "+"("+Date2+")"+"\t"+"\t"+"\t"+"\t"+days);
}
}
Output:
Download