java - Compare two objects, and return string. But one object doesn't take parameters? -
this homework.
goal: want compare date of 2 objects decide whether person object adult or not , store in string.
the strange thing is, values of date d1 0;
public class date { public int day, month, year; public string child date(date d1, date d2) { if ((d1.year - d2.year > 18) || ((d1.year - d2.year == 18) && (d2.year> d1.year)) || ((d1.year - d2.year == 18) && (d2.year == d1.maand) && (d2.day > d1.day))) { child = adult; } else { child = child; } date(int a, int b, int c) { = year; b = month; c = day; } date (string birthdate) { string pattern = "\\d{2}-\\d{2}-\\d{4}"; boolean b = birthdate.matches(pattern); if (b) { string[] str = birthdate.split("-"); (string s: str) this.day = integer.parseint(str[0]); this.month = integer.parseint(str[1]); this.year = integer.parseint(str[2]); this.child = false; } else { system.out.println("wrong format"); } }
when make test, happens:
system.out.println("d1 year = " + d1.year); system.out.println("d1 day = " + d1.day); system.out.println("d1 month = " + d1.month);
result: d1 year = 0 d1 day = 0 d1 month = 0
why happen? lets @ other class.
my other class, method infoperson located following:
public static person infoperson() { string name, lastname, birthdate; datum birthday, today; system.out.println("firstname:"); name = userinput(); system.out.println("lastname:"); lastname = userinput(); system.out.println("birthdate?:"); birthdate = userinput(); //here send string birthdate date class birthday = new date(birthdate); today = new date(3, 7, 2013); //here want compare 2 date objects, today , birthday. got stuck, how do correctly? datechild = new date(today, birthday); // here send new date person class consists of 2 strings , data birthday return new gast(name, lastname, datechild); }
the assignment in constructor reversed:
date(int a, int b, int c) { = year; // should year = a; b = month; // month = b; c = day; // day = c; }
please don't use class name same 1 defined in java api. date
class in java.util
package.
apart there many compiler errors in code:
public string child
- isn't going compile. shouldstring
notstring
.void compareto(date d1, date d2)
- don't know you're trying here. won't compile. undefined type -date
- you've declared
datum birthday
, initializing usingnew date(...)
. wouldn't work. - for reason, feel don't have method in class, bunch of constructors. suggestion - throw code away, , start afresh.
- and please don't use bunch of integer fields store birthdays. use
calendar
instance instead.
Comments
Post a Comment