Instruction:
Create a Java Program that will accept two values.
Determine if the two argument lists are identical or not.
Sample IO #1:
Sample Input:
Name1: Jose
Birthday: 1-1-2000
Name2: Jose
Birthday: 1-1-2000
Output:
IDENTICAL
Name: Jose
BirthDate: 1-1-2000
Name: Jose
BirthDate: 1-1-2000
Sample IO #2:
Sample Input:
Name1: Jose
Birthday: 1-1-2000
Name2: Jose
Birthday: 6-7-2012
Output:
NOT IDENTICAL
Name: Jose
BirthDate: 7-6-2000
Name: Jose
BirthDate: 6-7-2012
MyDate Class
Attribute/s: private int day, month, year;
Constructor/s:
public MyDate(int month, int day, int year)
Method/s:
Setters and getters, public boolean equals(Object o);
public int hashCode(); and
public String toString()
Person Class
Attribute/s: private String name; private MyDate dob;
Constructor/s:
public Person(String name, MyDate dob)
Method/s: Setters and getters,
public boolean equals(Object o);
public int hashCode(); and
public String toString()
Your Test class named TestPerson
import java.util.*;
class MyDate {
private int day, month, year;
// Constructor/s:
public MyDate(int month, int day, int year) {
this.day = day;
this.month = month;
this.year = year;
}
// Method/s:
public boolean equals(Object o) {
MyDate other = (MyDate) o;
return (this.day == other.day && this.month == other.month && this.year == other.year);
}
public int hashCode() {
return 4545;
}
public String toString() {
return "BirthDate: " + day + "-" + month + "-" + year;
}
// Setters and getters,
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @param day the day to set
*/
public void setDay(int day) {
this.day = day;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
}
class Person {
private String name;
private MyDate dob;
public Person(String name, MyDate dob) {
this.name = name;
this.dob = dob;
}
public boolean equals(Object o) {
Person person = (Person) o;
return (person.name.compareTo(this.name) == 0 && person.dob.equals(this.dob));
}
public int hashCode() {
return 78945;
}
public String toString() {
return "Name: " + name + "\n" + dob.toString();
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the dob
*/
public MyDate getDob() {
return dob;
}
/**
* @param dob the dob to set
*/
public void setDob(MyDate dob) {
this.dob = dob;
}
}
class TestPerson {
static Scanner keyboard = new Scanner(System.in);
static Person createPerson(int i) {
System.out.print("Name" + i + ": ");
String name = keyboard.nextLine();
System.out.print("Birthday: ");
String dob = keyboard.nextLine();
String values[] = dob.split("-");
int day = Integer.parseInt(values[0]);
int month = Integer.parseInt(values[1]);
int year = Integer.parseInt(values[2]);
return new Person(name, new MyDate(month, day, year));
}
public static void main(String[] args) {
Person p1 = createPerson(1);
Person p2 = createPerson(2);
if (p1.equals(p2)) {
System.out.println("IDENTICAL");
} else {
System.out.println("NOT IDENTICAL");
}
System.out.println(p1.toString());
System.out.println(p2.toString());
keyboard.close();
}
}
Comments
Leave a comment