Susan
Susan

Reputation: 1

Trouble with Add function in java

Directions: Create a new class Couple which is composed of two Person objects. The constructor,__init__(self,person1, person2) should take 2 Person objects as arguments. It should then set to instance attributes self.person1 & self.person2 to the values passed in to the constructor.

For Java, rather than overloading the + operator, just define the method add(Person p) as part of the person class, so the following could be called:

Person person1 = new Person(…);
Person person2 = new Person(…);
person1.add(person2);

Keep getting Cannot find add(person)

I'm not really sure where I'm going wrong here.

This is what I have so 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 so code spits year in format such as "2005"
    return C_Year;
}

public int getD_Century(){
    return C_Year/100;
}

public void setA_Month(int A) {
    int TempA;
    if(A == 1 || A == 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 the week is Sunday");
    }
    else if(R== 1){
        System.out.print("The day of the week is Monday");
    }
    else if(R== 2){
        System.out.print("The day of the week is Tuesday");
    }
    else if(R== 3){
        System.out.print("The day of the week is Wednesday");
    }
    else if(R== 4){
        System.out.print("The day of the week is Thursday");
    }


    else if(R== 5){
        System.out.print("The day of the week is Friday");
    }
    else if(R == 6){
        System.out.print("The day of the week is Saturday");
     }
  }
  }





class Person extends Zeller

{

private String countryOfBirth;
private String name;


   /** 
 * Remember to initialize all instance attributes in the constructor. The      instance attributes in this case
 * would be 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 a function getIDCardString(self) to the Person class that simply returns a string concatenation of
the self. name and self.birthday instance attributes. This is to be used on ID cards for all people
students, faculty, staff and 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;
}

/**
 * An instance method bornOnDay(self) that takes no parameters but simply calls the zeller(A,B,C,D)
 * method that you created in HW1. You can just include the zeller method in this Person class or import it.
 * bornOnDay(self) should return the string returned from zeller.

 */public String bornOnDay() 
{
    return getA_Month()+"/"+getB_Day()+"/"+getC_Year();
}

/**
 * An instance method isOldEnoughToDrink(country) that takes a string parameter country. This will return
true if the person is eligible to drink in the country passed to the method, false otherwise. In your
example please pass into the method a value from the class attribute validCountries to ensure that we
do 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 your example please pass into the method a value from the class attribute validCountries to ensure that we
do 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 a 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);


}
}

Upvotes: 0

Views: 156

Answers (1)

zookastos
zookastos

Reputation: 945

Please let me know if this is something you are looking for. This can be the 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 will be the 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 this will be the class that runs the 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 this is what you were looking for.

Upvotes: 1

Related Questions