What's wrong with my code? Zeller's Congruence algorithm code - java

What's wrong with my code? For some reason I keep on getting the day off by one day? For example, today is the 26th of 2013 and it's a Tuesday, but the program tells me it's a Wednesday. I am using Zeller's Congruence algorithm.
import javax.swing.JOptionPane;
public class zeller {
public static void main(String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");
int year = Integer.parseInt(yearString);
String monthString = JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);
String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);
switch (month) {
case 13: monthString = "January";
break;
case 14: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
int j = year / 100;
int k = year % 100 ;
double h = (day + ((26*(month + 1)) / 10) + k + (k / 4) +(j / 4) + (5 * j)) % 7;
int h1 = (int)h;
switch (h1) {
case 0: dayString = "Saturday";
break;
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
default: monthString = "Invalid month";
break;
}
System.out.println("Day of the week is: " + dayString);
}
}

The months Jan and Feb are counted as the 13th and 14th month of the previous year, so
if you want the user to enter the actual months of 1 or 2 for Jan or Feb you could add code like this:
if (month == 1 ){
month = 13;
year -=1;
}
else if (month == 2) {
month = 14;
year -=1;
}

You need to modify the year if it is January or February. Explanation on wikipedia:
One can readily see that, in a given year, March 1 (if that is a
Saturday, then March 2) is a good test date; and that, in a given
century, the best test year is that which is a multiple of 100. Zeller
used decimal arithmetic, and found it convenient to use J and K in
representing the year. But when using a computer, it is simpler to
handle the modified year Y, which is Y - 1 during January and
February:
I modified your code as follows and it works:
import javax.swing.JOptionPane;
public class zeller {
public static void main(final String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");
int year = Integer.parseInt(yearString);
String monthString =
JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);
String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);
switch (month) {
case 14:
year--;
monthString = "January";
break;
case 13:
year--;
monthString = "February";
break;
case 3:
monthString = "March";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "December";
break;
default:
monthString = "Invalid month";
break;
}
int j = year / 100;
int k = year % 100;
double h = (day + ((13 * (month + 1) / 5)) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
int h1 = (int) h;
switch (h1) {
case 0:
dayString = "Saturday";
break;
case 1:
dayString = "Sunday";
break;
case 2:
dayString = "Monday";
break;
case 3:
dayString = "Tuesday";
break;
case 4:
dayString = "Wednesday";
break;
case 5:
dayString = "Thursday";
break;
case 6:
dayString = "Friday";
break;
default:
monthString = "Invalid month";
break;
}
System.out.println("Day of the week is: " + dayString);
}
}

From Wikipedia,
In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is February 2, 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format).
So there is technically nothing wrong with your code; putting in 02/26/2013 (today) as the 26th day of the 14th month of 2013 is actually calculating the day of the week of 02/26/2014.

Related

How to make this if else-if statement into a short form in Java?

public static double eMED(int emo) {
double result = 0;
if (emo >= 40 && emo < 60) {
result = upFunc(emo, 40, 60);
} else if (emo > 60 && emo <= 80) {
result = downFunc(emo, 60, 80);
} else if (emo == 60) {
result = 1;
}
return result;
}
Your code already looks short. You can't make it shorter. You can use ternary operator but the code becomes barely readable.
return emo == 60 ? 1
: emo >= 40 && emo < 60 ? upFunc(emo, 40, 60)
: emo > 60 && emo <= 80 ? downFunc(emo, 60, 80)
: 0;
use case it is short
example :
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}

Creating Constructors and Methods with Months

