Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 20th, 2005, 6:05 AM   #1
secrecy230
Newbie
 
Join Date: May 2005
Posts: 11
Rep Power: 0 secrecy230 is on a distinguished road
break 8 digit

i Have to create a program which will enter 8 digit and output it in the form of a date in the form of dd/mm/yyyy i don't have to validate the days with repect to month or leap year.
am getting mix up with the mutators and accessors
can you complete the prgram for me plz i don't know how to set the date
so as when i break it into days,month and year it can be used in every method
i have start the program but i can't finish it the prgram is below>>

public class Break
{
private int day, month, year;
private int date;



public void setDate(int indate)
{
date=indate;
}

public void setDay()
{

day = (date/1000000);

}
public void setMonth()
{

month = (date/10000)%100;

}
public void setYear()
{

year= (date%1000);

}

private boolean isValidDate()
{
boolean validDate = false;
if (( 2000 <= year ) && ( year <= 3000 ))
if (( 1 <= month ) && ( month <= 12 ))
if (( 1 <= 1) && ( day <= 31 ))
validDate = true;

return validDate;
}


public int getDate()
{
return date;
}





//ACCESSORS
public int getDay()
{

return day;
}
public int getMonth()
{
return month;
}



public int getYear()
{
return year;
}

public String toString()
{
String str= day + "/" + month + "/" + getYear();
return str;
}//end toString()

//MUTATORS



public static void main(String args [])
{
Break we = new Break();


System.out.println(we.toString());
}


}
secrecy230 is offline   Reply With Quote
Old Jun 20th, 2005, 8:51 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
1. plaes put it in code tags

also we.toString() will output a string representation of the Break object, you will need to write an accessor to print out the result from the break class, also you need to set the break class's variables. at the minute they will all be null.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 20th, 2005, 9:18 AM   #3
HastaSiempre
Newbie
 
HastaSiempre's Avatar
 
Join Date: Jun 2005
Location: Istanbul University
Posts: 2
Rep Power: 0 HastaSiempre is on a distinguished road
Send a message via MSN to HastaSiempre
import java.io.*;
public class Break
{
private int day, month, year;
private int date;



public void setDate(int indate)
{
date=indate;
}

public void setDay()
{

day = (date/1000000);

}
public void setMonth()
{

month = (date/10000)%100;

}
public void setYear()
{

year= (date%10000);

}

private boolean isValidDate()
{
boolean validDate = false;
if (( 2000 <= year ) && ( year <= 3000 ))
if (( 1 <= month ) && ( month <= 12 ))
if (( 1 <= 1) && ( day <= 31 ))
validDate = true;

return validDate;
}


public int getDate()
{
return date;
}





//ACCESSORS
public int getDay()
{

return day;
}
public int getMonth()
{
return month;
}



public int getYear()
{
return year;
}

public String toString()
{
String str= day + "/" + month + "/" + getYear();
return str;
}//end toString()

//MUTATORS



public static void main(String args []) throws Exception
{
Break we = new Break();

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try{System.out.println("enter 8 digit");
String digits = in.readLine();
we.setDate(Integer.parseInt(digits)); }
catch(NumberFormatException e) {System.out.println("u have to enter 8 digits");}
we.setMonth();
we.setYear();
we.setDay();

if(we.isValidDate())
System.out.println(we.toString());
else System.out.println("ur entery is not a valid date");
}


}
__________________
Religions come and go,but beer and wine always remains with us
HastaSiempre is offline   Reply With Quote
Old Jun 20th, 2005, 10:37 AM   #4
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Hmm... From the looks of things your making this a lot more complicated than it needs to be. For starters, having seperate setDay(), setMonth(), and setYear() methods is unnecessary. Your program should do all those things automatically from the beggining, or do them all when a request is made for the converted date. Either way they should all be done at the same time. Secondly, you'd have a much easier time if you reead your "date" input in as a string instead of an int, pulled out the appropriate characters, then converted them back into Integers. (It can be done with integer input, but it's much harder than you'd think.) Lastly, Berto is right, your variables all look to be null. Make sure that when your writing your programs that all of your variables have a value (whether default or entered) at all times. You'll run into NullPointers much less often, which will make your programming life much easier. Speaking of which, how do you plan to take in input? I see that HastaSiempre went ahead and decided that for you, but what were you originally planning? Also, instead of having am actual Break object, I'd just give Break a static method that take the date in and spit the converted result right back out. Here's how I might have done it. What I worte supports both the new object way of doing it and the static converter method way. Feel free to change anything to suit your needs, but make sure you understand how everything works. If there's something you don't understand, just ask..

public class Break {
  private String date;
  private int day, month, year;

  public Break(String date) {

    this.date = date;

    if (date.length() != 8) {
      day = 0;
      month = 0;
      year = 0;
    } else {
      day = Integer.parseInt(date.substring(0, 2));
      month = Integer.parseInt(date.substring(2, 4));
      year = Integer.parseInt(date.substring(4));
    }
  }

  public static String convertDate(String date) {
    Break temp = new Break(date);
    return temp.toString();
  }

  private boolean isValidDate() {
    boolean validDate = false;
    if ((2000 <= year) && (year <= 3000))
      if ((1 <= month) && (month <= 12))
        if ((1 <= day) && (day <= 31))
          validDate = true;

    return validDate;
  }

  public String getDate() {
    return date;
  }

  public int getDay() {
    return day;
  }

  public int getMonth() {
    return month;
  }

  public int getYear() {
    return year;
  }

  public String toString() {
    String str = "";
    
    if (isValidDate()) 
      str = getDay() + "/" + getMonth() + "/" + getYear();
    else
      str = "Invalid date";
    
    return str;
  }

  public static void main(String args[]) {
    
    Break we = new Break("20062005");
    System.out.println(we.toString());
    
    //or
    
    System.out.println(Break.convertDate("20062005"));
    
  }

}
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC