Textbook ObjectOriented Data Structures Using Java 3rd editi
Solution
28)
package com.cp.classes;
import java.util.Date;
public class IncDate extends Date{
public IncDate (int newMonth, int newDay, int newYear)
{
// Initializes this IncDate with the parameter values
super (newMonth ,newDay, newYear) ;
}
public void increment ()
{
// Increments this IncDate to represent the next day, i.e. ,
// this = (day after this)
// For example if this = 6/30/2003 then this becomes 7/1/2003
// Increment algorithm goes here
Date today = new Date(12,25,2001);
Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));
System.out.println(\"the tomorrow date is\"+tomorrow);
}
}
**********************************************************************************************************
package com.cp.classes;
import java.util.Date;
public class sample {
public static void main(String[] args) {
int temp;
Date date1 = new Date(10, 2, 1989);
Date date2 = new Date(4, 2, 1992);
IncDate date3 = new IncDate(12, 25, 2001);
// testing the statements are legar or illeagal
temp = date1.getDay();
System.out.println(\"a statement result is\" + temp);
temp = date3.getYear();
System.out.println(\"b statement result is\" + temp);
// date1.increment();//c statement causes compilation error,this
// statement is invalid
date3.increment();// d statement valid
date2 = date1;// valid
date2 = date3;// valid
// date3=date2;//invalid causes compilation error
}
}
output
a statement result is2
b statement result is19
the tomorrow date isSat Jul 26 00:00:00 IST 1919 //date3.increment output
**********************************************************************
40)
package com.cp.classes;
import java.util.Date;
public class sample {
public static void main(String[] args) {
IncDate date1=new IncDate(5,5,2000);
IncDate date2 = date1;
System.out.println(\"date1\"+date1);
System.out.println(\"date2\"+date2);
date1.increment();
System.out.println(\"the date2 is\"+date2);
}
}
package com.cp.classes;
import java.util.Date;
public class IncDate extends Date{
public IncDate (int newMonth, int newDay, int newYear)
{
// Initializes this IncDate with the parameter values
super (newMonth ,newDay, newYear) ;
}
public void increment ()
{
// Increments this IncDate to represent the next day, i.e. ,
// this = (day after this)
// For example if this = 6/30/2003 then this becomes 7/1/2003
// Increment algorithm goes here
Date today = new IncDate(5,5,2000);
Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));
System.out.println(\"the incremented date1 is\"+tomorrow);
}
}
output
date1Mon Nov 21 00:00:00 IST 1910
date2Mon Nov 21 00:00:00 IST 1910
the incremented date1 isTue Nov 22 00:00:00 IST 1910
the date2 isMon Nov 21 00:00:00 IST 1910
*********************************************************************************
42)
package com.cp.classes;
import java.util.Date;
public class sample {
public static void main(String[] args) {
IncDate date1=new IncDate(5,5,2000);
IncDate date2 = new IncDate(5,5,2000);
if(date1==date2)
System.out.println(\"the dates are equal\");
else
System.out.println(\"the dates are not equal\");
date1=date2;
if(date1==date2)
System.out.println(\"the dates are equal\");
else
System.out.println(\"the dates are not equal\");
Date date=date1.increment();
if(date==date2)
System.out.println(\"the dates are equal\");
else
System.out.println(\"not equal\");
}
}
package com.cp.classes;
import java.util.Date;
public class IncDate extends Date{
public IncDate (int newMonth, int newDay, int newYear)
{
// Initializes this IncDate with the parameter values
super (newMonth ,newDay, newYear) ;
}
public Date increment ()
{
// Increments this IncDate to represent the next day, i.e. ,
// this = (day after this)
// For example if this = 6/30/2003 then this becomes 7/1/2003
// Increment algorithm goes here
Date today = new IncDate(5,5,2000);
Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));
System.out.println(\"the incremented date1 is\"+tomorrow);
return tomorrow;
}
}
output
the dates are not equal
the dates are equal
the incremented date1 isTue Nov 22 00:00:00 IST 1910
not equal
***********************************************************************************************************
44)
package com.cp.classes;
import java.util.Date;
import java.util.Scanner;
public class sample {
public static void main(String[] args) {
Date[] datearray = new Date[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
int year = 2005;
int month = 12;
System.out.println(\"enter the day between 1 to 10\");
int day = scanner.nextInt();
Date date = new Date(day, month, year);
datearray[i] = date;
}
int count = 0;
for (Date date : datearray) {
System.out.println(\"the \" + count++ + \"is \" + date);
}
}
}
output
enter the day between 1 to 10
1
enter the day between 1 to 10
2
enter the day between 1 to 10
3
enter the day between 1 to 10
4
enter the day between 1 to 10
5
enter the day between 1 to 10
6
enter the day between 1 to 10
7
enter the day between 1 to 10
8
enter the day between 1 to 10
9
enter the day between 1 to 10
10
the 0is Fri Jun 28 00:00:00 IST 1907
the 1is Sat Jun 27 00:00:00 IST 1908
the 2is Sun Jun 27 00:00:00 IST 1909
the 3is Tue Jun 28 00:00:00 IST 1910
the 4is Wed Jun 28 00:00:00 IST 1911
the 5is Thu Jun 27 00:00:00 IST 1912
the 6is Fri Jun 27 00:00:00 IST 1913
the 7is Sun Jun 28 00:00:00 IST 1914
the 8is Mon Jun 28 00:00:00 IST 1915
the 9is Tue Jun 27 00:00:00 IST 1916




