Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Contact Classes Copy your three contact classes from your last homework to a new

ID: 3791474 • Letter: C

Question

Contact Classes

Copy your three contact classes from your last homework to a new project.

Link to the three contact classes:

https://expirebox.com/download/7b9a3eae3a8706c6183207685c57fcb6.html

Change your Contact class to an abstract class. This will prevent instantiation of Contact objects.

Add an abstract method, public void validate(), to your Contact class.

Validation

Implement the validate() method in your BusinessContact class.

If the name field is the empty string or null, then throw a NullPointerException.

If the age field is not between 1-100, then throw an IllegalStateException

If either of the phone number fields is not exactly 12 characters, then throw an IllegalStateException

Implement the validate() method in your PersonalContact class.

Validate name and age as described above

If the address or city field is the empty string or null, then throw a NullPointerException.

If the state field is not exactly 2 characters, then throw an IllegalStateException

If the zip code field is not exactly 5 numeric characters, then throw an IllegalStateException

Note: you should not have any try-catch blocks in your contact classes.

Driver Program

Create a driver program that allows a user to create a personal or business contact. Your program should prompt the user for a contact type:

Create a new contact?
1. Personal
2. Business
2

It should then prompt the user for all details for that contact type:

Name? Susie Grace
Age? 39
Business Phone? 222-333-4444
Cell Phone? 555-666-7777

Call your validate() method to check for bad input and then print out the toString() of your new contact object.

Name? Susie Grace
Age? 39
Business Phone? 222-333-4444
Cell Phone? 555-666-7777

Business Contact: Susie Grace (39), business - 222-333-4444, cell - 555-666-7777

Validating User Input

Your driver program should have appropriate try-catch blocks to respond to bad user input. This includes NullPointerExceptions and IllegalStateExceptions. Each exception type should print an appropriate message to the console.

For example:

Name? Susie Grace
Age? -10
Business Phone? 222-333-4444
Cell Phone? 555-666-7777

Please enter a valid age from 1-100

Another Example:

Name? Susie Grace
Age? 39
Business Phone? 2223334444
Cell Phone? 555-666-7777

Please enter a phone number using the following format: ###-###-####.

Explanation / Answer

Please find below code for 4 classes as mentioned in question.

1. Abstract class Contact

2. BusinessContact

3. PersonalContact

4. Driver class.

The same solution is provided below :
----------------------------------------------------------------
File name : Contact.java
----------------------------------------------------------------
public abstract class Contact {

protected String n;
protected int a;

public Contact(String n, int a) {
this.n = n;
this.a = a;
}
  
public abstract void validate();

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + ((n == null) ? 0 : n.hashCode());
return result;
}

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 (a != other.a)
return false;
if (n == null) {
if (other.n != null)
return false;
} else if (!n.equals(other.n))
return false;
return true;
}

public String toString() {
return "Contact [name= " + n + ", age= " + a + "]";
}

}

----------------------------------------------------------------
Filename : BusinessContact.java
----------------------------------------------------------------

import java.util.regex.Pattern;

public class BusinessContact extends Contact {

public String busPhone;
public String pubPhone;

public BusinessContact(String n, int a, String busPhone, String pubPhone) {
super(n, a);
this.busPhone = busPhone;
this.pubPhone = pubPhone;
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((busPhone == null) ? 0 : busPhone.hashCode());
result = prime * result + ((pubPhone == null) ? 0 : pubPhone.hashCode());
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 (busPhone == null) {
if (other.busPhone != null)
return false;
} else if (!busPhone.equals(other.busPhone))
return false;
if (pubPhone == null) {
if (other.pubPhone != null)
return false;
} else if (!pubPhone.equals(other.pubPhone))
return false;
return true;
}

public String getBusinessPhone() {
return busPhone;
}

public void setBusinessPhone(String businessPhone) {
this.busPhone = businessPhone;
}

public String getPublicPhone() {
return pubPhone;
}

public void setPublicPhone(String publicPhone) {
this.pubPhone = publicPhone;
}

public String toString() {
return "BusinessContact: " + this.n + "(" + this.a + "), business - " + busPhone + ", cell - " + pubPhone;
}

@Override
public void validate() {
if (this.n == null || this.n.length() == 0) {
throw new NullPointerException("Please enter valid name.");
}
if (this.a < 1 || this.a > 100) {
throw new IllegalStateException("Please enter valid age between 1-100");
}
Pattern p = Pattern.compile("^([1-9]\d{2})([-])(\d{3})[-](\d{4})$");
if (!(p.matcher(this.getBusinessPhone()).matches())) {
throw new IllegalStateException("Please enter valid business phone number.");
}
if (!(p.matcher(this.getPublicPhone()).matches())) {
throw new IllegalStateException("Please enter valid public phone number.");
}

}

}

----------------------------------------------------------------
Filename : PersonalContact.java
----------------------------------------------------------------

import java.util.regex.Pattern;

public class PersonalContact extends Contact {

public String state;
public String address;
public String zipcode;
public String city;

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 String getZip() {
return zipcode;
}

public void setZip(String zip) {
this.zipcode = zip;
}

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 + ((zipcode == null) ? 0 : zipcode.hashCode());
return result;
}

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 (zipcode != other.zipcode)
return false;
return true;
}

public String toString() {
return "PersonalContact: " + this.n + "(" + this.a + "), address - " + address + ", city - " + city + ", state - " + state + ", zip - " + zipcode;
}

public PersonalContact(String name, int age, String address, String city, String state, String zip) {
super(name, age);
this.address = address;
this.city = city;
this.state = state;
this.zipcode = zip;
}

@Override
public void validate() {
if (this.n == null || this.n.length() == 0) {
throw new NullPointerException("Please enter valid name.");
}
if (this.a < 1 || this.a > 100) {
throw new IllegalArgumentException("Please enter valid age between 1-100");
}
if (this.getAddress() == null || this.getAddress().length() == 0) {
throw new NullPointerException("Please enter valid address");
}
if (this.getCity() == null || this.getCity().length() == 0) {
throw new NullPointerException("Please enter valid city name");
}
if (this.getState().length() != 2) {
throw new IllegalStateException("Please enter valid input for state");
}
Pattern p = Pattern.compile("^(\d{5})$");
if (!p.matcher(this.getZip()).matches()) {
throw new IllegalStateException("Please enter valid zipcode");
}
}

}

----------------------------------------------------------------
FileName: Driver.java
----------------------------------------------------------------

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
do {
System.out.println("Create a new contact?");
System.out.println("1. Business Contact");
System.out.println("2. Personal Contact");
System.out.println("3. Exit");
choice = sc.nextInt();
if (choice == 1) {
System.out.println("Name?");
String name = sc.next();
System.out.println("Age?");
int age = sc.nextInt();
System.out.println("Business phone?");
String businessPhone = sc.next();
System.out.println("Cell phone?");
String cellPhone = sc.next();
Contact contact = new BusinessContact(name, age, businessPhone, cellPhone);
try {
contact.validate();
System.out.println(contact);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else if (choice == 2) {
System.out.println("Name?");
String name = sc.next();
System.out.println("Age?");
int age = sc.nextInt();
System.out.println("Address?");
String address = sc.next();
System.out.println("City?");
String city = sc.next();
System.out.println("State?");
String state = sc.next();
System.out.println("Zipcode?");
String zipcode = sc.next();
Contact contact = new PersonalContact(name, age, address, city, state, zipcode);
try {
contact.validate();
System.out.println(contact);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} while (choice != 3);
sc.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote