14 Replies - 1204 Views - Last Post: 08 August 2012 - 11:12 PM Rate Topic: -----

#1 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Object Oriented Programming-creating new objects

Posted 08 August 2012 - 12:05 PM

I just started learning about object oriented programming. The program is supposed to ask the user for today's date and his birthday in (monday day year) format. It will than print the birthday (year/month/day), the day of the week the user was born, and how old he is in days. The assignment tells us what methods to implement in the data.java file as well as what the method should do.
I already wrote all the methods except I'm having a problem with public int advanceTo(Date endDay) which modifies the state of this Date object by advancing it to the given end Date and returns the number of days it took to advance this Date object to the given end Date. I'm guessing public boolean equals(Date d) is also incorrect as I think it should compare a complete date not just the day.
Anyways, my question comes from the Dataclient.java file (the 2nd pice of code here). I'm trying to create a new object from the input provided by the user but once it runs the "new Date" line, it runs an object with 0/0/0. Can someone explain to me what I did wrong or what I'm missing?

Thank you in advance!

Date.java
//This program tells you what day of the week you were born, and how old you are in days by
//collecting today's date and your birthday. It will also let you know if you were born on a leap year.


public class Date {
	private int year;
	private int month;
	private int day;
	
	public Date (int year, int month, int day) {
		this.year = getYear();
		this.month = getMonth();
		this.day = getDay();
	}
	
	public int getYear() {
		return year;
	}
	
	public int getMonth() {
		return month;
	}
	
	public int getDay() {
		return day;
	}
	
	public String toString() {
		return year+ "/" + month + "/" + day;
	}
	
	public boolean equals(Date d) {
		return d.equals(year + " " + month + " " + day);
	}
	
	public boolean isLeapYear() {
		if(year%4 == 0) {
			if(year%100 == 0 && !(year%400 == 0)) {
				return true;
			}else if(year%100 == 0) {
				return false;
			}
		}
		return true;
	}
	
	public int getDaysInMonth() {
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
			return 31;
		}else if(month==4||month==6||month==9||month==11) {
			return 30;
		}else if(month==2 && isLeapYear()) {
			return 29;
		}else {
			return 28;
		}
	}
	
	public void nextDay() {
		if(!equals(getDaysInMonth())) {
			if(month==12) {
				year += 1;
				month += 1;
				day += 1;
			}else {
				month += 1;
				day += 1;
			}
		}else {
			day += 1;
		}
	}
	
	public int advanceTo(Date endDay) {
		if (getDay() > endDay) {
			//fix this method to use today's date instead of day...
		}
	}
	
	public String getDayOfWeek() {
		int count = 0;
		for (int i=1753; i<=year; i++) {
			if(i%4==0 && i%100==0 && i%400==0) {
				count++;
			}
		}
		int dayChange = ((year - 1753) * 365 + count + month - 1 + day - 1)%7;
		if(dayChange==1) {
			return "Tuesday";
		}else if(dayChange==2) {
			return "Wednesday";
		}else if(dayChange==3) {
			return "Thursday";
		}else if(dayChange==4) {
			return "Friday";
		}else if(dayChange==5) {
			return "Saturday";
		}else if(dayChange==6) {
			return "Sunday";
		}else {
			return "Monday";
		}
	}
}



DateClient.java
import java.util.*;

public class DateClient {
	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);
		giveIntro();
		System.out.print("What is today's date (month day year)? ");
		int month = console.nextInt();
		System.out.print(month + " ");
		int day = console.nextInt();
		System.out.print(day + " ");
		int year = console.nextInt();
		System.out.print(year + " ");
		Date today = new Date(year, month, day); // object isn't built, prints 0/0/0
		System.out.println(today);
		
		System.out.print("What is your birthday (month day year)? ");
		month = console.nextInt();
		day = console.nextInt();
		year = console.nextInt();
		Date birthday = new Date(year, month, day);
		
		System.out.print(today);
		//System.out.println(", which was a " + getDayOfWeek());
		
		
	}
	
	public static void giveIntro() {
		System.out.println("This program tells you what day of the week you were born on,");
		System.out.println("how old you are in days, and if you were born on a leap year.");
		System.out.println();
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Object Oriented Programming-creating new objects

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 12:08 PM

this.year = getYear()


will simply assign the variable back to itself (doing nothing)

Instead in the constructor you want

this.year = year;


for all three
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 12:15 PM

Another thing to note here, dont call your classes names already used in API. In java.util package we have a class called Date and you have your Date here! And you have imported everything from util!
Was This Post Helpful? 1
  • +
  • -

#4 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 12:50 PM

View PostRyano121, on 08 August 2012 - 12:08 PM, said:

this.year = getYear()


will simply assign the variable back to itself (doing nothing)

Instead in the constructor you want

this.year = year;


for all three


OMG Thanks! Can't believe it was that simple... So, if I just use year while constructing the new date object, what would be the point of having a getYear() method?
public Date (int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	public int getYear() {
		return this.year;
	}


Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 01:05 PM

You will need getter methods to be able to access those variables in other classes - e.g

Date date = new Date(...);

System.out.println(date.getYear());

Was This Post Helpful? 1
  • +
  • -

#6 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 01:18 PM

Ok that makes sense. Completely forgot about that.
I'm also encountering some issues trying to print the last line in DataClient.java
I want to print how many days old the user is.
import java.util.*;

public class DateClient {
	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);
		giveIntro();
		System.out.print("What is today's date (month day year)? ");
		int month = console.nextInt();
		int day = console.nextInt();
		int year = console.nextInt();
		Date today = new Date(year, month, day);
		
		System.out.print("What is your birthday (month day year)? ");
		month = console.nextInt();
		day = console.nextInt();
		year = console.nextInt();
		Date birthday = new Date(year, month, day);
		System.out.println(birthday);
		
		System.out.print("You were born on " + birthday);
		System.out.println(", which was a " + today.getDayOfWeek() + ".");
		System.out.println("You are " +birthday.advanceTo(today) + " days old.");
}


but the last line doesn't print... I'm guessing I'm having an issue with "birthday.advanceTo(today)"
my code for advance to is:
public int advanceTo(Date endDay) {
		int days = 0;
		Date startDay = new Date(1753, 1, 1);
		while(!startDay.equals(endDay)) {
			startDay.nextDay();
			days++;
		}
		return days;
	}



Any ideas?

View Postsmohd, on 08 August 2012 - 12:15 PM, said:

Another thing to note here, dont call your classes names already used in API. In java.util package we have a class called Date and you have your Date here! And you have imported everything from util!


Good to know. I'll definitely make sure to use different names when creating classes. Thanks for the info! :)
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 01:29 PM

Look at your equals method, it wont be true and so causing infinite loop:
public boolean equals(Date d) {
		return d.equals(year + " " + month + " " + day);
	}

Why dont you check if year, month and day are equal instead of that string?
Was This Post Helpful? 1
  • +
  • -

#8 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 01:44 PM

Yes, I noticed that too and changed it:
	public boolean equals(Date d) {
		return (this.year == d.year && this.month==d.month && this.day==d.day);
	}


Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 01:54 PM

Yap thats it? so is it working now?
Was This Post Helpful? 0
  • +
  • -

#10 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 02:23 PM

View Postsmohd, on 08 August 2012 - 01:54 PM, said:

Yap thats it? so is it working now?


No, when I try to run the last line to print how old the user is in days, birthday.advanceTo(today) it doesn't print anything...
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 02:40 PM

Please post your updated code.
Also your advanceTo wont give you the correct results because you are doing it using:
Date startDay = new Date(1753, 1, 1);

You should be using the instance calling it. "this"
Was This Post Helpful? 0
  • +
  • -

#12 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 03:19 PM

View Postsmohd, on 08 August 2012 - 02:40 PM, said:

Please post your updated code.
Also your advanceTo wont give you the correct results because you are doing it using:
Date startDay = new Date(1753, 1, 1);

You should be using the instance calling it. "this"


I'm kind of confused on how to do that. I tried switching to this.year, this.month, this.day but that doesn't work either. Here is my updated code:
Date.java
//A Date object represents the year, month, and day of a date.


public class Date {
	private int year;
	private int month;
	private int day;
	
	//constructs a new Date object
	public Date (int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	//returns the Date object's year
	public int getYear() {
		return this.year;
	}
	
	//returns the Date object's month of the year (1-12)
	public int getMonth() {
		return this.month;
	}
	
	//returns the Date object's day of the month (1-end of month)
	public int getDay() {
		return this.day;
	}
	
	//returns a String representation of this Date in a (year/month/day) format
	public String toString() {
		return this.year+ "/" + this.month + "/" + this.day;
	}
	
	//returns whether this Date object represents the same date as the given Date parameter
	public boolean equals(Date d) {
		return (this.year == d.year && this.month==d.month && this.day==d.day);
	}
	
	//returns whether this Date's year is a leap year
	public boolean isLeapYear() {
		if(this.year%4 == 0) {
			if(this.year%100 == 0 && !(this.year%400 == 0)) {
				return true;
			}else if(this.year%100 == 0) {
				return false;
			}
		}
		return true;
	}
	
	//provides the number of days in a given month
	public int getDaysInMonth() {
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
			return 31;
		}else if(month==4||month==6||month==9||month==11) {
			return 30;
		}else if(month==2 && isLeapYear()) {
			return 29;
		}else {
			return 28;
		}
	}
	
	//modifies the state of this Date object by advancing it one day 
	public void nextDay() {
		if(equals(getDaysInMonth())) {//if the last day of the month
			if(this.month==12) {//if the last day of the last month, 
				this.year += 1;
				this.month = 1;
				this.day = 1;
			}else {//if the last day of a month
				this.month += 1;
				this.day = 1;
			}
		}else {//any other day
			this.day += 1;
		}
	}
	
	//modifies the state of this Date object by advancing it to the given end Date
	public int advanceTo(Date endDay) {
		int count = 0;
		Date startDay = new Date(this.year, this.month, this.day);
		while(!startDay.equals(endDay)) {
			startDay.nextDay();
			count++;
		}
		return count;
	}
	
	//returns a String representing what day of the week this Date falls on
	public String getDayOfWeek() {
		int count = 0;
		for (int i=1753; i<=year; i++) {
			if(i%4==0 && i%100==0 && i%400==0) {
				count++;
			}
		}
		int dayChange = ((year - 1753) * 365 + count + month - 1 + day - 1)%7;
		if(dayChange==1) {
			return "Tuesday";
		}else if(dayChange==2) {
			return "Wednesday";
		}else if(dayChange==3) {
			return "Thursday";
		}else if(dayChange==4) {
			return "Friday";
		}else if(dayChange==5) {
			return "Saturday";
		}else if(dayChange==6) {
			return "Sunday";
		}else {
			return "Monday";
		}
	}
}


DateClient.java
import java.util.*;

public class DateClient {
	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);
		giveIntro();
		System.out.print("What is today's date (month day year)? ");
		int month = console.nextInt();
		int day = console.nextInt();
		int year = console.nextInt();
		Date today = new Date(year, month, day);
		
		System.out.print("What is your birthday (month day year)? ");
		month = console.nextInt();
		day = console.nextInt();
		year = console.nextInt();
		Date birthday = new Date(year, month, day);
		System.out.println(birthday);
		
		System.out.print("You were born on " + birthday);
		System.out.println(", which was a " + today.getDayOfWeek() + ".");
		System.out.println("You are " + birthday.advanceTo(today) + " days old.");//how to call this method??
		
		
	}
	
	public static void giveIntro() {
		System.out.println("This program tells you what day of the week you were born on,");
		System.out.println("how old you are in days, and if you were born on a leap year.");
		System.out.println();
	}
}


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 04:50 PM

	
	//returns whether this Date's year is a leap year
	public boolean isLeapYear() {
		if(this.year%4 == 0) {
			if(this.year%100 == 0 && !(this.year%400 == 0)) {
				return true;
			}else if(this.year%100 == 0) {
				return false;
			}
		}
		return true;   // <--- that should be false
	}
	


when you have a boolean method always easier to exit as soon as a condition is not encountered
	
	//returns whether this Date's year is a leap year
	public boolean isLeapYear() {
		if(year%4 != 0) 
                   return false;

            .....
	}
	


Was This Post Helpful? 0
  • +
  • -

#14 sarav1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-August 12

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 04:59 PM

View Postpbl, on 08 August 2012 - 04:50 PM, said:

	
	//returns whether this Date's year is a leap year
	public boolean isLeapYear() {
		if(this.year%4 == 0) {
			if(this.year%100 == 0 && !(this.year%400 == 0)) {
				return true;
			}else if(this.year%100 == 0) {
				return false;
			}
		}
		return true;   // <--- that should be false
	}
	


when you have a boolean method always easier to exit as soon as a condition is not encountered
	
	//returns whether this Date's year is a leap year
	public boolean isLeapYear() {
		if(year%4 != 0) 
                   return false;

            .....
	}
	


Nice catch! Thanks for pointing it out.
I'm having a hard time trying to build a new object in the advanceTo() method using the birthday as the date. Could someone point me in the right direction? I've been experimenting with various ways to construct the object within the method but nothing seems to be working...
Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object Oriented Programming-creating new objects

Posted 08 August 2012 - 11:12 PM

You dont need to, because by saying:
birthday.advanceTo(today)
you have everything you need about birthday and everything you need about today!
To access birthday instances here in advanceTo() you just name them like year
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1