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.
Related
I was developing this java program that tells the user the number of days in their birth month and the number of days until their birthday, the former part works and the number of days until their birthday works if it's less than three months away but I can't figure out why it doesn't work after that point
import java.util.*;
public class Jack_Dennin_HW2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("What month of the year is it?(1-12)");
int month = (sc.nextInt()-1);
System.out.println("What day of the month?");
int day = sc.nextInt();
System.out.println("What month is your birthday?(1-12)");
int bmonth = (sc.nextInt()-1);
System.out.println("What day of that month is your birthday?");
int bday = sc.nextInt();
Date today = new Date(month, day);
Date birthday = new Date(bmonth, bday);
int sum = (today.daysInMonth()-day+bday);
System.out.println("There are "+birthday.daysInMonth()+" days in your birth month");
if (month==bmonth)
{
if(day==bday)
{
System.out.println("Happy Birthday!");
}
else if(bday>day)
{
System.out.println("Your birthday is "+(bday-day)+" days away");
}
else
{
System.out.println("Your birthday is "+(365+bday-day)+" days away");
}
}
else
{
for(int i = month+1; i==(bmonth-1)%12; i++)
{
Date n = new Date(i%12,1);
sum=(sum+n.daysInMonth());
}
System.out.println("There are "+sum+" days until your birthday");
}
}
}
class Date
{
private int month;
private int day;
public Date(int month2, int day2)
{
month = month2;
day = day2;
}
public int getMonth()
{
return(month);
}
public int getDay()
{
return(day);
}
public String toString()
{
String day2;
if (day%10==day)
{
day2 = "0"+day;
}
else
{
day2 = day+"";
}
return(month+"/"+day2);
}
public int daysInMonth()
{
switch(month)
{
case 0: return(31);
case 1: return(28);
case 2: return(31);
case 3: return(30);
case 4: return(31);
case 5: return(30);
case 6: return(31);
case 7: return(31);
case 8: return(30);
case 9: return(31);
case 10: return(30);
case 11: return(31);
default: return(-1);
}
}
}
This is because of this line:
for(int i = month+1; i==(bmonth-1)%12; i++)
Specifically, i==(bmonth-1)%12.
Lets take two dates: 1st of January and 1st of February. In that case, month=0, i=0+1=1, i==1%12 is true, and you drop into the for statement and calculate the days correctly.
Lets take another two dates: 1st of January and 1st of April. In that case, month=0, i=1, but i==2%12 is false, so you do not drop into the for statement and therefore do not calculate the correct sum.
Since this is homework, I will not tell you the solution, but the line that needs to be fixed is i==(bmonth-1)%12. Its a very simple fix.
P.S. Take some time to learn how to use a debugger! :)
I need to know which day and day name is on a specific date. And somehow I made a mistake because for years with 19.. it works but at 2000 I can't get the right day anymore. For example if I use the date 9.1.2001 it doesn't say me the day, instead the error of the switch I made occurs.
and i shouldn't use the calendar methods
here is my code:
public class FindDay {
public static void main(String[] args) {
System.out.println("Type a day: ");
int day = In.readInt();
System.out.println("Type a month: ");
int month = In.readInt();
System.out.println("Type a year: ");
int year = In.readInt();
if(day < 1 || day > 32) {
System.out.println("Type valid day");
}else if (month < 1 || month >12) {
System.out.println("Type valid month");
}else if (year < 1900) {
System.out.println("Type valid year");
}
int wholeDaysYear; //to calculate how much days a year has
if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) ) {
wholeDaysYear = 366;
}else {
wholeDaysYear = 365;
}
int monthDays = 0; //calculates days to this month
int february; //if February has 28 or 2 days
if(wholeDaysYear ==366) {
february = 29;
}else {
february = 28;
}
switch(month) {
case 1: monthDays= 0; break;
case 2: monthDays =31; break;
case 3: monthDays= 31+february; break;
case 4: monthDays= 31+february+31; break;
case 5: monthDays= 31+february+31+30; break;
case 6: monthDays= 31+february+31+30+31; break;
case 7: monthDays= 31+february+31+30+31+30; break;
case 8: monthDays= 31+february+31+30+31+30+31; break;
case 9: monthDays= 31+february+31+30+31+30+31+31; break;
case 10: monthDays= 31+february+31+30+31+30+31+31+30; break;
case 11: monthDays= 31+february+31+30+31+30+31+31+30+31; break;
case 12: monthDays= 31+february+31+30+31+30+31+31+30+31+30; break;
default: System.out.println("Enter valid month!");break;
}
int leapYear = ((year-1) / 4 - 474) - ((year-1) / 100 - 18) + ((year-1) / 400 - 4); //calculates the leap years
int allDays =(((year - leapYear)-1900)*wholeDaysYear)+monthDays+day-1; //Calculates all days
System.out.println(allDays);
int dayName = allDays % 7;
String dayNames = null; //gives the days their name
switch (dayName) {
case 1: dayNames = "Monday";break;
case 2: dayNames = "Tuesday";break;
case 3: dayNames = "Wendesday";break;
case 4: dayNames = "Thursday";break;
case 5: dayNames = "Friday";break;
case 6: dayNames = "Saturday";break;
case 7: dayNames = "Sunday";break;
default: System.out.println("Type valid Input");break;
}
System.out.println(dayNames);
}
}
You don't have to work so hard... assuming you have the day of the month, month of the year and full-year, you can simply do (example):
LocalDate ld = LocalDate.of(1999, 12, 12);
System.out.println(ld.getDayOfWeek()); // prints SUNDAY
LocalDate is available since Java 8.
As for your code, two bugs that are easy to spot are:
The cases in the switch go from 1 to 7 while they should go from 0 to 6 since you're calculating the reminder out of 7.
int dayName = allDays % 7; here you assume that the first day of the year is Monday (according to your case switch) which is not necessarily true.
You can try something like this:
Date yourDate;
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH); //or Calendar.DAY_OF_WEEK
// etc.
I have a project due soon. We have to write a program that asks for a year and prints out a calendar. We can only use one while, one for, and one switch loop. (and no arrays! ugh) Im having trouble figuring out how to print out the days of each month, starting with the first day of the first week, as most months will not start on Sunday.
import java.util.*;
import java.util.Calendar;
class Lab2 {
public static void main(String[] args) {
Scanner user = new Scanner(System.in);
System.out.print("What year do you want to view? ");
int year = user.nextInt();
System.out.printf("%12d\n", year);
System.out.println();
boolean leap = isLeap(year);
int firstDay = JulianDate(year);
monthLoop(year, firstDay, leap);
}
public static boolean isLeap(int year) {
boolean verdict = false;
if (year % 100 == 0 && year % 400 == 0) {
verdict = true;
}
if(year % 100 != 0 && year % 4 == 0) {
verdict = true;
}
return verdict;
}
public static int JulianDate(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_YEAR, 1);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return dayOfWeek;
}
public static void monthLoop(int year, int firstDay, boolean leap) {
for(int i=1; i <= 12; i++) {
switch (i) {
case 1: System.out.printf("%13s\n", "January");
break;
case 2: System.out.printf("%13s\n", "February");
break;
case 3: System.out.printf("%12s\n", "March");
break;
case 4: System.out.printf("%12s\n", "April");
break;
case 5: System.out.printf("%11s\n", "May");
break;
case 6: System.out.printf("%11s\n", "June");
break;
case 7: System.out.printf("%11s\n", "July");
break;
case 8: System.out.printf("%13s\n", "August");
break;
case 9: System.out.printf("%14s\n", "September");
break;
case 10: System.out.printf("%13s\n", "October");
break;
case 11: System.out.printf("%14s\n", "November");
break;
case 12: System.out.printf("%14s\n", "December");
break;
}
System.out.println("S M Tu W Th F S");
}
}
}
You would get the day of the week, as in this post, of the first day of the month and then start the counter from there.
How to get the day of the week in Java
For instance, if the day of the week was 5, you would put 4 "blanks" before the first date. The trick is that when you are doing your mod, to determine if there should be a new line, it would be
(dayofMonth + firstDayOfWeekOfMonth) % 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);
}
}
}
The guidelines for this program follow
Problem B: Year Dates. Objectives: Understanding the Switch structure, helper methods, and using a While loop with a sentinel value.
You are to make a class called Year2012 to manipulate dates when given a month(mm), or a month plus a day(dd) as integer values. It has the following get methods: 1) MonthName which returns a String value that is the name of the Month, e.g. September, June, May, etc. 2) DaysInMonth which returns the number of days in the month. 3)DayOfTheYear which returns the ordinal year date (a number between 1-365, often called the Julian date). Hint, use a for loop to add the days in each prior month, and then add the current month's days. 4) DayOfWeek which returns a String value which is the name of the day, e.g. Monday, Tuesday, etc.
Some of these methods can be used as 'helper' methods for others. All methods will use a switch statement either directly or indirectly. Each method computes a return value from the values sent to it, therefore there are no class attributes, and only a default constructor. All logic must be contained in your own methods. (ie. You will not use existing API classes for your logic.)
Design a tester application that asks the user for a month and day, and then displays the name of the month, the number of days in the month, the day of the week for this date, and the Julian date for this day. Write your program to process dates using a While loop until a sentinel value is entered. Run your program multiple times to test out different days, but turn in a final run using the following five dates: Jan.1, Apr.18, Aug.2, Nov.28, & Dec.15.
I'm having troubles with certain parts of this program. Specifically with the Julian date method and the dayofTheWeek method. Julian date keeps printing out a 1 (I haven't tested many dates), and is a helper method to the dayofTheWeek method, could you take a look at my code and see what my problem is?
public String monthName(int month)
{
String mon = null;
switch (month)
{
case 1:
mon = "January";
break;
case 2:
mon = "February";
break;
case 3:
mon = "March";
break;
case 4:
mon = "April";
break;
case 5:
mon = "May";
break;
case 6:
mon = "June";
break;
case 7:
mon = "July";
break;
case 8:
mon = "August";
break;
case 9:
mon = "September";
break;
case 10:
mon = "October";
break;
case 11:
mon = "November";
break;
case 12:
mon = "December";
break;
default:
mon = "Inccorect entry";
break;
}
return mon;
}
public int daysInMonth(int month)
{
int days = 0;
switch (month)
{
case 1:
days = 31;
break;
case 2:
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;
default:
days = 0;
}
return days;
}
public int dayOfTheYear(int month, int day)
{
int julian = 0;
for (int count = 1; count == month; count++)
{
julian += daysInMonth(count);
}
return julian;
}
public String dayOfWeek(int month, int day)
{
int daysSoFar = dayOfTheYear(month, day);
int weekDay = daysSoFar % 7;
String dayName = null;
switch (weekDay)
{
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Incorrect entry";
}
return dayName;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Year2012 year = new Year2012();
System.out.println("Please enter a month using integers (Jan = 1): ");
int month = keyboard.nextInt();
System.out.println("Please enter a day within that month: ");
int day = keyboard.nextInt();
System.out.println("Month: " + year.monthName(month));
System.out.println("Number of days in month: " + year.daysInMonth(month));
System.out.println("Day of the week: " + year.dayOfWeek(month, day));
System.out.println("Julian date: " + year.dayOfTheYear(month, day));
}
}
You got it wrong in the for loop setting the julian value. Try this:
int julian = 0;
for (int count = 1; count < month; count++)
{
julian += daysInMonth(count);
}
return julian + day;
This loop uses count < month instead of count == month. It also returns julian + day.
You have a couple issues in the julian value calculation. Try this:
public int dayOfTheYear(int month, int day)
{
int julian = 0;
for (int count = 1; count < month; count++) //note this loop will not run for Jan(as the logic below will cover that
{
julian += daysInMonth(count);
}
julian += day;
return julian;
}
This loop uses count < month instead of count == month, and then adds the days from the input before returning the answer.
note this loop will not run for Jan as in that case you just want to add the days entered.
Your for loop condition is count == month.
public int dayOfTheYear(int month, int day)
{
int julian = 0;
for (int count = 1; count == month; count++)
{
julian += daysInMonth(count);
}
return julian;
}
This means the loop body will only execute when the month input is 1, and then only once. Did you mean count < month?