473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can anyone help me with this program?

Need to write a program that does this:
Create class Date with the following capabilities:

a) Output the date in multiple formats, such as:

MM/DD/YYYY
June 14, 2005
DDD YYYY
b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year. [Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.]

I'm starting from scratch thus the new thread! :) And if anyone can tell me how to delete the other one or wants to delete it for me, feel free to do so!
Dec 15 '06 #1
4 5721
r035198x
13,262 8TB
Need to write a program that does this:
Create class Date with the following capabilities:

a) Output the date in multiple formats, such as:

MM/DD/YYYY
June 14, 2005
DDD YYYY
b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year. [Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.]

I'm starting from scratch thus the new thread! :) And if anyone can tell me how to delete the other one or wants to delete it for me, feel free to do so!
Write code for the class and the constructors and post what you think they should look like then we can help from there.
Dec 15 '06 #2
This is what I got so far.
Two java files. First one is

public class Date
{

private int month;
private int day;
private int year;
private static String[] months ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

public Date ()
{
this (0,0,0);
}
public Date (int m)
{
this (m, 0,0);
}
public Date(int m, int d, int y)
{
setDateClass( m ,d ,y );
}
public Date(Date DateClass)
{
this(DateClass.getmonth(), DateClass.getday(), DateClass.getyear());
}
public void setDateClass(int m, int d, int y)
{
setmonth(m);
setday(d);
setyear(y);
}
public void setmonth( int m)
{
month =((m >=0 && m <=12)? m : 0);
}
public void setday( int d)
{
day = ((d >=0 && d <=31)? d : 0);
}
public void setyear(int y)
{
year = ((y >= 0 && y <=2007)? y : 0);
}
public int getmonth()
{
return month;
}
public int getday()
{
return day;
}
public int getyear()
{
return year;
}
}



Second one is


import java.util.StringTokenizer;
import java.io.*;

public class DateClass
{
public static void main( String args[])
{

int month=12;
int day=14;
int year=2006;



Date s1= new Date(month, day, year);


System.out.println( s1.getmonth()+"/" +s1.getday()+"/" +s1.getyear() );
}


}
Dec 17 '06 #3
I am absolutely horrible at constructors and I am just absolutely guessing at what Im doing here. The string array I made for the months was on the advice of another member here, but I have no clue why its going in there at all etc etc.
For arguments sake, let's all assume that my total knowledge of Java is how to spell the word! :P
Dec 17 '06 #4
Ganon11
3,652 Expert 2GB
Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.
This is why I suggested using a static array of strings holding the months. Suppose you have a constructor with the following header:

Expand|Select|Wrap|Line Numbers
  1. public Date(String mon, int day, int year)
But your month is held by an integer, not a String, so you must determine what month the user sent you. You can do this by comparing the String argument mon to each element in the Date.months array. To compare two strings, use s1.equalsIgnoreCase(s2), where s1 and s2 are string variables (s1 would be the argument mon, s2 would be an element (0 through 11) of Date.months). Your hint tells you to use equals, but equalsIgnoreCase will be much better, since "february", "February", and "FEBRUARY" will all be treated as different by .equals(), but identical by .equalsIgnoreCase().

In order to check, declare an integer variable (call it num) that will hold the position of the month indicated by the user's string. Then, using a for loop (with your index going from 0 to 11), compare mon to each string in Date.months (If your index variable was i, you would compare mon to Date.months[i] inside the loop). If the strings are equal, set num equal to the index and break the loop. Outside of the loop, increment num by 1 (Suppose mon is "February". "February" is held at the 2nd position of your array, but that index is 1, so you must increment num so that your output will reflect the correct month).

Now that you have an integer value for your month, you can call another constructor, or you can use your setMonth(int) method.
Dec 17 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like...
10
by: Chris | last post by:
Could anyone write a small program to log the Signal-to-Noise figures for a Netgear DG834 router? I have been getting very variable SNR readings - and I would like to collect some evidence to...
13
by: Snis Pilbor | last post by:
Hello, Here is an idea I've been toying with to speed up programs but still keep them portable. It's just a very handwavey rough description right now since I haven't worked out details. The...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
3
by: shadachi | last post by:
Question 1 The mail-order company requires a program to calculate discounts that it is giving to all its customers during a sales campaign. Write a program that asks the user to enter the total...
29
by: shadachi | last post by:
This is the question?.. can anyone help me?? kinda confused..using JcreatorLe SAmple Output :
4
by: John | last post by:
Can anyone recommend a good PHP web site backup program? I would like it to be freeware and simple. I just need to backup one directory on a daily basis and need it to be zipped and...
19
by: William Gill | last post by:
I seem to be having a mentally bad period lately . My code is beginning to be terrible convoluted mess, and I shudder to think what it will be like to go back in a couple months and try to follow...
1
by: progman417 | last post by:
My Shareware program (or rather Commerical Program with a time limited demo) used to have sales of around $2,000 per month around 2002, but then I got busy at work and didn't have time to maintain...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.