472,353 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Need help with date output program

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.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;
}
}



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(NameName DateClass2)
{
this(DateClass2.getname(),DateClass2.getday(),Date Class2.getyear());
}
public void setDateClass2(String 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.StringTokenizer;
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.println( s1.getmonth()+"/" +s1.getday()+"/" +s1.getyear() );
}


}

Anyone know where I am screwing the pooch here?
Dec 14 '06 #1
7 3785
Ganon11
3,652 Expert 2GB
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
So get rid of the entire Date.java after class NameName?
Dec 14 '06 #3
Ganon11
3,652 Expert 2GB
I think so - after all, your problem specification says "Create class Date" not "Create two classes Date and whatever"
Dec 14 '06 #4
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,int)
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 Expert 2GB
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.equalsIgnoreCase(otherString)).
Dec 14 '06 #6
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 Expert 2GB
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
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...
0
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. ...
13
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...
4
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...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import...
0
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...
0
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...
3
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...
1
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.