I can\'t seem to figure out how to properly program the program in order to answ
ID: 3634406 • Letter: I
Question
I can't seem to figure out how to properly program the program in order to answer the question in my exercise that is due tomorrow. I have tried many things, but ended up scraping them all. The question reads as follows:In Programming Exercise 2, the class Date was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class Date so that, in addition to the operations already defined, it can perform the following operations on a date:
a. Set the Month
b. Set the day
c. Set the year
d. Return the month
e. Return the day
f. Return the year
g. Test whether the year is a leap year
h. Return the number of days in the month. For example, if the date is 3-12/2005, the number of days to be returned is 31 because there are 31 days in March.
i. Return the number of days passed in the year. For example, if the date is 3-18-2005, the number of days passed in the year is 77. Note that the number of days returned also includes the current day.
j. Return the number of days remaining in the year. For example, if the date is 3-18-2005 the number of days remaining in the year is 288.
k. Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-18-2005 and the days to be added are 25, the new date is 4-12-2005.
I know it says that multiple sub-questions will not be answered, but I do not feel as though this goes against that rule. Also, the Exercise 2 that is talks about it this following program:
public class Date {
private int dMonth;
private int dDay;
private int dYear;
public Date() {
dMonth = 1;
dDay = 1 ;
dYear = 1900;
}
public Date(int month, int day, int year) {
dMonth = month;
dDay = day;
dYear = year;
}
public void setDate(int month, int day, int year) {
dMonth = month;
dDay = day;
dYear = year;
}
public int getMonth() {
return dMonth;
}
public int getDay() {
return dDay;
}
public int getYear() {
return dYear;
}
public String toString() {
return (dMonth + "-" + dDay + "-" + dYear);
}
}
Explanation / Answer
please rate - thanks
this is 1 question--expand on the class
import java.util.*;
public class datetester
{public static void main(String [] args)
{Date a=new Date(2,3,2011);
System.out.println("The date is "+ a.getMonth()+"/"+a.getDay()+"/"+a.getYear());
System.out.println("days in "+a.getMonth()+" "+a.getYear()+"="+a.daysInMonth());
System.out.println(a.getYear()+" is a leapyear: "+a.isleapYear());
System.out.println("it is day: "+a.convertedDayOfYear()+" of the year");
System.out.println("there are "+a.daysleft()+" left in the year");
a.newDay(5);
System.out.println("in 5 days it will be "+ a.getMonth()+"/"+a.getDay()+"/"+a.getYear());
}
}
----------------------------------------
public class Date{
private int dMonth;
private int dDay;
private int dYear; private String [] monthNames = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
private int [] monthDays = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public Date() {
dMonth = 1;
dDay = 1 ;
dYear = 1900;
}
public Date(int month, int day, int year) {
dMonth = month;
dDay = day;
dYear = year;
}
public void setDate(int month, int day, int year) {
dMonth = month;
dDay = day;
dYear = year;
}
public int getMonth(){
return dMonth;
}
public void setMonth(int theMonth){
if(theMonth > 0 && theMonth < 13)
dMonth = theMonth ;
else
dMonth= 1;
}
public int getDay(){
return dDay;
}
public int getYear(){
return dYear;
}
public void setDay(int theDay){
if(theDay > 0 && theDay <= monthDays[dMonth])
dDay=theDay;
if(dMonth == 2 && theDay == 29 && isleapYear())
dDay= theDay;
else
dDay= 1;
}
public void setYear(int theYear){
dYear = theYear;
}
public boolean isleapYear(){
if ((dYear % 400 == 0) || (dYear % 4 == 0 && dYear % 100 != 0))
return true;
else
return false;
}
public String toString() {
return (dMonth + "-" + dDay + "-" + dYear);
}
public int daysInMonth()
{
if(isleapYear()&&dMonth==2)
return monthDays[dMonth]+1;
else
return monthDays[dMonth];
}
public int convertedDayOfYear(){
int ddd = 0;
for(int i = 1; i < dMonth; i++){
if(isleapYear() && i == 2)
ddd += 29;
else
ddd += monthDays[i];
}
ddd += dDay;
return ddd;
}
public int daysleft(){
return 365-convertedDayOfYear();
}
public void newDay(int days)
{int numloop=days/30+3;
int j,doneflag=0,daysinmonth,daysinmonthminusday;
for(j=1;j<=numloop;j++)
{ if(doneflag==1)
return;
switch(dMonth)
{
case 9:
case 4:
case 6:
case 11:daysinmonth=30;
break;
case 2: daysinmonth=28;
if(dYear%4==0)
{if(dYear%100!=0)
daysinmonth=29;
else
if(dYear%400==0)
daysinmonth=29;
}
break;
default:daysinmonth=31;
}
daysinmonthminusday=daysinmonth-dDay;
if(days>daysinmonthminusday)
{dMonth=++dMonth%13; //more days left then in present month
if(dMonth==0)
{dYear++;
dMonth++;
}
days-=daysinmonthminusday;
dDay=0;
}
else
{dDay+=days; //done just set date
doneflag=1;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.