[Show all top banners]

Slackdemic
Replies to this thread:

More by Slackdemic
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java Programming Help!
[VIEWED 1598 TIMES]
SAVE! for ease of future access.
Posted on 09-22-06 4:59 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hi all Java guys, I need to get this done. I have completed Phase - I, now I am working on Phase -II. How do you all think I should proceed to get the output for phase-II?

DATE CLASS DESCRIPTION
The object will include three properties, month, day and year as instance variables. Include two constructors in your class. A default constructor which initializes all instance variables to zero and a constructor which initializes the instance variables with parameter values passed to the constructor. On this assignment you may assume the values provided are correct…you do not have to provide error detection.
Provide the following methods.
􀂃set and get methods for each of the instance variables.
􀂃a method to add a specified number of days based on a parameter value passed to the method.
􀂃a method to add a specified number of months based on a parameter value passed to the method.
􀂃a method to add a specified number of years based on a parameter value passed to the method.
􀂃a toString () that will print the date in the following format mm/dd/yyyy

TESTDATE CLASS DESCRIPTION
Create a TestDate class which will test your Date class by doing the following.
􀂃Create a default Date Object
􀂃Create a Date Object with a valid day, month, year (use your birthday)
􀂃Print the Date Objects before any modifications to the objects have been made
􀂃Set the day, month and year on the default Date Object using set methods
􀂃Add days to the birthdate Date Object to test the addDay method
􀂃Add months to the birthdate Date Object to test the addMonth method
􀂃Add years to the birthdate Date Object to test the addYear method
􀂃Print the Date Objects after modifications
􀂃Print the days, month, and year of a Date Object with the get methods.
PHASES
This program will be done in three phases:
Phase I: Date class will calculate not be concerned with valid dates after adding days and months.
Phase II: Date class will calculate valid dates after adding days and months but will not be concerned with leap year.
Phase III: Date class will calculate valid dates for each year.
OBJECTIVES
􀂃Demonstrate ability to declare a class and use it to create an object
􀂃Demonstrate ability to declare methods in a class to implement the class behaviors
􀂃Demonstrate ability to declare instance variables to implement class attributes
􀂃Demonstrate ability to use a constructor to ensure an object’s data is initialized when the object is created
􀂃Demonstrate ability to use problem-solving techniques
􀂃Demonstrate ability to use if and if…else selection statements.
􀂃Demonstrate ability to use while statements
SAMPLE PROGRAM EXECUTIONS
􀂃Assume the following Test Data for Phase I and II
o Birthday is 9/30/1986
o Default date was set to 6/26/1960
o One day is added to birthday Date Object
o Six months is added to birthday Date Object
o 10 years is added to birthday Date Object

Here's are the outputs of Phase - I and Phase - II

 
Posted on 09-22-06 5:03 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

This is what I did for Phase-I.

For Date Class:

public class Date
{
private int month; //value of month as an integer
private int day; //value of day as an integer
private int year; //value of year as an integer

public Date ( int dateMonth, int dateDay, int dateYear ) //the constructor
{ //with the parameter

month = dateMonth;
day = dateDay;
year = dateYear;

} //end the constructor with the parameter

public Date() // default constructor
{
month = 0;
day = 0;
year = 0;
}

public void setMonth( int dateMonth ) //method setMonth
{
month = dateMonth;
}

public void setDay (int dateDay ) //method setDay
{
day = dateDay;
}

public void setYear( int dateYear ) //method setYear
{
year = dateYear;
}
public int getMonth() //method getMonth
{
return month;
}
public int getDay() //method getDay
{
return day;
}
public int getYear() //method getYear
{
return year;
}

public void addMonth(int m)
{
month = m + 6; // add 6 to the month and assign it for month

}
public void addDay(int d)
{
day = d + 1; //add 1 to the day and assign it for day
}
public void addYear(int y)
{
year = y + 10; //add 10 to the year and assign to the year
}
public String toString()
{
return month + "/"+ day + "/" + year; //return the value of
} //concatenated month, day, and year

}

