Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HelloWorld{
- public static void main(String[] args) {
- date d = new date(3, 6, 1973);
- d.print();
- date d1 = new date(3, 6, 1973);
- date d2 = new date(3, 24, 2015);
- date d3 = new date(1, 3, 2000);
- date d4 = new date(7, 4, 1973);
- date d5 = new date(3, 6, 1991);
- date current = new date();
- System.out.println(d1.ageAsOf(d2));
- System.out.println(d1.ageAsOf(d3));
- System.out.println(d1.ageAsOf(d4));
- System.out.println(d1.ageAsOf(d5));
- System.out.println(d1.compareTo(d2));
- System.out.println(d2.compareTo(d1));
- System.out.println(d1.compareTo(d1));
- date bday = new date(6, 23, 1998);
- Pet fifi = new Pet("Fifi", "cat", bday);
- }
- }
- -----------------------------------------------------------------
- import java.util.Calendar;
- public class date {
- //properties
- private int year;
- private int month;
- private int day;
- //constructors (part a)
- public date(int m, int d, int y){
- month = m;
- day = d;
- year = y;
- }
- public date(int m, int d){
- month = m;
- day = d;
- Calendar now = Calendar.getInstance();
- year = now.get(Calendar.YEAR);
- }
- public date(){
- Calendar now = Calendar.getInstance();
- year = now.get(Calendar.YEAR);
- month = now.get(Calendar.MONTH) + 1; //see note below
- day = now.get(Calendar.DATE);
- }
- //getters and setters (part b)
- public int getYear(){
- return year;
- }
- public int getMonth(){
- return month;
- }
- public int getDay(){
- return day;
- }
- public void setYear(int y){
- y = year;
- }
- public void setMonth(int m){
- m = month;
- }
- public void setDay(int d){
- d = day;
- }
- //other methods (parts c-e)
- public void print(){
- String[] mo = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- System.out.println(mo[month] + " " + day + ", " + year);
- }
- public int compareTo(date other){
- if(other.year > this.year){
- return -1;
- }
- if(other.year < this.year){
- return 1;
- }
- if(other.year == this.year){
- if(other.month > this.month){
- return -1;
- }
- if(other.month < this.month){
- return 1;
- }
- if(other.month == this.month){
- if(other.day > this.day){
- return -1;
- }
- if(other.month < this.month){
- return 1;
- }
- }
- }
- return 0;
- }
- public int ageAsOf(date d){
- if(d.month < this.month){
- return d.year-this.year-1;
- }
- if(d.month == this.month){
- if(d.day < this.day){
- return d.year-this.year-1;
- }
- if(d.day >= this.day){
- return d.year-this.year;
- }
- }
- if(d.month > this.month){
- return d.year - this.year;
- }
- return d.year - this.year;
- }
- public int ageAsOf(date current){
- if(current.month < this.month){
- return current.year-this.year-1;
- }
- if(current.month == this.month){
- if(current.day < this.day){
- return current.year-this.year-1;
- }
- if(current.day >= this.day){
- return current.year-this.year;
- }
- }
- if(current.month > this.month){
- return current.year - this.year;
- }
- return current.year - this.year;
- }
- }
- -------------------------------------------------------------------
- public class Pet {
- private String name;
- private String type;
- private date birthday; //see, I told you!
- public Pet(String n, String t, date bd){
- n = name;
- t = type;
- bd = birthday;
- }
- public Pet(String n, String t, int month, int day, int year){
- this(n, t, new date(month, day, year));
- }
- //getters and setters (part b)
- public String getName(){
- return name;
- }
- public String getType(){
- return type;
- }
- public date getBirthday(){
- return birthday;
- }
- public void setName(String n){
- name = n;
- }
- //other methods (parts c and d)
- public boolean isBirthday() {
- date current = new date();
- return birthday.getMonth() == current.getMonth() && birthday.getDay() == current.getDay();
- }
- public int getAge(){
- return birthday.ageAsOf(currentDate);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement