File task7.java contains an incomplete program, that takes as input from the use
ID: 3676316 • Letter: F
Question
File task7.java contains an incomplete program, that takes as input from the user a date and outputs two things: the number of days that have passed from 12/31/999 up to the specified date, and the day of the week for that specified date. Complete that program, by defining two functions. The first function is called days_passed, and should satisfy the following specs:
Function days_passed takes three arguments, called year, month, day. You can assume that year > 0, that month is between 1 and 12, and that dayis between 1 and the number of days for that month of that year. We will only test your code with such cases.
The function returns the number of days that have passed from 12/31/999 to the specified date.
Hint: to compute that, you may find it useful to use your solutions from Task 6. Otherwise you will need to write significantly more lines of code. In more detail:
Do a for-loop from 1000 up to (and not including) the specified year, adding up the days of each year in the loop. You may find it useful to call your year_days function from Task 6 (to be able to call your year_days function, you need to copy and paste that function from your task6.java file to your task7.java file).
Then, do another for-loop, from 1 up to (and not including) the specified month, adding up the days of each month in the loop. You may find it useful to call your month_days function from Task 6 (to be able to call your month_days function, you need to copy and paste that function from your task6.java file to your task7.java file).
Finally, add to your total the specified day of the month.
The second function is called day_of_week, and should satisfy the following specs:
Function day_of_week takes three arguments, called year, month, day. For these arguments, you can make the same assumptions that are stated for the arguments of the days_passedfunction.
The function returns the day of the week (e.g, Sunday, Tuesday, and so on) that corresponds to the specified date.
Hint: the answer depends only on the remainder of dividing by 7 the result of days_passed(year, month, day). For example, if the remainder is 0 the function should return "Tuesday", if the remainder is 1 the function should return "Wednesday", and so on.
IMPORTANT: You are NOT allowed to modify in any way the main function..
This is an example run of the complete program:
Explanation / Answer
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class task7 {
public static int user_integer(String message) {
Scanner in = new Scanner(System.in);
int result;
while (true) {
System.out.printf(message);
String s = in.next();
if (s.equals("q")) {
System.out.printf("Exiting... ");
System.exit(0);
}
try {
result = Integer.parseInt(s);
} catch (Exception e) {
System.out
.printf("%s is not a valid number, try again. ", s);
continue;
}
if (result <= 0) {
System.out.printf("%s is <= 0, try again. ", s);
continue;
}
return result;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
int year = user_integer("Enter a year (must be >= 1000): ");
if (year < 1000) {
System.out.printf("Invalid year. ");
continue;
}
int month = user_integer("Enter a month(must be between 1 and 12): ");
if (month > 12) {
System.out.printf("Invalid month. ");
continue;
}
int day = user_integer("Enter a day: ");
int result = days_passed(year, month, day);
System.out.printf(
"%d days have passed from 12/31/999 to %d/%d/%d. ",
result, month, day, year);
String day_name = day_of_week(year, month, day);
System.out.printf("%d/%d/%d is a %s. ", month, day, year,
day_name);
}
}
private static String day_of_week(int year, int month, int day) {
// TODO Auto-generated method stub
SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy");
String inputString2 = day + "/" + (month) + "/" + year;
String dayOfWeek = "";
try {
Date date2 = myFormat.parse(inputString2);
myFormat = new SimpleDateFormat("EEEE"); // the day of the week
// spelled out
// completely
dayOfWeek = myFormat.format(date2);
} catch (Exception e) {
// TODO: handle exception
}
return dayOfWeek;
}
private static int days_passed(int year, int month, int day) {
// TODO Auto-generated method stub
SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy");
String inputString1 = "31/12/999";
String inputString2 = day + "/" + (month) + "/" + year;
int days = 0;
try {
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diff = date2.getTime() - date1.getTime();
days = (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
}
OUTPUT:
Enter a year (must be >= 1000): 1000
Enter a month(must be between 1 and 12): 1
Enter a day: 1
1 days have passed from 12/31/999 to 1/1/1000.
1/1/1000 is a Monday.
Enter a year (must be >= 1000): 2015
Enter a month(must be between 1 and 12): 8
Enter a day: 4
370932 days have passed from 12/31/999 to 8/4/2015.
8/4/2015 is a Tuesday.
Enter a year (must be >= 1000): 1776
Enter a month(must be between 1 and 12): 7
Enter a day: 4
283609 days have passed from 12/31/999 to 7/4/1776.
7/4/1776 is a Thursday.
Enter a year (must be >= 1000): q
Exiting...
NOTE: I DONT HAVE TASK6 CODE thats why i am using java util api
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.