1//CodeHS AP Computer Science A Java, 9.5.7: Creating .equals
2//DO NOT COPY AND PASTE, YOUR TEACHER CAN SEE YOUR EDIT HISTORY.
3//Part 1/2
4
5//PersonTester.java
6
7import java.util.Scanner;
8
9public class PersonTester
10{
11 public static void main(String[] args)
12 {
13 Scanner scanner = new Scanner(System.in);
14
15 System.out.print("Please enter the Person's name: ");
16 String tempName = scanner.nextLine();
17 System.out.print("Please enter the Person's birthday: ");
18 String tempBirthday = scanner.nextLine();
19
20 Person person = new Person(tempName, tempBirthday);
21 tempName = "";
22 tempBirthday = "";
23
24 System.out.print("Please enter the Student's name: ");
25 tempName = scanner.nextLine();
26 System.out.print("Please enter the Student's birthday: ");
27 tempBirthday = scanner.nextLine();
28 System.out.print("Please enter the Student's grade: ");
29 int tempGrade = scanner.nextInt();
30
31 Person student = new Student(tempName, tempBirthday, tempGrade);
32 System.out.println(person.equals(student));
33
34 }
35}
1//CodeHS AP Computer Science A Java, 9.5.7: Creating .equals
2//DO NOT COPY AND PASTE, YOUR TEACHER CAN SEE YOUR EDIT HISTORY.
3//Part 2/2
4
5//Person.java
6
7public class Person {
8
9 private String name;
10 private String birthday;
11
12 public Person (String name, String birthday)
13 {
14 this.name = name;
15 this.birthday = birthday;
16 }
17
18 public String getBirthday(){
19 return birthday;
20 }
21
22 public String getName(){
23 return name;
24 }
25
26 public boolean equals(Person person){
27 if(person.getName().equals(this.name) && person.getBirthday().equals(this.birthday)){
28 return true;
29 }else{
30 return false;
31 }
32 }
33
34}
35