0

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);


}
}
2
  • It makes no sense to have Person extend Zeller. Commented Jul 17, 2015 at 16:20
  • person extends zeller because of a previous assignment. Commented Jul 18, 2015 at 17:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Agreed, doing the second part is pointless, utterly redundant.
Are you getting the error in main of Person class or in the main of Couple class. because main of Person class will always give you an error because type is wrong.
i'm still getting a compiler error when i add Person p1 = new Couple(); p1.add(p2); I'm getting "cannot find symbol - method add(Person)
@NalinAgrawal Your add method needs to be part of the Person class. First, create a class Couple that consists of two Person objects, Be sure to pass Person objects to the constructor. Then the __add__method should be added to the Person class that you have been using in the previous homework. That method should take 2 Person objects as arguments and return a Couple object. So, you should be able to run: aCouple = person1 + person2 print(aCouple1.person1.name + " " + aCouple.person2.name) .. new instruction from professor. Now i'm extremely confused. could you offer any guidance?
It makes no sense to me to provide add method to the Person class, when you already have constructor in Couple class that does the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.