7 Replies - 9100 Views - Last Post: 03 February 2011 - 05:40 PM Rate Topic: -----

#1 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Day of Week

Posted 02 February 2011 - 10:16 PM

Hey guys, I just started taking my first Java class this term, and we have a homework problem where we need to create a simple date class. We are not allowed to use classes that have already been created, besides the string class. I am having trouble getting the getDayOfYear function to work properly as well as the getDayOfWeek function. They both work at some points, and fail at others. We were given a driver to test our code, so the error isn't coming from that.

Here is the driver:
// Test driver to use the MyDate class to calculate the day of the year
//  and day of the week for a date given as year (as NNNN), month (as 1-12)
//  and a day (1-31) of month

import java.util.*;
import java.text.SimpleDateFormat;

public class FindDay2 {
    // Main method gets date as input, creates a MyDate object, and calls
    // methods to determine day of year and day of week.
    public static void main(String[] args) {
        // Prompt the user for month, day, and year
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a date as month(1-12), day(1-31), year ("
           + MyDate.YEAR_FIRST + "-" + MyDate.YEAR_LAST + "): " );
        int month = input.nextInt();
        int day = input.nextInt();
        int year = input.nextInt();

        // Create a MyDate object and check that it is a valid date
        MyDate mydate = new MyDate(month, day, year);
        if (!mydate.isValidDate()) {
            System.out.println("Invalid date: " + mydate);
            System.exit(1);
        }

        // Calculate the day number in the year
        int dayOfYear = mydate.getDayOfYear();

        // Calculate the day of the week
        String dayOfWeek = mydate.getDayOfWeek();

        System.out.println(mydate + " is day number " + dayOfYear
            + " of the year" + " which is a " + dayOfWeek);


        // Use Java library classes to check our results
        Calendar calendar = new GregorianCalendar(year, month-1, day);
        SimpleDateFormat formatter =
          new SimpleDateFormat("EEE, MMM d, yyyy 'is day number' D 'of the year'");
        System.out.println("Java: " + formatter.format(calendar.getTime()));

    }
}



and here is the code that I have written for the MyDate class:
/**
 * @(#)MyDate.java
 *
 *
 * @author 
 * @version 1.00 2011/2/1
 */

public class MyDate {
	private int month, day, year;
	public static int YEAR_FIRST = 1600, YEAR_LAST = 2400;
	
	//the constructor of the MyDate object.
    public MyDate(int month, int day, int year) {
    	this.month = month;
    	this.day = day;
    	this.year = year;
    }
    
    // if the date the user enters is a valid date, then it returns true
    // otherwise, it returns false.
    public boolean isValidDate(){
    	
    	if(this.month < 1 || this.month > 12) return false;
    	
    	if(this.year < 1600 || this.year > 2400) return false;
    	
    	// minimum number of days is 1
    	// max number of days is 31
    	if(this.day < 1 || this.day > 31) return false;
    	
		if(this.month == 2){
			//if the month is February, check for a leap year
			if(isLeapYear()){
				if(this.day > 29) return false;
			}else{
				if(this.day > 28) return false;
			}
    	}
    	// if the month is April, June, September, or November, it can have a max of 30 days
    	if(this.month == 3 || this.month == 6 || this.month == 9 || this.month == 11){
    		if(day > 30) return false;
    	}
    	return true;
    }
    // check if the year is a leap year
    public boolean isLeapYear(){
    	if(this.year % 4 == 0 || ( this.year % 100 == 0 && this.year % 400 != 0)){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    public int getDayOfYear(){
    	// if the month is january,
    	// no special checking needed.
    	if(this.month == 1) return this.day;
    	// if the month is February
    	// just return 31 plus the number of days
    	if(this.month == 2) return this.day + 31;
    	int totalDays = 0;
    	// add 31 for every month up to the current month
    	for(int i = 1; i < this.month; i++){
    		totalDays += 31;
    	}
    	// check for months that only have 30 days in them
    	// and subtract 1 for each month that only has 30 days
    	for(int i = 1; i < this.month; i++){
    		if(i == 3 || i == 6 || i ==9 || i == 11) totalDays -= 1;
    	}
    	// check if leap year, and that the month of February is past
    	// if leap year, subtract 2 days, otherwise subtract 3 days
    	if(isLeapYear() && this.month > 2){
    		totalDays -= 2;
    	}else{
    		totalDays -= 3;
    	}
    	// at the current month, add the total number of 
    	// days into the month
    	totalDays += this.day;
    	// return days :D/>
    	return totalDays;
    }
    
    
    // get the day of the week
    public String getDayOfWeek(){
    	double totalDays = 0;
    	int firstDay = 1;
    	for(int i = 1600; i < this.year; i++){
    		totalDays += 365;
    		firstDay += 1;
    		if(firstDay == 8)firstDay = 1;
    		if(this.year % 4 == 0 || ( this.year % 100 == 0 && this.year % 400 != 0)){
    			totalDays += 1;
    			firstDay += 1;
    			if(firstDay == 8)firstDay = 1;
    		}
    		
    	}
    	totalDays += getDayOfYear();
    	int today = (int) totalDays  % firstDay;
    	String thisDay = "";
    	switch(today){
    		case 0:
    			thisDay = "Saturday";
    			break;
    		case 1:
    			thisDay = "Sunday";
    			break;
    		case 2:
    			thisDay = "Monday";
    			break;
    		case 3:
    			thisDay = "Tuesday";
    			break;
    		case 4:
    			thisDay = "Wednesday";
    			break;
    		case 5:
    			thisDay = "Thursday";
    			break;
    		case 6:
    			thisDay = "Friday";
    			break;
    		default:
    			thisDay = "";
    			break;
    	}
    	return thisDay;
    }
    
    // converts the object into a string
    public String toString(){
    	return this.month + "/" + this.day + "/" + this.year;
    }
}



Any help is greatly appreciated. :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Day of Week

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Day of Week

Posted 03 February 2011 - 05:25 AM

What exactly is the problem? Are you getting any error messages? if yes, please post them.
Was This Post Helpful? 0
  • +
  • -

#3 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Day of Week

Posted 03 February 2011 - 09:53 AM

I managed to fix the problem with the getDayOfYear function, but the problem that I'm having with the getDayOfWeek function is that it comes out with the wrong day of the week. That's the only problem that I'm having.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,570
  • Joined: 27-December 08

Re: Day of Week

Posted 03 February 2011 - 10:26 AM

What is the input you provide? What is the output you are getting? What output do you expect? Please specifically describe your problems or errors.
Was This Post Helpful? 0
  • +
  • -

#5 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Day of Week

Posted 03 February 2011 - 11:03 AM

The input I provide is a month represented by an integer between 1 and 12, a day represented by an integer between 1 and 31, and a year, represented by an integer between 1600 and 2400.

The output that I expect, is to get the date in the format mm/dd/yyyy, with the text " is day number " followed by the number of days into the year that the date provided is, followed by the text " which is a " with the day of the week.

The problem that I am having is that the day of the week is not coming out to what it should be. I'll enter a random date, and it will come out with the format perfect, the day of the year perfect, but not the day of the week. It works for some dates, but not all.

The driver that we were provided to test the code uses the Java Date class so as to help us get the code right.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,570
  • Joined: 27-December 08

Re: Day of Week

Posted 03 February 2011 - 11:10 AM

So what's a specific input that works? What is the output you get? No do the same for an input that doesn't work, and provide the output you expect as well.
Was This Post Helpful? 0
  • +
  • -

#7 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Day of Week

Posted 03 February 2011 - 11:14 AM

An input that works is: 1 1 1600 and it returns the output 1/1/1600 is day number 1 of the year which is a Saturday

An input that doesn't work is: 2 25 1980 and it returns the output 2/25/1980 is day number 56 of the year which is a Saturday
The expected output for this is: 2/25/1980 is day number 56 of the year which is a Monday
Was This Post Helpful? 0
  • +
  • -

#8 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Day of Week

Posted 03 February 2011 - 05:40 PM

I found out why it wasn't working. It was because when I was checking for a leap year, I was including years that weren't leap years, like 1700 for example. Also, when I mod totalDays, it needed to be by 7 and not by the first day of each year. I changed the configuration of my switch statement around so that Saturday would be the case when the integer today was equal to 1.

The final code for my MyDate.java file is this:
/**
 * @(#)MyDate.java
 *
 *
 * @author Anthony Smith
 * @version 1.0.0 2011/2/3
 */

public class MyDate {
	private int month, day, year;
	public static int YEAR_FIRST = 1600, YEAR_LAST = 2400;
	
	//the constructors of the MyDate object.
    public MyDate(int month, int day, int year) {
    	this.month = month;
    	this.day = day;
    	this.year = year;
    }
    public MyDate(){
    	this.month = 1;
    	this.day = 1;
    	this.year = 1600;
    }
    // if the date the user enters is a valid date, then it returns true
    // otherwise, it returns false.
    public boolean isValidDate(){
    	
    	if(this.month < 1 || this.month > 12) return false;
    	
    	if(this.year < 1600 || this.year > 2400) return false;
    	
    	// minimum number of days is 1
    	// max number of days is 31
    	if(this.day < 1 || this.day > 31) return false;
    	
		if(this.month == 2){
			//if the month is February, check for a leap year
			if(isLeapYear()){
				if(this.day > 29) return false;
			}else{
				if(this.day > 28) return false;
			}
    	}
    	// if the month is April, June, September, or November, it can have a max of 30 days
    	if(this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11){
    		if(day > 30) return false;
    	}
    	return true;
    }
    // check if the year is a leap year
    public boolean isLeapYear(){
    	if( this.year % 4 == 0 && (this.year % 100 != 0 || this.year % 400 == 0)){
    		return true;
    	}else{
    		return false;
    	}
    }
    // get the day of the year
    public int getDayOfYear(){
    	// if the month is january,
    	// no special checking needed.
    	if(this.month == 1) return this.day;
    	// if the month is February
    	// just return 31 plus the number of days
    	if(this.month == 2) return this.day + 31;
    	int totalDays = 0;
    	// add 31 for every month up to the current month
    	for(int i = 1; i < this.month; i++){
    		totalDays += 31;
    	}
    	// check for months that only have 30 days in them
    	// and subtract 1 for each month that only has 30 days
    	for(int i = 1; i < this.month; i++){
    		if(i == 4 || i == 6 || i ==9 || i == 11) totalDays -= 1;
    	}
    	// check if leap year, and that the month of February is past
    	// if leap year, subtract 2 days, otherwise subtract 3 days
    	if(isLeapYear() && this.month > 2){
    		totalDays -= 2;
    	}else{
    		totalDays -= 3;
    	}
    	// at the current month, add the total number of 
    	// days into the month
    	totalDays += this.day;
    	// return days :D/>
    	return totalDays;
    }
    // get the day of the week
    public String getDayOfWeek(){
    	double totalDays = 0;
    	// get the total days since January 1, 1600
    	for(int i = 1600; i < this.year; i++){
    		totalDays += 365;
    		if( i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)){
    			totalDays += 1;
    		}
    	}
    	totalDays += getDayOfYear();
    	// get the day of the week out of the total days
    	int today = (int) totalDays  % 7;
    	String thisDay = "";
    	switch(today){
    		case 1:
    			thisDay = "Saturday";
    			break;
    		case 2:
    			thisDay = "Sunday";
    			break;
    		case 3:
    			thisDay = "Monday";
    			break;
    		case 4:
    			thisDay = "Tuesday";
    			break;
    		case 5:
    			thisDay = "Wednesday";
    			break;
    		case 6:
    			thisDay = "Thursday";
    			break;
    		case 0:
    			thisDay = "Friday";
    			break;
    		default:
    			thisDay = "";
    			break;
    	}
    	return thisDay;
    }
    // converts the object into a string
    public String toString(){
    	return this.month + "/" + this.day + "/" + this.year;
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1