I fixed my setMonthnum method, but now my input will set the month number to 0, 13, etc. based off of my input. I need to know how to go and ask again for input and not to set my Monthnum to the incorrect input. If you have any suggestions to simply better my code, please feel free to state them! My code is as follows:
import java.util.Scanner;
public class whichMonth {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the month name or number: ");
int monthNumber = input.nextInt();
//set up variable access to the class
AnyMonth inputMonthNumber = new AnyMonth();
// set the month number from user input
inputMonthNumber.setMonthnum.input.nextInt(monthNumber);
// get the month name from user input number
String monthName = inputMonthNumber.getMonthName(monthNumber);
}
}
class AnyMonth {
int Monthnum;
String monthName;
public AnyMonth() {
Monthnum = 1;
}
public AnyMonth(int currentMonthNumber) {
Monthnum = currentMonthNumber;
switch(currentMonthNumber) {
case 1:
Monthnum = 1;
monthName = "January";
System.out.println("January");
break;
case 2:
Monthnum = 2;
monthName = "February";
System.out.println("February");
break;
case 3:
Monthnum = 3;
monthName = "March";
System.out.println("March");
break;
case 4:
Monthnum = 4;
monthName = "April";
System.out.println("April");
break;
case 5:
Monthnum = 5;
monthName = "May";
System.out.println("May");
break;
case 6:
Monthnum = 6;
monthName = "June";
System.out.println("June");
break;
case 7:
Monthnum = 7;
monthName = "July";
System.out.println("July");
break;
case 8:
Monthnum = 8;
monthName = "August";
System.out.println("August");
break;
case 9:
Monthnum = 9;
monthName = "September";
System.out.println("September");
break;
case 10:
Monthnum = 10;
monthName = "October";
System.out.println("October");
break;
case 11:
Monthnum = 11;
monthName = "November";
System.out.println("November");
break;
case 12:
Monthnum = 12;
monthName = "December";
System.out.println("December");
break;
default:
Monthnum = 1;
monthName = "January";
}
}
public AnyMonth(String userMonthName) {
switch(userMonthName) {
case "January":
Monthnum = 1;
monthName = "January";
System.out.println("January");
break;
case "February":
Monthnum = 2;
monthName = "February";
System.out.println("February");
break;
case "March":
Monthnum = 3;
monthName = "March";
System.out.println("March");
break;
case "April":
Monthnum = 4;
monthName = "April";
System.out.println("April");
break;
case "May":
Monthnum = 5;
monthName = "May";
System.out.println("May");
break;
case "June":
Monthnum = 6;
monthName = "June";
System.out.println("June");
break;
case "July":
Monthnum = 7;
monthName = "July";
System.out.println("July");
break;
case "August":
Monthnum = 8;
monthName = "August";
System.out.println("August");
break;
case "September":
Monthnum = 9;
monthName = "September";
System.out.println("September");
break;
case "October":
Monthnum = 10;
monthName = "October";
System.out.println("October");
break;
case "November":
Monthnum = 11;
monthName = "November";
System.out.println("November");
break;
case "December":
Monthnum = 12;
monthName = "December";
System.out.println("December");
break;
default:
Monthnum = 1;
monthName = "January";
}
}
public void setMonthnum (int userMonth) {
Monthnum = userMonth;
if (userMonth < 1 || userMonth > 12){
System.out.println("Invalid input");
this.Monthnum = userMonth;
}
}
public int getMonthnum(int currentMonthNumber) {
Monthnum = currentMonthNumber;
return currentMonthNumber;
}
public String getMonthName(int currentMonthName) {
Monthnum = currentMonthName;
// use swtich-case here instead, default case should return 1/Jan
switch(currentMonthName) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";;
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
monthName = "January";
}
return monthName;
}
public String toString(String monthNameToString){
monthName = monthNameToString;
return monthNameToString;
}
public boolean equals(int month) {
if (Monthnum == month) {
return true;
}
else {
return false;
}
}
public boolean greaterThan(int month) {
if (Monthnum > month) {
return true;
}
else {
return false;
}
}
public boolean lessThan(int month) {
if (Monthnum < month) {
return true;
}
else {
return false;
}
}
}
change
inputMonthNumber.setMonthnum.input.nextInt(monthNumber);
to
inputMonthNumber.setMonthnum(monthNumber);
Also, in all the getXXX methods, remove the below statement:
Monthnum = currentMonthName;
Getters are desiged to return the value. Setting the values via getter methods is not a good practice. This article explains why getters and setters are needed any how to write them :)

Math error switch always ends up zero

I am writing a small program to find the day of the week using gregorian math. The following code always outputs a zero into my switch statement resulting in the same output each time.
This is my code:
iSum = (CentCode + iYear + (iYear /4) + MonthCode + iDay);
iOutput %= iSum;
switch (iOutput)
{
case 0:
sDay = "sunday";
break;
case 1:
sDay = "monday";
break;
case 2:
sDay = "tuesday";
break;
case 3:
sDay = "wednesday";
break;
case 4:
sDay = "thursday";
break;
case 5:
sDay = "friday";
break;
case 6:
sDay = "saturday";
break;
}
iOutput %= iSum;
is the short form of
iOutput = iOutput % iSum;
Which is not what you want. I guess you want something like
iOutput = iSum % 7;