For TestDate class:

public class TestDate
{
public static void main(String args[])
{
Date date1 = new Date(); //construct a date1 object
Date date2 = new Date(07,07,1980); //construct a date2 object of date of birth

System.out.println(date1.toString()); //print date1, toString called
System.out.println(date2.toString()); //print date2, toString called

date1.setMonth(6); //set the default month 6
date1.setDay(26); //set the default day 26
date1.setYear(1960); //set the default year 1960

System.out.println(date1.toString()); //print the default date1

date2.addMonth(7); //addMonth = month of birth(7) + 6
System.out.println(date2.toString()); //print


date2.addDay(7); //addDay = day of birth (7) + 1
System.out.println(date2.toString()); //print


date2.addYear(1980); //addYear = year of birth (1980) + 10
System.out.println(date2.toString()); //print


}
}
 
Posted on 09-22-06 5:27 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I added this if block.

if
( dateMonth > 12 )
dateYear = dateYear + 1;
month = dateMonth;


}

public void setDay (int dateDay ) //method setDay
{

if
( dateDay > 30 )
dateDay = dateDay + 1;
day = dateDay;

Now it even doesn't want to compile! :S
 
Posted on 09-22-06 5:34 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hey, you are trying to get your homework done here. Though I am not going to write codes for you I will point our few mistakes in Phase I.

All three "add" methods are wrong. The example in question is just for one instance. addition is not supposed to be hardcoded like 1,6, and 10.

futher, there are more things u need to take care of. Like, when u add days ... if it goes over the last date of month, you shall increase month.
If month goes beyon december, year should be advanced.

Correct this aspect first.

SPECIAL NOTE: This is why it is not a good idea to provide SETTERS for all data members of a class. This approach is prone to DANGEROUS SIDE EFFECTS, resulting into inconsistent state of object.
 
Posted on 09-22-06 5:36 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I think we were wrting at the same time. There are still few errors in your code while setting the values.

After increasing year or month, respective month and day should start from 1.
 
Posted on 09-22-06 5:37 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

And it is not always 30. May be 31 or may be 29. Take care of leap year too.
 
Posted on 09-22-06 5:52 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Well, for phase-II, I do not need to consider leap year. But, yes, I need to consider both 30 and 31 days for months.
 
Posted on 09-22-06 5:56 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

The phase-I works VERY FINE with what I coded. You didn't read the whole question.
 
Posted on 09-22-06 9:26 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

It is very close to getting to work, but I am still unable to get the right output...Anyone can help me please?

Here are the codes:
Date class:

/*------------------------------------------------------------------------------------
Name: Mahesh Panta
File Name: Date.java
Course Section: 1337.001
Purpose: To write a class for a Date Object
--------------------------------------------------------------------------------------*/

public class Date
{
private int month; //value of month as an integer
private int day; //value of day as an integer
private int year; //value of year as an integer

public Date ( int dateMonth, int dateDay, int dateYear ) //the constructor
{ //with the parameter

month = dateMonth;
day = dateDay;
year = dateYear;

} //end the constructor with the parameter

public Date() // default constructor
{
month = 0;
day = 0;
year = 0;
}

public void setMonth( int dateMonth ) //method setMonth
{
month = dateMonth;
}

public void setDay (int dateDay ) //method setDay
{
day = dateDay;
}

public void setYear( int dateYear ) //method setYear
{
year = dateYear;
}
public int getMonth() //method getMonth
{
return month;
}
public int getDay() //method getDay
{
return day;
}
public int getYear() //method getYear
{
return year;
}

public void addMonth(int m)
{

month = month + m;
if (month>12)
{
month-=12;
addYear(1);
}


}
public void addDay(int d)
{
day = d + 1; //add 1 to the day and assign it for day
if (day>28)
{
if (month==2)
{
day-=28;
addMonth(1);
}
else
if ((month==4||month==6||month==9||month==11)&&day>30)
{
day-=30;
addMonth(1);
}
else
if (day>31)
{
day-=31;
addMonth(1);
}

} //add 1 to the day and assign it for day
}
public void addYear(int y)
{
//year = y + 10;
//year = year + y;
year+=y;
}
/*public String toString()
{
return month + "/"+ day + "/" + year; //return the value of
}*/
//concatenated month, day, and year
public String toString()
{
String d=""+day,m=""+month,y=""+year;
if (d.length()<2)
d="0"+d;
if (m.length()<2)
m="0"+m;
while (y.length()<4)
y="0"+y;
return m + "/"+ d + "/" + y; //return the value of
} //concatenated month, day, and year
}


TestDate:

/*----------------------------------------------------------------
Name: Mahesh Panta
File Name: Date.java
Course Section: 1337.01
Purpose: To write a Class TestDate that will test the Date Class
???Assume the following Test Data for Phase I and II
o Birthday is 9/30/1986
o Default date was set to 6/26/1960
o One day is added to birthday Date Object
o Six months is added to birthday Date Object
o 10 years is added to birthday Date Object

-----------------------------------------------------------------*/

public class TestDate
{
public static void main(String args[])
{
Date date1 = new Date(); //construct a date1 object
Date date2 = new Date(9,30,1986); //construct a date2 object of date of birth

System.out.println(date1.toString()); //print date1, toString called
System.out.println(date2.toString()); //print date2, toString called


date1.setMonth(6); //set the default month 6
date1.setDay(26); //set the default day 26
date1.setYear(1960); //set the default year 1960

System.out.println(date1.toString()); //print the default date1

date2.addMonth(7); //addMonth = month of birth(7) + 6
System.out.println(date2.toString()); //print


date2.addDay(7); //addDay = day of birth (7) + 1
System.out.println(date2.toString()); //print


date2.addYear(1980); //addYear = year of birth (1980) + 10
System.out.println(date2.toString()); //print

}

}

/*date1.setMonth(6); //set the default month 6
date1.setDay(26); //set the default day 26
date1.setYear(1960); //set the default year 1960

System.out.println(date1.toString()); //print the default date1

date2.addDay(1);
System.out.println(date2.toString()); //print

//date2.addYear(1);
date2.addDay(7); //addDay = day of birth (7) + 1
System.out.println(date2.toString()); *///print
 
Posted on 09-22-06 11:54 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Done, finally! And thanks those all who looked this thread (but didn't post anything) :P
 
Posted on 09-23-06 12:23 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

1967 ma 1980 jodeko? :)
 
Posted on 09-23-06 12:26 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

1986 ma 1980 bhaneko ta ke ke bhayecha... hmmm...
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 90 days
Recommended Popular Threads Controvertial Threads
डीभी परेन भने खुसि हुनु होस् ! अमेरिकामाधेरै का श्रीमती अर्कैसँग पोइला गएका छन् !
शीर्षक जे पनि हुन सक्छ।
What are your first memories of when Nepal Television Began?
Sajha Poll: नेपालका सबैभन्दा आकर्षक महिला को हुन्?
NRN card pros and cons?
Basnet or Basnyat ??
निगुरो थाहा छ ??
Nas and The Bokas: Coming to a Night Club near you
अमेरिकामा छोरा हराएको सूचना
TPS Re-registration case still pending ..
nrn citizenship
Breathe in. Breathe out.
ढ्याउ गर्दा दसैँको खसी गनाउच
Sajha has turned into MAGATs nest
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
Doctors dying suddenly or unexpectedly since the rollout of COVID-19 vaccines
Send Parcels from USA to Nepal & Worldwide.
TPS Work Permit/How long your took?
Why is every youths leaving Nepal? Why are not youths entering politics and serving the country, starting business etc?
Travelling to Nepal - TPS AP- PASSPORT
Nas and The Bokas: Coming to a Night Club near you
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters