Overview For this homework we will be creating a digital planner that can track
ID: 3785759 • Letter: O
Question
Overview
For this homework we will be creating a digital planner that can track contact details. The planner will only support a few entries.
Contact Classes
Create the following three contact classes (note the class hierarchy):
name
age
businessPhone
cellPhone
Each class should include:
The fields listed above
A parameterized constructor, no default constructors
Getters/setters for each field
An appropriate toString() method
Planner
Create a program that stores six Contact objects (three PersonalContact, three BusinessContact). Initialize your array like so:
//create my contacts
contacts[0] = new PersonalContact(...);
contacts[1] = new BusinessContact(...);
...
Use the following data for your contact objects:
Planner Menu
Add the following menu that lets you interact with your contacts array:
Welcome to my planner!
1. Print planner contacts.
2. Print planner statistics.
3. Exit.
Option 1
When printing the planner contacts, you should loop over your array of contacts and print the toString() method of each Contact object:
Personal Contact: Joe Smith (33), 100 Evergreen Ave Seattle, WA, 98999
Business Contact: Susie Adams (23), business - 253-333-7777, cell - 425-666-9999
...
Option 2
Print the following planner statistics. Your statistics should accurately reflect the data in your array (ie. if I change your contact array or any contact details, your planner statistics should still be accurate).
Number of contacts: 6
Number of personal contacts: 3
Number of business contacts: 3
Average contact age: 44
Note: Use the instanceof operator to identify which contacts are personal and which are business.
Option 3
After each user choice, the user should be presented with the menu repeatedly. If the user chooses option 3, the program will end.
Coding Guidelines
The output of your program should directly follow the instructions above
Make sure to use constants
Make sure to break your solution into several methods
Classes: Contact PersonalContact BusinessContact Fields:name
age
city
state
zip
businessPhone
cellPhone
Explanation / Answer
CONTACT.JAVA
public class Contact {
protected String name;
protected int age;
public Contact(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Contact other = (Contact) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Contact [name=" + name + ", age=" + age + "]";
}
}
---------------------------------------------------------------------------
//personalcontact.java
public class PersonalContact extends Contact{
public String address;
public String city;
public String state;
public int zip;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + zip;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
PersonalContact other = (PersonalContact) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
if (zip != other.zip)
return false;
return true;
}
@Override
public String toString() {
return "PersonalContact [address=" + address + ", city=" + city
+ ", state=" + state + ", zip=" + zip + "]";
}
public PersonalContact(String name, int age, String address, String city,
String state, int zip) {
super(name, age);
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
}
}
---------------------------------------------------------------------------------------------------------
//businesscontact.java
public class BusinessContact extends Contact{
public int businessPhone;
public int publicPhone;
public BusinessContact(String name, int age, int businessPhone,
int publicPhone) {
super(name, age);
this.businessPhone = businessPhone;
this.publicPhone = publicPhone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + businessPhone;
result = prime * result + publicPhone;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
BusinessContact other = (BusinessContact) obj;
if (businessPhone != other.businessPhone)
return false;
if (publicPhone != other.publicPhone)
return false;
return true;
}
public int getBusinessPhone() {
return businessPhone;
}
public void setBusinessPhone(int businessPhone) {
this.businessPhone = businessPhone;
}
public int getPublicPhone() {
return publicPhone;
}
public void setPublicPhone(int publicPhone) {
this.publicPhone = publicPhone;
}
@Override
public String toString() {
return "BusinessContact [businessPhone=" + businessPhone
+ ", publicPhone=" + publicPhone + ", name=" + name + ", age="
+ age + "]";
}
}
-----------------------------------------------------
//planner.java
import java.util.Scanner;
public class Planner {
public static void main(String[] args) {
Contact[] contacts = new Contact[6];
contacts[0] = new PersonalContact("Joe Smith", 33, "100 Evergreen Ave",
"Seattle", "WA", 98999);
contacts[1] = new PersonalContact("Lawrence Williams", 45,
"2000 1st St", "Tacoma", "WA", 98100);
contacts[2] = new PersonalContact("Rachel Garcia", 12,
"12 Forest Drive", "Los Angelos", "CA", 99301);
contacts[3] = new BusinessContact("Gregory Smith", 67,
360 - 888 - 7777, 360 - 555 - 6666);
contacts[4] = new BusinessContact("Jerome Bradley", 18,
216 - 111 - 2222, 253 - 444 - 7777);
contacts[5] = new BusinessContact("Susie Adams", 23, 253 - 333 - 7777,
425 - 666 - 9999);
int i = 1;
while (i == 1) {
System.out.println("1 planner contacts");
System.out.println("2 planner statistics");
System.out.println("3 exit");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
for (Contact contact : contacts) {
System.out.println(contact);
}
break;
case 2:
int personalContacts = 0;
int businessContacts = 0;
int contactsCount = 0;
for (Contact contact : contacts) {
contactsCount++;
if (contact instanceof PersonalContact) {
personalContacts++;
}
if (contact instanceof BusinessContact) {
businessContacts++;
}
}
System.out.println("the contacts are" + contactsCount);
System.out.println("the personal contact are"
+ personalContacts);
System.out.println("the business contacts are "
+ businessContacts);
break;
case 3:
System.exit(0);
default:
System.out.println("you chosen wrong option");
break;
}
}
}
}
output
1 planner contacts
2 planner statistics
3 exit
1
PersonalContact [address=100 Evergreen Ave, city=Seattle, state=WA, zip=98999]
PersonalContact [address=2000 1st St, city=Tacoma, state=WA, zip=98100]
PersonalContact [address=12 Forest Drive, city=Los Angelos, state=CA, zip=99301]
BusinessContact [businessPhone=-8305, publicPhone=-6861, name=Gregory Smith, age=67]
BusinessContact [businessPhone=-2117, publicPhone=-7968, name=Jerome Bradley, age=18]
BusinessContact [businessPhone=-7857, publicPhone=-10240, name=Susie Adams, age=23]
1 planner contacts
2 planner statistics
3 exit
2
the contacts are6
the personal contact are3
the business contacts are 3
1 planner contacts
2 planner statistics
3 exit
4
you chosen wrong option
1 planner contacts
2 planner statistics
3 exit
3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.