I'm working on using enum / switch case along with Zeller's formula for saying what day of the year a specific date will be. My code was printing the right days before I implemented the enum / switch portion of my code (below). After I put in the enum/ switch case, when I run it in DrJava it does prompt for the day, the month and the year, but nothing prints once it goes through the switch case
import java.util.*;
public class Zeller {
public enum DaysOftheWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
private static int value;
public Zeller (int value){
this.value = value;
}
public int getValue(){
return this.value;
}
public static void main(String[] args) {
DetermineDay(value); // Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a year, month and a day
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
System.out.print("Enter the day of the month: 1-31: ");
int day = input.nextInt();
System.out.print("Enter year (e.g., 2008): ");
int year = input.nextInt();
// Check if the month is January or February
// If the month is January and February, convert to 13, and 14,
// and year has to -1. (Go to previous year).
if (month == 1 || month == 2) {
month += 12;
year--;
}
// Compute the answer
int k = year % 100; // The year of the century
int j = (int)(year / 100.0); // the century
int q = day;
int m = month;
int h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0)
+ (int)(j / 4.0) + (5 * j)) % 7;
value = h;
System.out.println(value);
}
public static String DetermineDay(int value){
String result = "Day of the week is ";
switch (value){
case 1 :
System.out.println(result + "Sunday");
break;
case 2 :
System.out.println(result + "Monday");
break;
case 3:
System.out.println(result + "Tuesday");
break;
case 4:
System.out.println(result + "Wednesday");
break;
case 5:
System.out.println(result + "Thursday");
break;
case 6:
System.out.println(result + "Friday");
break;
case 7 :
System.out.println( result + "Saturday");
break;
default :
System.out.println ("Looks like that day doesn't exist");
break;
}
return result;
}
}
If you want to output the day using DetermineDay you need to call that method at the end after you did the calculation and assigned the result to value.
This seems to work but there is a problem in your algorithm when trying this program with the date 4/11/2016 it does find that it is a Friday, but when using the date 5/5/2016 which is today the output is ¨Looks like that day doesn't exist¨, so yeah there is that.
At the end of DetermineDay you dont need to return a result since you already sop the result inside the switch.
import java.util.*;
public class Zeller {
public enum DaysOftheWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
private static int value;
public Zeller (int value){
this.value = value;
}
public int getValue(){
return this.value;
}
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a year, month and a day
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
System.out.print("Enter the day of the month: 1-31: ");
int day = input.nextInt();
System.out.print("Enter year (e.g., 2008): ");
int year = input.nextInt();
// Check if the month is January or February
// If the month is January and February, convert to 13, and 14,
// and year has to -1. (Go to previous year).
if (month == 1 || month == 2) {
month += 12;
year--;
}
// Compute the answer
int k = year % 100; // The year of the century
int j = (int)(year / 100.0); // the century
int q = day;
int m = month;
int h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0) + (int)(j / 4.0) + (5 * j)) % 7;
value = h;
System.out.println(value);
DetermineDay(value);
}
public static void DetermineDay(int value){
String result = "Day of the week is ";
switch (value){
case 1 :
System.out.println(result + "Sunday");
break;
case 2 :
System.out.println(result + "Monday");
break;
case 3:
System.out.println(result + "Tuesday");
break;
case 4:
System.out.println(result + "Wednesday");
break;
case 5:
System.out.println(result + "Thursday");
break;
case 6:
System.out.println(result + "Friday");
break;
case 7 :
System.out.println( result + "Saturday");
break;
default :
System.out.println ("Looks like that day doesn't exist");
break;
}
}
}
Related
How do i take the user inputted day, month and year then store in a dd-mm-yyyy format? I can get everything to work except combining them into a date and storing them in the startDate variable. I also don't think startMonth will be accessible as it's in a switch case but I'm very new to java and unsure.
public static void carBookingDates() {
int carNumber;
int startYear;
int startDay;
int startMonth;
int daysInMonth = 0;
int endYear;
int endMonth;
int endDay;
Scanner input = new Scanner(System.in);
System.out.println("To make a booking:");
System.out.printf(" Select a car number from the car list: ");
carNumber = input.nextInt();
System.out.println("");
System.out.println("Enter a booking start date.");
System.out.printf("Please enter the year - for example '2022': ");
startYear = input.nextInt();
while (startYear < 2022 || startYear > 2030) {
System.out.println("Invalid year, please try again: ");
System.out.printf("Please enter the year - for example '2022': ");
startYear = input.nextInt();
}
System.out.printf("Please enter the month - for example '6': ");
startMonth = input.nextInt();
while (startMonth < 1 || startMonth > 12) {
System.out.println("Invalid month, please try again: ");
System.out.printf("Please enter the month - for example '6': ");
startMonth = input.nextInt();
}
switch (startMonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2:
if (((startYear % 4 == 0)
&& !(startYear % 100 == 0))
|| (startYear % 400 == 0)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.printf("Please enter the day number - for"
+ " example '18': ");
startDay = input.nextInt();
while (startDay > daysInMonth || startDay <= 0) {
System.out.println("Invalid day, plese try again");
System.out.printf("Please enter the day number - for"
+ " example '18': ");
startDay = input.nextInt();
LocalDate startDate() = LocalDate.parse(startDay + "-" + StartMonth "-" + startYear);
}
}
First of all, you can use the Java 8 Date Time API to get the number of days in a month instead of using a manual switch case for making the code more readable.
import java.time.YearMonth;
Then:
// 2021 is the year and 2 is the month
int daysInMonth = YearMonth.of(2021,2).lengthOfMonth());
Now to form the date in dd-mm-yyyy format :
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Then:
// pass your date value here under LocalDate.of method
LocalDate.of(2021,2,16).format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
I want to show jalali calendar and hijri calendar at the same time, but i don't know how to match jalali days with hijri days. I searched and found the source code below. It shows jalali calendar but I don't know how to match two calendars. Hijri calendar does'nt have any specific pattern for months.
import javax.swing.JOptionPane;
public class TestCal {
/** Main method */
public static void main(String[] args) {
// Prompt the user to enter year
String yearString = JOptionPane.showInputDialog(
"Lotfan Sale morede nazar ra vared konid (Masalan 1390):");
// Convert string into integer
int year = Integer.parseInt(yearString);
// Prompt the user to enter month
String monthString = JOptionPane.showInputDialog(
"Lotfan mahe khod ra niz az miane adade 1 ta 12 bargozinid:");
// Convert string into integer
int month = Integer.parseInt(monthString);
// Print calendar for the month of the year
printMonth(year, month);
}
/** Print the calendar for a month in a year */
static void printMonth(int year, int month) {
// Print the headings of the calendar
printMonthTitle(year, month);
// Print the body of the calendar
printMonthBody(year, month);
}
/** Print the month title, e.g., May, 1999 */
static void printMonthTitle(int year, int month) {
System.out.println(" " + getMonthName(month)
+ " " + year);
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/** Get the English name for the month */
public static String getMonthName(int month) {
String monthName = null;
switch (month) {
case 1: monthName = "Farvardin"; break;
case 2: monthName = "Ordibehesht"; break;
case 3: monthName = "Khordad"; break;
case 4: monthName = "Tir"; break;
case 5: monthName = "Mordad"; break;
case 6: monthName = "Shahrivar"; break;
case 7: monthName = "Mehr"; break;
case 8: monthName = "Aban"; break;
case 9: monthName = "Azar"; break;
case 10: monthName = "Dey"; break;
case 11: monthName = "Bahman"; break;
case 12: monthName = "Esfand";
}
return monthName;
}
/** Print month body */
public static void printMonthBody(int year, int month) {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numberOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
/** Get the start day of the first day in a month */
public static int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1300 = 1;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
// Return the start day
return (totalNumberOfDays + startDay1300) % 7;
}
/** Get the total number of days since Jan 1, 1800 */
static int getTotalNumberOfDays(int year, int month) {
int total = 0;
// Get the total days from 1800 to year - 1
for (int i = 1300; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
// Add days from Jan to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumberOfDaysInMonth(year, i);
return total;
}
/** Get the number of days in a month */
public static int getNumberOfDaysInMonth(int year, int month) {
if (month <= 6 && month > 0)
return 31;
else if (month < 12 && month > 6)
return 30;
if (month == 12) return isLeapYear(year) ? 30 : 29;
return 0; // If month is incorrect
}
/** Determine if it is a leap year */
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 3 && year % 100 != 0);
}
}
A very simple approach:
Take a common epoch between two calendars
Develop/Find algorithms to convert calendar date to/from number of days between that epoch.
Convert date from calendar type A to epoch-delta and epoch-delta to calendar type B.
I have implemented a simple C library to convert between dates in various calendars. The code is available here. You can extract algorithms and implement them in your own language of choice. In case you are using C/C++, you can simply:
#include <calendars/cl-calendar.h>
int16_t year;
uint8_t month;
uint16_t day;
convert_date(CAL_ISLAMIC_CIVIL, CAL_SOLAR_HIJRI,
1396, 7, 11, &year, &month, &day);
printf("1396/07/11 AP is: %d/%d/%d Hijri", year, month, day);
which will print:
$ 1396/07/11 AP is: 1439/1/12 Hijri
Notes:
Jalali calendar is not being used anymore. You probably want to use Solar Hijri Calendar.
Hijri Calendar is not tabular. You probably mean Islamic Civil Clanndar
We're supposed to make a program in computer science that figures out what day of the week you were born on. We were given these instructions.
1) Start with the last two digits of the year in which you were born.
2) Divide the above number by 4, dropping the remainder if there is one.
3) Find the number associated with the month in which you were born in the Table of Months.
4) On which day of the month is your birthday?
5) Find the sum of the four numbers obtained in steps 1 through 4.
6) Divide the sum by the number 7. The day that corresponds to the remainder in the Table of Days is the day of the week you were born on.
Table of Months Table of Days
January 1 (0 in leap year) Sunday 1
February 4 (3 in leap year) Monday 2
March 4 Tuesday 3
April 0 Wednesday 4
May 2 Thursday 5
June 5 Friday 6
July 0 Saturday 0
August 3
September 6
October 1
November 4
December 6
I've been having trouble with is incomparable types: int and boolean, i don't know what the problem is, ive tried changing variables to boolean, but it doesn't solve anything.
package LeapYear;
import java.util.*;
public class Birthday
{
static Scanner in= new Scanner(System.in);
public static void main (String []args)
{
int year=getYear();
int month=getMonth(year);
int day=getDay();
int total=computeDay(day, month, year);
int dayofbirth=dayOfWeek(day,month,year);
}//end main
public static int getYear()
{
System.out.println("Please enter the last two digits of the year you were born:");
int y=in.nextInt();
return y;
}
public static int getMonth(int year)
{
int m;
System.out.println("Please select the month in which you were born:"+
"1.)Januaryn\n"+
"2.)February\n"+
"3.)March\n"+
"4.)April\n"+
"5.)May\n"+
"6.)June\n"+
"7.)July\n"+
"8.)August\n"+
"9.)September\n"+
"10.)October\n"+
"11.)November\n"+
"12.)December\n");
m=in.nextInt();
System.out.println("You entered " + m);
switch(m)
{
case 1: if(year ==true)
return 0;
else
return 1 ;
break;
case 2:if(year == true)
return 3;
else
return 4;
break;
case 3:return 4;
break;
case 4:return 0;
break;
case 5:return 2;
break;
case 6:return 5;
break;
case 7:return 0;
break;
case 8:return 3;
break;
case 9:return 6;
break;
case 10:return 1;
break;
case 11:return 4;
break;
case 12:return 6;
break;
}//end case
}//end getMonth
public static int getDay()
{
int d;
System.out.println("Please enter the day on which you were born");
d=in.nextInt();
return d;
}//end getDay
public static int computeDay(int day, int month, int year)
{
int weekday;
int y2=year/4;
int m2= month + y2 + day;
int total=m2/7;
return total;
}//end computeDay
public static int dayOfWeek(int day, int month, int year, int total)
{
int dob;
switch(dob)
{
case 1:if (total=1)
System.out.println("You were born on a Sunday");
break;
case 2:if (total=2)
System.out.println("You were born on a Monday");
break;
case 3: if (total=3)
System.out.println("You were born on a Tuesday");
break;
case 4: if(total=4)
System.out.println("You were born on a Wednesday");
break;
case 5: if(total=5);
System.out.println("You were born on a Thursday");
break;
case 6: if (total=6);
System.out.println("You were born on a Friday");
break;
case 7: if (total=7);
System.out.println("You were born on a Saturday");
break;
}
}//end dayOfWeek
}//end class
if(year == true) cannot compile because an integer cannot be true. Do something useful with that statement like:
if(year < 50)
{
System.out.println("Wow, you're old!");
return 0;
}
Because you are only getting the last two digits of their year of birth, you will have trouble deciding if the user was born in the year 1905 or 2005. This is an unlikely case, but you would not be able to decipher whether the user was born this century or the last. If it is acceptable not to know, don't change it. If not, you will need to get more information from the user.
My code asks for the user to enter the day with an integer value ie Sunday = 0, Monday = 1, etc., then asks the user to input a number representing an offset day. From there the program finds the day corresponding to the offset number ie
Enter today's day: 0 //Sunday
Enter the number of days elapsed since today: 6
6 days from Sunday is Sunday
The problem with the above output is that since I'm using, "%6" to find the offset day, it skips one day for values 6.
Also, I can't figure a way to find the day for an entered negative offset value. ie
Enter today's day: 0 //Sunday
Enter the number of days elapsed since today: -2
-2 days from Sunday is Friday
Here's my code
import java.util.Scanner ;
public class test {
public static void main (String[] args) {
Scanner Input = new Scanner(System.in);
System.out.print("Enter today's day: ");
int today = Input.nextInt();
System.out.print("Enter the number of days elapsed since today: ");
int offset = Input.nextInt ();
int offsetDay = 0;
if (offset >= 0)
offsetDay = (today + offset)% 6 ; //to get remainder and make it vary from 0 - 6
else if (offset < 0)
offsetDay = (today - offset)% 6 ;
String todayText = null ;
String offsetText = null;
//Converting input integers into days in text for variable today
switch (today) {
case 0 : todayText = "Sunday" ; break;
case 1 : todayText = "Monday"; break;
case 2 : todayText = "Tuesday"; break;
case 3 : todayText = "Wednesday";break;
case 4 : todayText = "Thrusday"; break;
case 5 : todayText = "Friday"; break;
case 6 : todayText = "Saturday"; break;
}
//Converting input integers into days in text for variable offset
switch (offsetDay) {
case 0 : offsetText = "Sunday" ; break;
case 1 : offsetText = "Monday"; break;
case 2 : offsetText = "Tuesday"; break;
case 3 : offsetText = "Wednesday";break;
case 4 : offsetText = "Thrusday"; break;
case 5 : offsetText = "Friday"; break;
case 6 : offsetText = "Saturday"; break;
}
System.out.println(offset + " days from " + todayText + " is " + offsetText);
}
}
My last question is, how would I implement this using enumeration? Any thoughts?
Please, try this code snippet, maybe this will help you:
public class Days
{
enum DAY
{
MON("Monday"),
TUE("Tuesday"),
WED("Wednesday"),
THU("Thursday"),
FRI("Friday"),
SAT("Saturday"),
SUN("Sunday");
private String name;
private DAY(String name)
{
this.name= name;
}
#Override
public String toString()
{
return this.name;
}
public DAY add(int days)
{
int posMod = (this.ordinal() + days) % values().length;
if (posMod < 0)
{
posMod += values().length;
}
return DAY.values()[posMod];
}
}
public static void main(String[] args)
{
DAY a = DAY.values()[0];
System.out.println(a);
System.out.println(a.add(-2));
System.out.println(a.add(0));
System.out.println(a.add(-8));
}
}
You should use %7 instead of %6 as counting from 0-6 inclusive is 7.
i am making a program that reads in the month 1-12 and the start day, 1-7 for sunday, monday, etc. it prints the first week fine but im having problems printing the rest of the days after the first week. it wont format the output to have a space between the rest of the days, only the first day after the first week.
side note: Keyboard is a class i have included in my project
heres my code:
import java.io.*;
public class CalendarMonth
{
static final int WEEK = 7;
public static void main(String[] args) throws IOException
{
String proceed;
char repeatCheck;
int days;
String leapYear;
char leapYearCheck;
int startDay;
do
{
days = getDays();
System.out.println(days);
System.out.println("Please enter a number 1-7 that the month starts on: (1 for sunday, 2 for monday, etc)");
startDay = Keyboard.readInt();
while(startDay < 1 || startDay > 7)
{
System.out.println("You did not enter a number 1-7, Try again: ");
startDay = Keyboard.readInt();
}
System.out.println(" s m t w th f sa");
printMonth(days,startDay);
System.out.println("\nWould you like to print another month?");
proceed = Keyboard.readString();
repeatCheck = proceed.charAt(0);
}while(repeatCheck == 'y');
}
public static int getDays() throws IOException
{
int month;
String leapYear;
int startDay;
int days = 0;
char leapYearCheck;
System.out.println("Please input a number 1-12 to print the corresponding month: ");
month = Keyboard.readInt();
while (month < 1 || month > 12)
{
System.out.println("You did not put a number 1-12, Try again: ");
month = Keyboard.readInt();
}
switch(month)
{
case 1: days = 31;
break;
case 2:
System.out.println("is it a leap year? ");
leapYear = Keyboard.readString();
leapYearCheck = leapYear.charAt(0);
while(leapYearCheck != 'y' && leapYearCheck != 'n')
{
System.out.println("you did not enter a yes or no answer, is it a leap year?");
leapYear = Keyboard.readString();
leapYearCheck = leapYear.charAt(0);
}
if (leapYearCheck == 'y')
{
days= 29;
}
else
{
days = 28;
}
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}
return days;
}
public static void printMonth(int days, int startDay)
{
int count;
count = startDay;
for (int counter = 1; counter <= 7; counter++)
{
if (counter < startDay)
{
System.out.print(" ");
count = count-1;
}
else
{
System.out.printf("%2d", count);
count++;
}
}
//int restOfTheMonth = (WEEK - startDay);
System.out.println();
for(int restOfTheMonth = count; restOfTheMonth <= days; restOfTheMonth++)
{
if (restOfTheMonth%WEEK==0)
{
System.out.println();
}
System.out.printf("%2d", restOfTheMonth);
}
}
}