Each class should include: The fields listed above A parameterized constructor,
ID: 3783323 • Letter: E
Question
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
Type Data Personal Joe Smith, 33, 100 Evergreen Ave, Seattle, WA, 98999 Personal Lawrence Williams, 45, 2000 1st St, Tacoma, WA, 98100 Personal Rachel Garcia, 12, 12 Forest Drive, Los Angelos, CA, 99301 Business Gregory Smith, 67, 360-888-7777, 360-555-6666 Business Jerome Bradley, 18, 216-111-2222, 253-444-7777 Business Susie Adams, 23, 253-333-7777, 425-666-9999Explanation / Answer
package myProject;
import java.util.*;
//Contact class definition
class Contact
{
//Instance variables
String firstName;
String lastName;
int age;
String address;
//Parameterized constructor
Contact(String fn, String ln, int a, String add)
{
firstName = fn;
lastName = ln;
age = a;
address = add;
}//End of parameterized constructor
}//End of class Contact
//Class PersonalContact derived from class Contact
class PersonalContact extends Contact
{
//Instance variable to store number of personal contact
public static int numberOfPersonalContact;
//Parameterized constructor
PersonalContact(String fn, String ln, int a, String add)
{
//Calls base class constructor
super(fn, ln, a, add);
//Increase the counter for personal contact
numberOfPersonalContact++;
}//End of constructor
//Returns number of personal contact
static int getnumberOfPersonalContact()
{
return numberOfPersonalContact;
}
//Returns the first name
String getFirstName()
{
return firstName;
}
//Sets first name
void setFirstName(String s)
{
firstName = s;
}
//Returns the last name
String getLastName()
{
return lastName;
}
//Sets last name
void setLastName(String s)
{
lastName = s;
}
//Returns age
int getAge()
{
return age;
}
//Sets age
void setAge(int a)
{
age = a;
}
//Returns address
String getAddress()
{
return address;
}
//Sets address
void setAddress(String add)
{
address = add;
}
//Overrides toString method to display output
public String toString()
{
String output = "Personal Contact: " + getFirstName() + " " + getLastName() + " " + getAge() + " " + getAddress();
return output;
}
}//End of class
//Creates class BusinessContact derived from class Contact
class BusinessContact extends Contact
{
//Instance variable to store number of business contact
public static int numberOfBusinessContact;
//Parameterized constructor
BusinessContact(String fn, String ln, int a, String add)
{
//Calls base class constructor
super(fn, ln, a, add);
//Increases the business contact by one
numberOfBusinessContact++;
}
//Returns number of personal contact
static int getnumberOfBusinessContact()
{
return numberOfBusinessContact;
}
//Returns first name
String getFirstName()
{
return firstName;
}
//Sets first name
void setFirstName(String s)
{
firstName = s;
}
//Returns last name
String getLastName()
{
return lastName;
}
//Sets last name
void setLastName(String s)
{
lastName = s;
}
//Returns age
int getAge()
{
return age;
}
//Sets age
void setAge(int a)
{
age = a;
}
//Returns address
String getAddress()
{
return address;
}
//Sets address
void setAddress(String add)
{
address = add;
}
//Overrides toString method to display output
public String toString()
{
String output = "Business Contact: " + getFirstName() + " " + getLastName() + " " + getAge() + " " + getAddress();
return output;
}
}//End of class
//Driver class
public class MyContact
{
static Contact contact[];
//Display menu method
static void plannerMenu()
{
System.out.println("Welcome to my planner!");
System.out.println("1. Print planner contacts.");
System.out.println("2. Print planner statistics.");
System.out.println("3. Exit.");
}
//Returns average age of contacts
static float averageAge()
{
float avgAge;
int totAge = 0;
//Counts the total contact
int totalContact = PersonalContact.getnumberOfPersonalContact() + BusinessContact.getnumberOfBusinessContact();
//Loops till total number of contacts
for(int c = 0; c < totalContact; c++)
//Calculates the total age
totAge += (contact[c].age);
//Calculates the average age
avgAge = totAge/totalContact;
//Return the average
return avgAge;
}
//Main method
public static void main(String ss[])
{
int ch;
//Scanner class object created to accept data
Scanner sc = new Scanner(System.in);
contact = new Contact[6];
//Initializes the object of contact for personal and business contacts
contact[0] = new PersonalContact("Joe", "Smith", 33, "100 Evergreen Ave, Seattle, WA, 98999");
contact[1] = new PersonalContact("Lawrence", "Williams", 45, "2000 1st St, Tacoma, WA, 98100");
contact[2] = new PersonalContact("Rachel", "Garcia", 12, "12 Forest Drive, Los Angelos, CA, 99301");
contact[3] = new BusinessContact("Gregory", "Smith", 67, "360-888-7777, 360-555-6666");
contact[4] = new BusinessContact("Jerome", "Bradley", 18, "216-111-2222, 253-444-7777");
contact[5] = new BusinessContact("Susie", "Adams", 23, "253-333-7777, 425-666-9999");
//Loops till user choice
do
{
//Display menu
plannerMenu();
//Accepts user choice
System.out.println("Enter your choice: ");
ch = sc.nextInt();
switch(ch)
{
case 1:
//Displays Personal contacts
for(int i = 0; i < PersonalContact.getnumberOfPersonalContact(); i++)
System.out.println(contact[i]);
//Displays Business contacts
for(int i = PersonalContact.getnumberOfPersonalContact(); i < BusinessContact.getnumberOfBusinessContact(); i++)
break;
//Display statistics
case 2:
System.out.println("Number of contacts:" + (PersonalContact.getnumberOfPersonalContact() + BusinessContact.getnumberOfBusinessContact()));
System.out.println("Number of Personal Contacts:" + PersonalContact.getnumberOfPersonalContact());
System.out.println("Number of Business Contact:" + BusinessContact.getnumberOfBusinessContact());
System.out.println("Average contact age:" + averageAge());
break;
case 3:
System.exit(0);
}
}while(true);
}//End of main method
}//End of class
Outout
Welcome to my planner!
1. Print planner contacts.
2. Print planner statistics.
3. Exit.
Enter your choice:
2
Number of contacts:6
Number of Personal Contacts:3
Number of Business Contact:3
Average contact age:33.0
Welcome to my planner!
1. Print planner contacts.
2. Print planner statistics.
3. Exit.
Enter your choice:
1
Personal Contact: Joe Smith 33 100 Evergreen Ave, Seattle, WA, 98999
Personal Contact: Lawrence Williams 45 2000 1st St, Tacoma, WA, 98100
Personal Contact: Rachel Garcia 12 12 Forest Drive, Los Angelos, CA, 99301
Business Contact: Gregory Smith 67 360-888-7777, 360-555-6666
Business Contact: Jerome Bradley 18 216-111-2222, 253-444-7777
Business Contact: Susie Adams 23 253-333-7777, 425-666-9999
Welcome to my planner!
1. Print planner contacts.
2. Print planner statistics.
3. Exit.
Enter your choice:
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.