codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  date checking  rsixtyone at 00:36 on Monday, October 24, 2005
 

hello all,

in my high school java class we are investigating into this date checking stuff. So far we've covered Objects, Classes, and Decisions.

Would anyone mind do a program DateChecking.java that reports whether a user-specified date is a valid date.

This is the Date class that must be used:
public class Date
{// Instance variables
private int dayOfMonth;
private int month; // 1 = Jan, 2 = Feb, etc.
private int year;

// Constructors
// Initializes date to 1 January 2000
public Date ()
{dayOfMonth = 1;
month = 1;
year = 2000;
}

// Constructor that initializes date to user's arguments
public Date (int d, int m, int y)
{dayOfMonth = d;
month = m;
year = y;
}

// Accessor methods
// Returns the day
public int getDay()
{return dayOfMonth;
}

// Returns the month
public int getMonth()
{return month;
}

// Returns the year
public int getYear()
{return year;
}

// Returns all fields of the date as a string
public String toString()
{return "Day " + dayOfMonth + " month " + month + " year " + year;
}

// Mutator methods

// Change the day
public void setDay (int d)
{dayOfMonth = d;
}

// Change the month
public void setMonth (int m)
{month = m;
}

// Change the year
public void setYear (int y)
{year = y;
}

// End of class Date
}

Some things that are needed are:
1. The date to be checked from the keyboard at run time. Need to use class mutators for this.

2. Documents in a comment and in written prompts to the user the order in which the user is to enter the fields of the date: month day year.

3. Must documents that user should enter the date fields in one line.

4. The output should be an echo of the date, along with a message stating whether it is valid or not. If it is invalid, have the message specify which fields are invalid.

5. Use what have been learned in Objects, Classes, and Decisions.

  Re: date checking  crwood at 18:37 on Tuesday, October 25, 2005
 


import java.io.*;

public class DateTest
{
public static void main(String[] args)
{
System.out.println("enter 'x' to exit");
boolean readMore = true;
BufferedReader br = null;
try
{
br = new BufferedReader(
new InputStreamReader(System.in));
while(readMore)
{
System.out.println("enter a date in the form: " +
DateStore.getFormat());
String in = "";
in = br.readLine();

if(in != null && in.toLowerCase().equals("x"))
break;
int[] data = parseInput(in);
if(!isValidParse(data))
break;
DateStore ds = new DateStore(data);
String error = ds.getErrors();
if(error.equals(""))
System.out.println("formatted date: " + ds.format());
else
System.out.println("\t**** errors ****\n" + error + "\n");
}
br.close();
}
catch(IOException ioe)
{
System.err.println("read error: " + ioe.getMessage());
readMore = false;
}
}

private static int[] parseInput(String s)
{
int[] out = new int[3];
String token = " ";
int start = 0, end, index = 0;
String next;
if(s.indexOf(token) != -1)
{
end = s.indexOf(token);
next = s.substring(start, end);
out[index++] = Integer.parseInt(next);
start = end + 1;
}
else
{
System.out.println("no space tokens in input");
return out;
}
if(s.lastIndexOf(token) != -1)
{
end = s.lastIndexOf(token);
next = s.substring(start, end);
out[index++] = Integer.parseInt(next);
start = end + 1;
next = s.substring(start);
out[index] = Integer.parseInt(next);
}
else
{
System.out.println("format requires three numbers " +
"separated by two spaces");
return out;
}
return out;
}

private static boolean isValidParse(int[] n)
{
for(int j = 0; j < n.length; j++)
if(n[j] == 0)
return false;
return true;
}
}

class DateStore
{
// Instance variables
int[] data;
private int dayOfMonth;
private int month; // 1 = Jan, 2 = Feb, etc.
private int year;
String errors;
final String[] months = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
final String[] fields = { "month", "day", "year" };
final String[] corrections = { "1 - 12", "1 - 31", "four digits" };

// Constructors
// Initializes date to 1 January 2000
public DateStore()
{
dayOfMonth = 1;
month = 1;
year = 2000;
data = new int[] { month, dayOfMonth, year };
validate();
}

// Constructor that initializes date to user's arguments
public DateStore(int[] data)
{
this.data = data;
month = data[0];
dayOfMonth = data[1];
year = data[2];
validate();
}

public String format()
{
return months[month-1] + " " + dayOfMonth + " " + year;
}

public String getErrors() { return errors; }

private void validate()
{
errors = "";
if(month < 1 || month > 12)
errors += getErrorMessage(0) + "\n";
if(dayOfMonth < 1 || dayOfMonth > 31)
errors += getErrorMessage(1) + "\n";
if(String.valueOf(year).length() != 4)
errors += getErrorMessage(2);
}

private String getErrorMessage(int index)
{
return "invalid entry for " +
fields[index] + " : " + data[index] +
" - valid values for " + fields[index] +
" = " + corrections[index];
}

public static String getFormat()
{
return "month day year" + "\t" +
"month[1 - 12], year[4 digits]";
}

// Accessor methods
// Returns the day
public int getDay() { return dayOfMonth; }

// Returns the month
public int getMonth() { return month; }

// Returns the year
public int getYear() { return year; }

// Returns all fields of the date as a string
public String toString()
{
return "Day:" + dayOfMonth + ", month:" + month + ", year:" + year;
}

// Mutator methods

// Change the day
public void setDay(int d)
{
dayOfMonth = d;
data[1] = d;
validate();
}

// Change the month
public void setMonth(int m)
{
month = m;
data[0] = m;
validate();
}

// Change the year
public void setYear(int y)
{
year = y;
data[2] = y;
validate();
}
// End of class Date
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help
•  Re: How to make an investment calculator


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005