patternjavaMinor
Output day of the week from given date
Viewed 0 times
thegivenoutputdateweekfromday
Problem
I am posting this code up for review and debugging. This source code is for Java, and is meant to give you the exact day of the week from an entered date.
Review
Debugging
Please feel free to test this program out, and reply with any incorrect output given from this code. I have tested this program with various dates and have gotten the correct output. However, it is always a good idea to have more eyes test this code out.
Program Purpose:
Enter any date from the 1800 to the present day, and this program will display the day of the week from the date entered.
Example:
If a user entered "July 2 , 1985". This program will return "Tuesday", which was the day of the week that on "July 2, 1985".
```
// Import Libraries
import javax.swing.*;
import java.util.*;
import java.io.*;
// This is my program.
public class DateCalc
{
public static void main (String[] args)
{
String month;
String day;
String inputYear;
Scanner keyboard = new Scanner(System.in);
// receiving input for my age variable
System.out.print( " \n \n Please Enter The Month Of The Date :");
month = keyboard.nextLine();
// receiving input for my weight variable
System.out.print( " \n Please Enter The Day Of The Date : ");
day = keyboard.nextLine();
// receiving input for my height variable
System.out.print( " \n Please Enter The Year OF The Date : ");
inputYear = keyboard.nextLine();
String stringYear = ""+ inputYear.charAt(inputYear.length()-2) +
Review
- This program seems to have 2 case statements. Would it have made sense to use an enum variable to eliminate the second case statement?
- How about restructuring my entire program to include one super class and a sub class?
- Are these approach a good idea to shorten my code, or is my original code the best approach to my program's purpose?
Debugging
Please feel free to test this program out, and reply with any incorrect output given from this code. I have tested this program with various dates and have gotten the correct output. However, it is always a good idea to have more eyes test this code out.
Program Purpose:
Enter any date from the 1800 to the present day, and this program will display the day of the week from the date entered.
Example:
If a user entered "July 2 , 1985". This program will return "Tuesday", which was the day of the week that on "July 2, 1985".
```
// Import Libraries
import javax.swing.*;
import java.util.*;
import java.io.*;
// This is my program.
public class DateCalc
{
public static void main (String[] args)
{
String month;
String day;
String inputYear;
Scanner keyboard = new Scanner(System.in);
// receiving input for my age variable
System.out.print( " \n \n Please Enter The Month Of The Date :");
month = keyboard.nextLine();
// receiving input for my weight variable
System.out.print( " \n Please Enter The Day Of The Date : ");
day = keyboard.nextLine();
// receiving input for my height variable
System.out.print( " \n Please Enter The Year OF The Date : ");
inputYear = keyboard.nextLine();
String stringYear = ""+ inputYear.charAt(inputYear.length()-2) +
Solution
Formatting
Your intentation is a bit off. Please improve that. Use your IDE's automatic indentation keyboard shortcut for an easy fix (Ctrl + I in Eclipse).
In Java, braces go on the same line, for example
becomes
Your questions
This program seems to have two case statements. Would it have made sense to use an
Yes. And not only the second. The first as well.
How about restructuring my entire program to include one super class and a sub class?
I see absolutely no reason whatsoever why you'd want to include a superclass and a subclass in this program. The program is simple enough that there's no need to use polymorphism.
Are these approach a good idea to shorten my code, or is my original code the best approach to my program's purpose?
You need to learn how to use the data structures and types that Java provides.
Cleaning up
You're calling one variant or another of
Months
Here's an example that is using
Of course it'd be better to use an
Weekdays
Get rid of that temporary variable and return directly!
Or, again, even better: Use an enum!!
Input
You seem to only be accepting dates on the 1900's. This is bad. At least provide a decent error message for when a date is outside the range accepted by your program.
I'd like to see more dates supported. At first I actually tried some other dates and discovered that it was incorrect result, then I found out why.
I don't like that line at all. First of all, it could be better expressed as
secondly, you're asking the user to input more information than what you use. Instead, ask for less input from the user.
I would however, like to see that you instead added support for the following dates:
Algorithm
Your algorithm seems to be based on a restricted variant of The Doomsday Rule. As is explained on Wikipedia, you could add support for any century. No need to be limited to the 1900's.
Java 8
If possible, use Java 8! Java 8 provides the
Outputs:
Your intentation is a bit off. Please improve that. Use your IDE's automatic indentation keyboard shortcut for an easy fix (Ctrl + I in Eclipse).
In Java, braces go on the same line, for example
public class DateCalc
{becomes
public class DateCalc {Your questions
This program seems to have two case statements. Would it have made sense to use an
enum variable to eliminate the second case statement?Yes. And not only the second. The first as well.
How about restructuring my entire program to include one super class and a sub class?
I see absolutely no reason whatsoever why you'd want to include a superclass and a subclass in this program. The program is simple enough that there's no need to use polymorphism.
Are these approach a good idea to shorten my code, or is my original code the best approach to my program's purpose?
You need to learn how to use the data structures and types that Java provides.
Cleaning up
results(day, inputYear, month, year, intDay, janKey);You're calling one variant or another of
results on all methods. The only variance is in the janKey variable and value that is used.Months
Here's an example that is using
HashMap for this lookup:Map monthMap = new HashMap();
monthMap.put("january" , 1);
monthMap.put("february", 4);
monthMap.put("march" , 4);
int key = monthMap.get(month.toLowerCase());
results(day, inputYear, month, year, intDay, key);Of course it'd be better to use an
enum. enum Month {
JANUARY(1), FEBRUARY(4), MARCH(4);
public final int key;
private Month(int key) {
this.key = key;
}
}
int key = Month.valueOf(month.toUpperCase()).key;Weekdays
weekDay = " Wednesday ";
return weekDay;Get rid of that temporary variable and return directly!
Or, again, even better: Use an enum!!
enum Weekday {
Sunday, Monday, Tuesday;
}
return Weekday.values()[day - 1];Input
You seem to only be accepting dates on the 1900's. This is bad. At least provide a decent error message for when a date is outside the range accepted by your program.
I'd like to see more dates supported. At first I actually tried some other dates and discovered that it was incorrect result, then I found out why.
String stringYear = ""+ inputYear.charAt(inputYear.length()-2) + inputYear.charAt(inputYear.length()-1);I don't like that line at all. First of all, it could be better expressed as
String stringYear = inputYear.substring(inputYear.length() - 2);secondly, you're asking the user to input more information than what you use. Instead, ask for less input from the user.
I would however, like to see that you instead added support for the following dates:
- April 22, 2000 was a saturday.
- September 4, 2012 was a tuesday.
- May 4, 2014 (today) is a sunday.
- March 22, year -5 was a friday. (If you extrapolate the Gregorian calendar that far)
Algorithm
Your algorithm seems to be based on a restricted variant of The Doomsday Rule. As is explained on Wikipedia, you could add support for any century. No need to be limited to the 1900's.
Java 8
If possible, use Java 8! Java 8 provides the
java.time package which makes it very easy to look-up a day of week:System.out.println(Year.of(1985).atMonth(Month.JULY).atDay(2).getDayOfWeek());Outputs:
TUESDAYCode Snippets
public class DateCalc
{public class DateCalc {results(day, inputYear, month, year, intDay, janKey);Map<String, Integer> monthMap = new HashMap<String, Integer>();
monthMap.put("january" , 1);
monthMap.put("february", 4);
monthMap.put("march" , 4);
int key = monthMap.get(month.toLowerCase());
results(day, inputYear, month, year, intDay, key);enum Month {
JANUARY(1), FEBRUARY(4), MARCH(4);
public final int key;
private Month(int key) {
this.key = key;
}
}
int key = Month.valueOf(month.toUpperCase()).key;Context
StackExchange Code Review Q#48938, answer score: 7
Revisions (0)
No revisions yet.