473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with date output program

23 New Member
I need a java prog to do the following:
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.]


Here's what I got so far but i am at a dead stop now with no clue how to proceed further.

public class Date
{

private int month;
private int day;
private int year;


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.getda y(), DateClass.getye ar());
}
public void setDateClass(in t 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;
}
}



class NameName
{
private String name;
private int day;
private int year;

public NameName()
{
this ("",0,0);
}
public void NameName(String n, int d, int y)
{
setDateClass2(n , d, y);
}
public NameName(NameNa me DateClass2)
{
this(DateClass2 .getname(),Date Class2.getday() ,DateClass2.get year());
}
public void setDateClass2(S tring n)
{
setname(n);
setday(D);
setyear(Y);
}
public void setname( String n )
{
name=n;
}
public String getname()
{
return name;
}
public void setday(int D)
{
day = ((D >=0 && D <=31)? D : 0);
}
public int getday()
{
return day;
}
public void setyear(int Y)
{
year =((Y >=0 && Y <=2007)? Y : 0);
}
public int getyear()
{
return year;
}}



The second part of the prog is here:


import java.util.Strin gTokenizer;
import java.io.*;

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

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



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


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


}

Anyone know where I am screwing the pooch here?
Dec 14 '06 #1
7 3946
Ganon11
3,652 Recognized Expert Specialist
I think your NameName class is unnecessary. It looks like the assignment asks you to have a constructor that will take a string for the month "such as "February" instead of a number (such as 2). You would then compare the given string to a set of constant strings to see which month is represented by the given string. For instance, if the user passed "march" to the constructor, you would compare "march" to "January" (false), February (false), and "March" (true!). Knowing that the string represents March, you would set month equal to 3.

Next, I think the problem asks you to provide methods for output in the class itself - what you have now is output formatting in your main(). You should just be able to use a single function call, or something like this:

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         Date today = new Date("December", 14, 2006);
  4.         System.out.println(today.toString());
  5.     }
  6. }
Dec 14 '06 #2
erekose666
23 New Member
So get rid of the entire Date.java after class NameName?
Dec 14 '06 #3
Ganon11
3,652 Recognized Expert Specialist
I think so - after all, your problem specification says "Create class Date" not "Create two classes Date and whatever"
Dec 14 '06 #4
erekose666
23 New Member
OK I sorta agree with you there, but I have no idea how to change the stuff from ints to strings n whatnot.
Basically I know I eventually need 3 this:
this(int,int,in t)
this(string,int ,int)
this(int,int)

I have no idea how to get there from here though.
Dec 14 '06 #5
Ganon11
3,652 Recognized Expert Specialist
Well, as I said, you will need to compare the string given as an argument to the constructor and compare it to some constants.

Start by defining a static member of the Date class as follows:

Expand|Select|Wrap|Line Numbers
  1. private static String[] months; // {"January", "February", "March", etc etc "December" }
Then, inside your constructor, loop through this array (which is called through Date.months[index]) and compare the argument string to the values (you will have to use String.equalsIg noreCase(otherS tring)).
Dec 14 '06 #6
erekose666
23 New Member
Are you saying make one like this
private static String[] months; // {"January", "February", "March", etc etc "December" }

or one like THIS

private static String[] months ={"January", "February", "March", "April", "May", "June", "July", "August", "September" , "October", "November", "December" };
Dec 14 '06 #7
Ganon11
3,652 Recognized Expert Specialist
Whichever initializes the array with the values, I haven't done Java in a while, so I forgot how to define static variables. Probably your second one is the right one.
Dec 14 '06 #8

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

Similar topics

3
1645
by: Yannick Turgeon | last post by:
Hello all, One of our PHP program bugged recently. I've reduced the problem to this: April 4th! Anybody can tell me if I'm doing something wrong? Why April 4th is not printing but replaced by April 3rd? I'm using PHP 4.3.5 Yannick
0
2696
by: Norm Wong | last post by:
If anyone is interested in using db2uext2 with Cygwin gcc compiler on Windows, I've modified the IBM provided sample with the attached file. There are two main modifications. The mkdir command is the POSIX compliant version as opposed to the MicroSoft C compiler. The second parameter to mkdir is the file mode. In this case, I've made the directories created to be read/write/execute(list) for all users.
13
2458
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that month. We are assuming that the year will be valid 4 digit integer. So you don't have to think much about that(in terms of validation) except for the month of February. If the month is February, then you have to check whether that year is Leap...
4
2750
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself. the description is the following: the program will read a text file that contains historical price of a stock. The program will allow users to query...
2
2949
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import namespace="System.Web.UI.WebControls" %> <%@ import namespace="System.Data" %> <%@ import namespace="System.Data.OleDb" %>
0
3562
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this OleDbParameterCollection" whenI try to write a new record. Delete and Modify work fine its just the add record function causes the error.
0
7110
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
3
3658
by: Water Cooler v2 | last post by:
Sorry for asking this beginner question. I've written DTDs so far and read about XML Schemas. I understand that they are a replacement of the DTD fundamentally, and therefore allow for the validation of an XML document. My question really is: Why do we need XML Schemas other than for validation of an XML document? I am more interested in knowing if already available
1
3832
by: xtremebass | last post by:
Hello Bytes, i have a calender program which is created by using Javascript. when i execute that program using Internet Explorer,it works properly but when i tried in Mozilla firefox it didnt worked. Dates of particular year,month not displayed in that calender grids. i have collected that coding from online free resources. Here i have attached code for your reference. please do suggest me how do i display the calender date in grids in...
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.