Switch Statement Reverse

Okay so i'm trying to complete this project for school and it is asking me to make a switch statement of the months. So if a user enters 1 it will print out January and so on... I get this error where it says: cannot find symbol - variable January
import java.util.*;
/**
* Outputs the number on a month name entered by the user.
*
* #author Jack
* #version 1a
*/
public class MonthSwitchReverse {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int monthString;
String month;
System.out.println(" Jan = 1 / Feb = 2 / March = 3 / April = 4 / MAy = 5 / June = 6");
System.out.println(" July = 7 / Aug = 8 / Sep = 9 / Oct = 10 / Nov = 11 / Dec = 12");
System.out.print("Choose a month above and the system will print out the number assigned to that month: ");
month = in.next();
switch (month) {
case "January": monthString = 1;
break;
case "February": monthString = 2;
break;
case "March": monthString = 3;
break;
case "April": monthString = 4;
break;
case "May": monthString = 5;
break;
case "June": monthString = 6;
break;
case "July": monthString = 7;
break;
case "August": monthString = 8;
break;
case "September": monthString = 9;
break;
case "October": monthString = 10;
break;
case "November": monthString = 11;
break;
case "December": monthString = 12;
break;
default: monthString = 404;
break;
}
System.out.println(monthString);
}
}
EDIT: I fixed it. Thanks for your help.
Since you are passing an integer to switch , That each case should be an int value.
For example
case 1: monthString = "1";
case 2: monthString = "2";
And same for remaining all.
recommending to read : Switch in java.

Okay, so my code is compiled but the if/else statement is not working

I think it might be due the switch statement.
Do I have make the both them switch statements for them to work out.
import java.util.Scanner;
public class mylab
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int month;
int day;
String season= "seasons";
System.out.print("type a two digit month");
System.out.print(" and day");
month = in.nextInt();
day = in.nextInt();
String winter = " winter ";
String summer = " summer";
String spring = " spring";
System.out.print(" Month="+ month +" Day= "+day);
switch (month) {
case 1:
month = 1; System.out.println(" Winter");
break;
case 2:
month = 2; System.out.println(" Winter");
break;
case 3:
month= 3;System.out.println(" Winter");
break;
case 4:
month= 4;System.out.println(" Spring");
break;
case 5:
month = 5;System.out.println(" Spring");
break;
case 6:
month = 6 ;System.out.println(" Spring");
break;
case 7:
month = 7 ;System.out.println(" Summer");
break;
case 8:
month = 8;System.out.println(" Summer");
break;
case 9:
month = 9;System.out.println(" Summer");
break;
case 10:
month = 10;System.out.println(" Fall");
break;
case 11:
month = 11;System.out.println(" Fall");
break;
case 12:
month = 12;System.out.println(" Fall");
break;
}
How , do I make this part work with the switch statement
the pseudo code for this portion is If month is divisible by 3 and day >= 21, If season is "Winter", season = "Spring",Else if season is "Spring", season = "Summer",Else if season is "Summer", season = "Fall"
Else season = "Winter"
if (month % 3 == 0 && day >= 21)
{
if ( season.equals(winter) )
System.out.println(" Spring");
else if ( season.equals(spring) )
System.out.println ( "Summer" );
else if ( season.equals(summer) )
System.out.println ( " fall");
else if ( season.equals(winter) )
System.out.println( " winter");
}
}
}
This is how I'd probably write it (if I absolutely had to keep the switch and I didn't care to check user input):
import java.util.Scanner;
public class mylab {
public static void main(String[] args) {
int month, day;
Scanner in = new Scanner(System.in);
System.out.print("Type a two digit month: ");
month = in.nextInt();
System.out.print("Type a two digit day: ");
day = in.nextInt();
System.out.print(" Month="+ month +" Day= "+day+" ");
if(month%3==0 && day>=21) {
month++;
if(month>12) month=1;
}
switch (month) {
case 1: case 2: case 3: System.out.println("Winter"); break;
case 4: case 5: case 6: System.out.println("Spring"); break;
case 7: case 8: case 9: System.out.println("Summer"); break;
case 10: case 11: case 12: System.out.println("Fall"); break;
}
}
}

Categories

Resources