• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

DateRec

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am new to this site I have a problem in my DateTest program and it is with the output I know it has to do with the constructors any help would be appreciated I am very new at this please be specfic


import java.util.*;

public class DateRec
{
private int month,day,year;
boolean good;
private static final int[] daysnMonth ={31,28,31,30,31,30,31,31,30,31,30,31};

DateRec ()
{
month = 1;
day = 1;
year = 2008;
}

DateRec (int m,int d,int y)
{
month = m;
day = d;
year = y;
good = validate();
}

public boolean validate ()
{
if (month < 0 && month < 13 && day > 0 && day <= daysnMonth[month] && year >1900 && year < 2009)
return good;
else
return false;
}

public String myToString()
{
String m = Integer.toString(month); //there must be a
String d = Integer.toString(day); //simpler format
String y = Integer.toString(year);

String s = m + "/" + d + "/" + y;
return s;
}

}




import java.util. *;

public class DateTest
{
public static void main(String[] args)
{
DateRec Datetest = new DateRec();
String s,m,d,y;
int month = 0 ,day = 0,year = 0 ;

Scanner keyboard = new Scanner(System.in);

char ch;
String ans = "y";

while(ans.charAt(0) == 'y' || ans.charAt(0) == 'Y')
{
System.out.print("Please enter the month: ");
m = keyboard.nextLine();

System.out.print("Please enter the day: ");
d = keyboard.nextLine();

System.out.print("Please enter the year: ");
y = keyboard.nextLine();

s = m + d + y;
for(int i = 0; i < s.length(); i++)
{
ch = s.charAt(i);
} // end for

System.out.println("Do you want to continue: Enter <Y>es or <N>o: ");
ans = keyboard.nextLine();
}

DateRec today = new DateRec();
s = today.myToString();
System.out.println(s);

DateRec anyDay = new DateRec(month,day,year);
s = anyDay.myToString();
System.out.println(s);

DateRec noDay = new DateRec(month,day,year);
s = noDay.myToString();
System.out.println("You entered an invalid date:" + s);
}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

What is the output you were expecting, and what is actually being shown?
 
Liz Lara
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are my instructions for my DateRec program
design a program with a main method to test the DateRec class
a) Create a local variable named today, and assign it to a new object of the type DateRec created using the current date for month, day, and year.
b) Create a local variable named anyDay, and assign it to a new object of the type DateRec created with no arguments.
c) Create a local variable named noDay, and assign it to a new object of the type DateRec created with invalid date values.
d) Call toString()method for each DateRec object and display the values returned
I am basically getting all zeros no values returned except for today returns default constructor from DateRec
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic