directions: create new class couple composed of 2 person objects. constructor,__init__(self,person1, person2) should take 2 person objects arguments. should set instance attributes self.person1 & self.person2 values passed in constructor.
for java, rather overloading + operator, define method add(person p) part of person class, following called:
person person1 = new person(…); person person2 = new person(…); person1.add(person2); keep getting cannot find add(person)
i'm not sure i'm going wrong here.
this have far: zeller
public class zeller { public int a_month; public int b_day; public int c_year; private int d_century; public zeller() { this.a_month= 0; this.b_day= 0; this.c_year= 0; this.d_century= 0; } public zeller(int a_month, int b_day, int c_year) { if(a_month == 1 || a_month == 2){ this.a_month = a_month + 10; } else{ this.a_month = a_month -2; } this.b_day = b_day; if(geta_month()== 11 || geta_month() == 12){ this.c_year = (c_year-1)%100; } else{ // modified***** this.c_year=c_year; } this.d_century = c_year/100; } public int geta_month() { return a_month; } public int getb_day() { return b_day; } public int getc_year() { //rmodified code spits year in format such "2005" return c_year; } public int getd_century(){ return c_year/100; } public void seta_month(int a) { int tempa; if(a == 1 || == 2){ tempa = (a+10); } else{ tempa = (a -2); } this.a_month = tempa; } public void setb_day(int b) { this.b_day = b; } public void setc_year(int c) { int tempc = 0; if(geta_month()== 11 || geta_month() == 12){ tempc = (c-1); } this.c_year = tempc%100; } public void finddayofweek(){ int d = this.d_century; int x = this.c_year/4; int w = (13*this.a_month -1)/5; int y = this.d_century/4; int z = (w+x+y+this.b_day+this.c_year - 2 *d); int r = z%7; if(r <0){ int tempr; tempr = r + 7; r = tempr; } if (r == 0){ system.out.print("the day of week sunday"); } else if(r== 1){ system.out.print("the day of week monday"); } else if(r== 2){ system.out.print("the day of week tuesday"); } else if(r== 3){ system.out.print("the day of week wednesday"); } else if(r== 4){ system.out.print("the day of week thursday"); } else if(r== 5){ system.out.print("the day of week friday"); } else if(r == 6){ system.out.print("the day of week saturday"); } } } class person extends zeller { private string countryofbirth; private string name; /** * remember initialize instance attributes in constructor. instance attributes in case * self.birthday, self.country, self.name (this in java) */ public person() { this.countryofbirth="unknown"; this.name=" john smith "; } public person(string name, int a_month, int b_day, int c_year, string countryofbirth) { super ( a_month, b_day, c_year); this.countryofbirth=countryofbirth; this.name=name; } /** * ************add function getidcardstring(self) person class returns string concatenation of self. name , self.birthday instance attributes. used on id cards people students, faculty, staff , alumni). */ public string getidcardstring() { return "id card: " + this.name + " birthdate " + geta_month()+"/"+ getb_day()+"/"+ getc_year(); } /** * name */ public void setname(string name) { this.name=name; } public string getname() { return this.name; } /** * instance method bornonday(self) takes no parameters calls zeller(a,b,c,d) * method created in hw1. can include zeller method in person class or import it. * bornonday(self) should return string returned zeller. */public string bornonday() { return geta_month()+"/"+getb_day()+"/"+getc_year(); } /** * instance method isoldenoughtodrink(country) takes string parameter country. return true if person eligible drink in country passed method, false otherwise. in example please pass method value class attribute validcountries ensure not use invalid values (for example “us” instead of “usa”) . */public boolean isoldenoughtodrink(string countryofbirth) { calendar cal=calendar.getinstance(timezone.getdefault()); //current year zeller dt2=new zeller(cal.get(calendar.month),cal.get(calendar.day_of_month),cal.get(calendar.year)); int yrs=dt2.getc_year()-getc_year(); //get difference // check conditions if(yrs>=21 && countryofbirth.equals("usa")) return true; else if(yrs>=19 && countryofbirth.equals("canada")) return true; else return false; //otherwise return false } /** * in example please pass method value class attribute validcountries ensure not use invalid values (for example “us” instead of “usa”) . */ public boolean validcountries(string countryofbirth) { if(countryofbirth.equals("usa") || countryofbirth.equals("canada")) return true; else { system.out.println("not valid country"); return false; } } public static void main (string args[]) { person person1 = new person("danielle", 07, 04, 1999, "usa"); person person2 = new person("bob", 11, 05, 1987, "usa"); person1.add(person1); system.out.println("test : " + person2); } } public class couple extends person { private string personname1; private string personname2; public void add(person p) { this.personname1="unknown person 1"; this.personname2="unkown person 2"; } public static void main (string args[]){ couple person1 = new couple(); couple person2 = new couple(); person1.add(person2); } }
please let me know if looking for. can person class.
public class person { private string name; public person(string name){ this.name = name; } public string getname() { return name; } public void setname(string name) { this.name = name; } public static couple add(person p1, person p2){ couple c = new couple(p1,p2); system.out.println(c.tostring()); return c; } } this couple class -
public class couple { private person person1; private person person2; public couple(person p1, person p2){ this.person1 = p1; this.person2 = p2; } public person getperson1() { return person1; } public void setperson1(person person1) { this.person1 = person1; } public person getperson2() { return person2; } public void setperson2(person person2) { this.person2 = person2; } public string tostring(){ return this.getperson1().getname() + " "+ this.getperson2().getname(); } } and class runs main method -
public class javaapplication13 { public static void main(string[] args) { person p1 = new person("a"); person p2 = new person("b"); person.add(p1, p2); } } please let me know if looking for.
Comments
Post a Comment