![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 11
Rep Power: 0
![]() |
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()); } } |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
|
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
|
|
|
|
|
|
#4 |
|
Programmer
|
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|