I have a Java Programming question. I am working on an assignment where I need t
ID: 3762830 • Letter: I
Question
I have a Java Programming question.
I am working on an assignment where I need to create the following four classes.
Person class- has the data fields of first name, last name, street address, zip code and phone number. Needs a method to set each data field, using a series of dialog boxes and a display method to display all of a Person’s data on a single line on the command line.
CollegeEmployee class- extends the Person class with the additional fields for social security number, annual salary and department name. It needs methods that override the Person methods to accept and display all CollegeEmployee data.
Faculty class – extends the CollegeEmployee class plus adds a Boolean field that indicates if the Faculty member has tenure. It needs methods that override the CollegeEmployee methods to accept and display this additional piece of information.
Student class- descends from Person with the additional fields for major field of study and GPA. Plus methods that override the Person methods to accept and display these additional facts.
Then I need to create an application, CollegeList,that declares an array of four CollegeEmployees, three Faculty, and seven Students. Prompt the user to specify which type of person’s data they are enterting (C, F, or S) or allow the user to quit (Q). While they continue the program should accept the data for the type of Person but if they try to enter more than four CollegeEmployees, three Faculty, or seven Students they should get an error message. When the user quits the program should display a report on the screen listing each group under the appropriate heading “College Employees”, “Faculty” or “Students”. If there was no data entered for a type of Person display a message indicating this under the appropriate setting.
This is what I have but it is not giving me the dialog boxes to enter the data. I can’t figure out what I am doing wrong.
Person Class
import javax.swing.*;
public class Person {
public static void main(String[] args)
{
}
private String firstName;
private String lastName;
private String streetAddress;
private String zipCode;
private String phoneNum;
public void getData(){
firstName = JOptionPane.showInputDialog(null, "Please enter First Name:");
lastName = JOptionPane.showInputDialog(null, "Please enter Last Name:");
streetAddress = JOptionPane.showInputDialog(null, "Please enter Street Address:");
zipCode = JOptionPane.showInputDialog(null, "Please enter Zip Code:");
phoneNum = JOptionPane.showInputDialog(null, "Please enter Phone Number:");
}
public void displayData(){
System.out.println(firstName + " " + lastName + " " + streetAddress + " " + zipCode + " " + phoneNum);
}
}
CollegeEmployee class
import javax.swing.*;
public class CollegeEmployee extends Person {
static String ssn;
static String sal;
static String dept;
@Override
public void getData()
{
String temp;
super.getData();
{
ssn = JOptionPane.showInputDialog(null, "Please enter Social Security Number:");
sal = JOptionPane.showInputDialog(null, "Please enter Salary:");
dept = JOptionPane.showInputDialog(null, "Please enter Department:");
}
}
@Override
public void displayData()
{
super.displayData();
{
System.out.println(ssn + " " + sal + " " + dept);
}
}
}
Faculty class
import javax.swing.*;
public class Faculty extends CollegeEmployee{
private boolean tenure;
@Override
public void getData()
{
super.getData();
{
tenure = JOptionPane.showInputDialog(null, "Does the Professor have tenure?") != null;
}
}
@Override
public void displayData()
{
super.displayData();
{
System.out.println(tenure);
}
}
}
Student class
import javax.swing.*;
public class Student extends Person {
private String major;
private String gpa;
@Override
public void getData()
{
super.getData();
{
major = JOptionPane.showInputDialog(null, "Please enter Major:");
gpa = JOptionPane.showInputDialog(null, "Please enter GPA:");
}
}
@Override
public void displayData()
{
super.displayData();
{
System.out.println(major + " " + gpa);
}
}
}
CollegeList Program
import javax.swing.*;
public class CollegeList {
public static void main(String[] args) {
CollegeEmployee[] emp = new CollegeEmployee[4];
Faculty[] fac = new Faculty[3];
Student[] stud = new Student[7];
int empCount = 0, facCount = 0, studCount = 0;
char letter;
String input;
int x;
input = JOptionPane.showInputDialog(null, "Type C, F, S to enter data for" + " (C)College Employee (F)Faculty (S)Student " + " or type Q to quit");
letter = input.charAt(0);
while (letter != 'Q');
{
if(letter == 'C')
{
if(empCount < emp.length)
{
CollegeEmployee c = new CollegeEmployee();
c.getData();
emp[empCount] = c;
++empCount;
}
else
JOptionPane.showMessageDialog(null, "You have reached the limit of College Employee records that can be added.");
}
else if(letter == 'F')
{
if(facCount < fac.length)
{
Faculty f = new Faculty();
f.getData();
fac[facCount] = f;
++facCount;
}
else
JOptionPane.showMessageDialog(null, "You have reached the limit of Faculty records that can be added.");
}
else if(letter == 'S')
{
if(studCount < stud.length)
{
Student s = new Student();
s.getData();
stud[studCount] = s;
++studCount;
}
else
JOptionPane.showMessageDialog(null, "You have reached the limit of Student records that can be added.");
}
}
System.out.println(" College Employee:");
if(empCount == 0)
System.out.println("No College Employees records entered");
else
for(x = 0; x < empCount;++x)
emp[x].displayData();
System.out.println(" College Employee:");
System.out.println(" Faculty:");
if(facCount == 0)
System.out.println("No Faculty records entered");
else
for(x = 0; x < facCount;++x)
fac[x].displayData();
System.out.println(" Faculty:");
System.out.println(" Student:");
if(studCount == 0)
System.out.println("No Student records entered");
else
for(x = 0; x < studCount;++x)
stud[x].displayData();
System.out.println(" Student:");
}
}
Explanation / Answer
--------------------------------------------------class Person---------------------------------------------------------------------------
import javax.swing.*;
public class Person {
private String firstName;
private String lastName;
private String streetAddress;
private String zipCode;
private String phoneNum;
public void setData(String f_name,String l_name,String s_add, String z_code,String p_num)
{
firstName=f_name;
lastName=l_name;
streetAddress=s_add;
zipCode=z_code;
phoneNum=p_num;
}
public void getData()
{
firstName = JOptionPane.showInputDialog(null,"Please Enter First Name", JOptionPane.OK_CANCEL_OPTION);
lastName = JOptionPane.showInputDialog(null, "Please enter Last Name:", JOptionPane.OK_CANCEL_OPTION);
streetAddress = JOptionPane.showInputDialog(null, "Please enter Street Address:", JOptionPane.OK_CANCEL_OPTION);
zipCode = JOptionPane.showInputDialog(null, "Please enter Zip Code:", JOptionPane.OK_CANCEL_OPTION);
phoneNum = JOptionPane.showInputDialog(null, "Please enter Phone Number:", JOptionPane.OK_CANCEL_OPTION);
}
public void displayData()
{
System.out.println(firstName + " " + lastName + " " + streetAddress + " " + zipCode + " " + phoneNum);
}
}
--------------------------------------------------------------CollegeEmployee------------------------------------------------------------
import javax.swing.*;
public class CollegeEmployee extends Person {
static String ssn;
static String sal;
static String dept;
@Override
public void getData()
{
String temp;
super.getData();
{
ssn = JOptionPane.showInputDialog(null, "Please enter Social Security Number:");
sal = JOptionPane.showInputDialog(null, "Please enter Salary:");
dept = JOptionPane.showInputDialog(null, "Please enter Department:");
}
}
@Override
public void displayData()
{
super.displayData();
{
System.out.println(ssn + " " + sal + " " + dept);
}
}
}
----------------------------------------------------------------Faculty-------------------------------------------------------------
import javax.swing.*;
public class Faculty extends CollegeEmployee{
private boolean tenure;
@Override
public void getData()
{
super.getData();
{
tenure = JOptionPane.showInputDialog(null, "Does the Professor have tenure?") != null;
}
}
@Override
public void displayData()
{
super.displayData();
{
System.out.println(tenure);
}
}
}
---------------------------------------------------------------------Student------------------------------------------------
import javax.swing.*;
public class Student extends Person {
private String major;
private String gpa;
@Override
public void getData()
{
super.getData();
{
major = JOptionPane.showInputDialog(null, "Please enter Major:");
gpa = JOptionPane.showInputDialog(null, "Please enter GPA:");
}
}
@Override
public void displayData()
{
super.displayData();
{
System.out.println(major + " " + gpa);
}
}
}
------------------------------------------------------------CollegeList------------------------------
import javax.swing.*;
public class CollegeList {
public static void main(String[] args)
{
CollegeEmployee[] emp = new CollegeEmployee[4];
Faculty[] fac = new Faculty[3];
Student[] stud = new Student[7];
int empCount = 0, facCount = 0, studCount = 0;
char letter=' ';
String input;
int x;
while(letter != 'Q')
{
input = JOptionPane.showInputDialog(null, "Type C, F, S to enter data for" + " (C)College Employee (F)Faculty (S)Student " + " or type Q to quit");
letter = input.charAt(0);
if(letter == 'C')
{
if(empCount < emp.length)
{
CollegeEmployee c = new CollegeEmployee();
c.getData();
emp[empCount] = c;
++empCount;
}
else
{
JOptionPane.showMessageDialog(null, "You have reached the limit of College Employee records that can be added.");
}
}
else if(letter == 'F')
{
if(facCount < fac.length)
{
Faculty f = new Faculty();
f.getData();
fac[facCount] = f;
++facCount;
}
else
JOptionPane.showMessageDialog(null, "You have reached the limit of Faculty records that can be added.");
}
else if(letter == 'S')
{
if(studCount < stud.length)
{
Student s = new Student();
s.getData();
stud[studCount] = s;
++studCount;
}
else
JOptionPane.showMessageDialog(null, "You have reached the limit of Student records that can be added.");
}
}
System.out.println(" College Employee:");
if(empCount == 0)
System.out.println("No College Employees records entered");
else
for(x = 0; x < empCount;++x)
emp[x].displayData();
System.out.println(" College Employee:");
System.out.println(" Faculty:");
if(facCount == 0)
System.out.println("No Faculty records entered");
else
for(x = 0; x < facCount;++x)
fac[x].displayData();
System.out.println(" Faculty:");
System.out.println(" Student:");
if(studCount == 0)
System.out.println("No Student records entered");
else
for(x = 0; x < studCount;++x)
stud[x].displayData();
System.out.println(" Student:");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.