I'm currently working on a program, and in my subclass, I need to have a no argument constructor that initializes the object with empty strings.
I've tried using super, I have setter and getter methods, but I keep getting "the field Person.name is not visible". I get this for address and phoneNumber as well.
How do I make it so it is visible, and I can initialize the objects without giving the constructor arguments? Please let me know if I'm doing anything wrong and need to fix something (:
// Create a class named Person
public class Person {
// Fields: name, address, and phone number. (2 points)
private String name;
private String address;
private String phoneNumber;
// No argument constructor that initializes the object with empty strings for name, address, and phone. (2 points)
public Person () {
super();
this.name = "";
this.address = "";
this.phoneNumber = "";
}
// 3 argument constructor that initializes the object with a name, address, and a phone number. (2 points)
public Person2 (String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
// Getter/setter methods for each of the fields. (3 points)
// set/get name
public void setName (String name) {
this.name = name;
}
public String getName () {
return this.name;
}
// set/get address
public void setAddress (String address) {
this.address = address;
}
public String getAddress () {
return this.address;
}
// set/get phone number
public void setPhoneNumber (String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPhoneNumber () {
return this.phoneNumber;
}
// toString method that returns a string for the name, address, and phone number (2 points)
// (you can override the toString method as part of a class which is pretty swag)
public String toString() {
return "Name: " + name + "\n" + "Address: " + address + "\n" + "Phone: " + phoneNumber;
}
}
// Create a subclass of Person named Customer
class Customer extends Person {
// A field for a customer number. (1 point)
private String customerNumber;
public Customer () {
// A no argument constructor that initializes the object with an empty string for the name, address, phone, and customer number. (2 points)
super(name, address, phoneNumber);
}
// A 4 argument constructor that initializes the object with a name, address, a phone number, and a customer number. (2 points)
public Customer2 (String name, String address, String phoneNumber, String customerNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.customerNumber = customerNumber;
}
// Getter/setter method for the customer number field. (1 point)
public void setCustomerNumber (String customerNumber) {
this.customerNumber = customerNumber;
}
// toString method that prints the information from the Person toString as well as the customer number (2 points)
public String toString() {
return "Name: " + name + "\n" + "Address: " + address + "\n" + "Phone: " + phoneNumber + "\n" + "Customer Number: " + customerNumber;
}
}
If a field is marked with private access then it can only be accessed from inside that class or instances of it. You should use the get methods. Or, you can get the result of toString and build on that.
Also, all constructors should have the same name as the class (no "2" added).
public class Person {
private String name;
private String address;
private String phoneNumber;
public Person() {
this("", "", "");
}
public Person(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public void setName (String name) {
this.name = name;
}
public String getName () {
return this.name;
}
public void setAddress (String address) {
this.address = address;
}
public String getAddress () {
return this.address;
}
public void setPhoneNumber (String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPhoneNumber () {
return this.phoneNumber;
}
#Override
public String toString() {
return "Name: " + name + "\n" + "Address: " + address + "\n" + "Phone: " + phoneNumber;
}
}
// Create a subclass of Person named Customer
class Customer extends Person {
private String customerNumber;
public Customer () {
this("", "", "", "");
}
public Customer (String name, String address, String phoneNumber, String customerNumber) {
super(name, address, phoneNumber);
this.customerNumber = customerNumber;
}
public String getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber (String customerNumber) {
this.customerNumber = customerNumber;
}
#Override
public String toString() {
return super.toString() + "\n" + "Customer Number: " + customerNumber;
}
}
You cannot access or assign to a private field in any other class than the one that it is declared in.
You cannot declare a constructor with any name other than the name of the class. Thus Person2 and Customer2 are not neither valid constructors or valid methods. (A method requires a return type!)
A constructor must explicitly (via a super call) or implicitly chain a no-args constructor in its superclass.
Basically, your choices for initializing a private field in a superclass are either use a super(...) call to chain the a superclass constructor passing the value OR call a superclass setter method within the subclass constructor.
For example, the 4 arg constructor in Customer could be:
public Customer (String name, String address,
String phoneNumber, String customerNumber) {
super(name, address, phoneNumber);
this.customerNumber = customerNumber;
}
or
public Customer (String name, String address,
String phoneNumber, String customerNumber) {
super(); // you could leave this out - it is implied
setName(name);
setAddress(address);
setPhoneNumber(phoneNumber);
this.customerNumber = customerNumber;
}
IMO, the former is better. It is more concise and more readable.
The toString() method in Customer cannot refer directly to the private fields of the Person. It could use the fields' getters.
I don't think there is any actual need to call the super constructor in the person class. However, in the constructor for the costumer class, you should just call the super constructor with no arguments.
Edit: or you could initiate the values directly upon declaration, like the guy above me said.
PS. kinda unrelated, but, you could have one constructor but give the arguments default values. So you can call it with and without arguments with no need to override it.
There are a few issues in the snippet that you provided:
There is no need to define different names for constructors of the same class. The fact that their signature (i.e. set of input parameters) is different suffices.
Regarding your question, the error is that you're trying to access Person's private fields in the Customer's constructors directly. There are two ways to fix this issue:
Change the Person fields' scope from private to protected. This will allow any class that inherits the Person class to access the aforementioned fields directly.
When in Customer class, use the getter/setter methods to access the private fields.
Related
I am working on a code for school and part of our topic is on the use of constructors.
I have a constructor implemented into my code and when I run it, I am getting most of the output correct. The string arguments are returning a "null" instead of the string values I have entered. I feel like the error is in my constructor under the dog class, but I don't know where to go from here.
public class Dog {
// instance variables
private String type;
private String breed;
private String name;
private String topTrick;
// constructor
public Dog(String type, String breed, String name) {
}
// methods
public void setTopTrick(String topTrick) {
this.topTrick = topTrick;
}
// method used to print Dog information
public String toString() {
String temp = "\nDOG DATA\n" + name + " is a " + breed +
", a " + type + " dog. \nThe top trick is : " +
topTrick + ".";
return temp;
}
}
public class Corgi extends Dog {
// additional instance variables
private int weight;
private int age;
// constructor
public Corgi(String type, String breed, String name, int pounds, int years) {
// invoke Dog class (super class) constructor
super(type, breed, name);
weight = pounds;
age = years;
}
// mutator methods
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// override toString() method to include additional dog information
#Override
public String toString() {
return (super.toString() + "\nThe Corgi is " + age +
" years old and weighs " + weight + " pounds.");
}
}
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Corgi spot = new Corgi("herding group","Golden Retriever" ,"Frank", 0, 0);
spot.setTopTrick("Hoop Jumping");
System.out.print(spot.toString());
}
}
My output is DOG DATA
null is a null, a null dog.
The top trick is : Hoop Jumping.
The Corgi is 0 years old and weighs 0 pounds.
While you are passing the constructor values for type, breed and name, you are not assigning them to class variables.
Easiest fix in my opinion would be to change:
public Dog(String type, String breed, String name) {
}
to
public Dog(String type, String breed, String name) {
this.type=type;
this.breed=breed;
this.name=name;
}
public Dog(String type, String breed, String name) {
this.type = type;
this.breed = breed;
this.name = name;
}
The difference between "this.type" and "type" is, that the first refers to the object's type that you are creating, the latter is the parameter handed over via the constructor parameter.
The problem is you are calling "super(type, breed, name)" but in parent constructor you are not assigning it to the instance of the object,So because the value of "type", "breed" and "name" is null initially, it is returning the same.
Best way to do it is:
public Dog(String type, String breed, String name) {
this.type = type;
this.breed = breed;
this.name = name;
}
where the values are assigned to the instance of that object.
first time I've tried this. I need to be able to replace an email for subclass Student and sublass Teacher after an email has been inputted, I have a parent class and superclass which is where I believe I need to add my changeEmail method. I may be a way off here but can I use stringBuilder or is there an easier way? Real noob when it comes to this.
SUBCLASS -
public class Teacher extends Member
{
private String qualifications;
public Teacher(String name, String email, String qualifications)
{
super(name, email);
this.qualifications = qualifications;
}
public String getQualifications()
{
return qualifications;
}
public String toString()
{
StringBuffer details = new StringBuffer();
details.append(super.getName());
details.append(' ');
if(qualifications != null && qualifications.trim().length() > 0) {
details.append("(" + qualifications + ")");
details.append(' ');
}
details.append(super.getEmail());
return details.toString();
}
}
SUBCLASS -
public class Student extends Member
{
private int attendance;
public Student(String name, String email)
{
super(name, email);
this.attendance = 0;
}
public int getAttendance()
{
return attendance;
}
public void markAttendance(int attendance)
{
this.attendance += attendance;
}
public void print()
{
System.out.println(super.getName() + " (" + attendance + ")");
}
}
SUPERCLASS -
public class Member
{
private String email;
private String name;
public Member(String name, String email)
{
this.name = name;
this.email = email;
}
public String getEmail()
{
return email;
}
public String getName()
{
return name;
}
public String changeEmail()
{
//..........
}
}
Since changeEmail is a public method in the superclass, the subclasses can access it too. Student (as well as Teacher) is a Member.
public String changeEmail(String newEmailAddress) {
String old = email;
this.email = newEmailAddress;
return old;
}
What I changed was adding a parameter (String newEmailAddress) and then set the new value to the email instance field.
(EDIT: I updated the answer to return the old email address. I don't know why a method like this would return anything but anyways..)
That is called inheritance, basically if you have some shared variables, you can use some parent class and with the keyword extends create some subclasses.
All subclasses, which inherits the parent class, can have their own class variables, but also are having the parent variables.
In your case you can image the diagram like that- obvious, doesnt?
So...
Parent class member is having these class variables:
- String : mail
- String : name
You have two subclasses- Student and Teacher:
Teacher class variables:
qualifications
mail, name (inherited from parent!)
Student class variables:
attendance
mail, name (inherited from parent!)
Notice- with the keyword super you are calling the constructor (or simply "class" other methods) from the parent, so in Teacher and Student class, you will call exactly following:
public Member(String name, String email) {
this.name = name;
this.email = email;
}
To be able change the email, you need following
1) implement methods in parent class
2) optional- add call to child classes, and for usage outside the class also add some external method (without this you can still use public parent class methods)
Eg.
in parent
public void changeEmail(String newEmail) {
this.email = newEmail;
}
public String changeEmailWithReturnOld(String newEmail) {
String oldMail = this.email;
changeEmail(newEmail); //calling above
return oldMail;
}
In childs
public String changeTheMailWithReturnOld(String newMail) {
return super.changeEmailWithReturnOld(newMail); //super means super class, parent
}
Clear? :)
Then you can call following:
Teacher teacher1 = new Teacher("foo", "foo#foo.foo", "whateverFoo");
teacher1.changeEmail("someNewFoo#foo.foo"); //parent method
teacher1.changeEmailWithReturnOld("someNewFoo#foo.foo"); //Child method
How do I create a class that has different lengths of arguments?
public static void main(String[] args) {
group g1 = new group("Redskins");
group g2 = new group("Zack", "Mills", 21);
group g3 = new group("John","Smith",20);
group g4 = new group("Fred","Fonsi",44);
group g5 = new group("Jeb","Bush",26);
System.out.println(g1.getName());
}
}
I want to be able to display the team name (redskins) and then each member after that using one method.
I've tried using two methods and got that to work, but can't get one.
I was thinking about possibly using an array but not sure if that would work.
Thanks for any help.
I have three classes the main, student, and group.
I need the group class to display the group name and then figure out how to display the students information underneath. The only thing, is that my assignment is vague about whether I can use two methods or one.
public class student {
String firstName;
String lastName;
int age;
student(String informedFirstName, String informedLastName, int informedAge){
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
}
String getName()
{
return "Name = " + firstName + " " + lastName + ", " + "Age = " + age;
}
}
public class Team{
String name;
Set<Player> players;
public Team(String name){
this.name = name;
}
public void addPlayer(Player p){
players.add(p);
}
}
public class Player{
String name;
etc
}
EDIT for revised question:
Ok, Im going to show a lot here. Heres what a proper Java versio of what you want for student.
public class Student {
private String firstName;
private String lastName;
private int age;
public Student(String firstName, String lastName, int age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
/*
* Use:
* Student s = new Student(Bill, Nye, 57);
* System.out.println(s.toString());
*/
#Override
public String toString() {
return "First Name: " + firstName + ", Last Name: " + lastName + ", Age: " + age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
The Things to take away from this.
1) Capitalize the first letter of class names! (Student)
2) note the class variables are private (look here for a tutorial Java Class Accessibility) and have getters and setter to control access outside the class.
3) I dont say "getName()" and return name and age. This doesnt make sense. Instead i make it so when you go toString() it shows all the relevant information.
4) Java is an object oriented language which means the classes that model data are supposed (to some extent) model appropriately to the way they are used in real life. This makes it more intuitive to people reading your code.
5) if your Group class (note the capital!) needs to contain many Students use a LIST such as an ArrayList. Arrays would make no sense because you dont know how many Students are going to be in each Group. A SET like i used above is similar to a list but only allows ONE of each item. For simplicity use a list though
6) the THIS operator refers to class (object) variables. In the constructor this.firstName refers to the firstName within the Class (object...an instance of the class) whereas just firstName would refer to the variable in the contructor and not alter the class variable.
use the constructor for that
class group {
String fname,lname;
group(String fname ){
this.fname=fname;
}
group(String fname,String lname){
this.fname=fname;
this.lname=lname;
}
group(String fname,String lname,int age){
this.fname=fname;
this.lname=lname;
this.age=age;
}
public String getName(){
return fname+lname+age;
}
}
I have been struggling with setters and getters in java for quite a long time now.
For instance, if I want to write a class with some information as name, sex, age etc with appropriate set and get methods. Then in a another class I want to test my set and getters with this as a example:
personInfo = myInfo() = new Personinfo("Anna", "female", "17");
How do I do that?
I know that I can have a printout like:
public void printout() {
System.out.printf("Your name is: " + getName() +
" and you are a " + getSex());
}
This is a simple example to show you how to do it:
public class Person {
private String name;
private String gender;
private int age;
Person(String name, String gender, int age){
this.name = name;
this.gender = gender;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setGender(String gender){
this.gender = gender;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public String getGender(){
return this.gender;
}
public int getAge(){
return this.age;
}
public static void main(String[] args)
{
Person me = new Person("MyName","male",20);
System.out.println("My name is:" + me.getName());
me.setName("OtherName");
System.out.println("My name is:" + me.getName());
}
}
This will print out:
My name is:MyName
My name is:OtherName
Let eclipse handler it for you
Click on your variable
Source > Generate Setter / Getter
You need to create an object of one class in the other class. You can then call the .get() and .set() methods on them. I will post an example in 2 minutes
First class (i'll call it Person) will have methods to return its fields
private String name = "";
private String age = 0;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
The second class will call those methods after it creates an object of the first class
bob = new Person("Bob", 21);
System.out.println("Your name is: " + bob.getName() +
" and you are " + bob.getAge());
The point of getters and setters is to let you limit/expand the scope or functionality of your property, independent of each other.
You may want your 'name' property to be readonly outside of your PersonInfo class. In this case, you have a getter, but no setter. You can pass in the value for the readonly properties through the constructor, and retrieve that value through a getter:
public class PersonInfo
{
//Your constructor - this can take the initial values for your properties
public PersonInfo(String Name)
{
this.name = Name;
}
//Your base property that the getters and setters use to
private String name;
//The getter - it's just a regular method that returns the private property
public String getName()
{
return name;
}
}
We can use getName() to get the value of 'name' outside of this class instance, but since the name property is private, we can't access and set it from the outside. And because there is no setter, there's no way we can change this value either, making it readonly.
As another example, we may want to do some validation logic before modifying internal values. This can be done in the setter, and is another way getters and setters can come in handy:
public class PersonInfo
{
public PersonInfo(String Name)
{
this.setName(Name);
}
//Your setter
public void setName(String newValue)
{
if (newValue.length() > 10)
{
this.name = newValue;
}
}
Now we can only set the value of 'name' if the length of the value we want to set is greater than 10. This is just a very basic example, you'd probably want error handling in there in case someone goes jamming invalid values in your method and complains when it doesn't work.
You can follow the same process for all the values you want, and add them to the constructor so you can set them initially. As for actually using this pattern, you can do something like the following to see it in action:
public static void main(String[] args)
{
PersonInfo myInfo = new PersonInfo("Slippery Sid",97,"male-ish");
var name = myInfo.getName();
System.out.printf("Your name is: " myInfo.getName() + " and you are a " myInfo.getSex());
myInfo.setName("Andy Schmuck");
System.out.printf("Your name is: " myInfo.getName() + " and you are a " myInfo.getSex());
}
You create an object by instantiating the constructor as follows
Personinfo pi = new Personinfo("Anna", "female", "17");
You can then call methods upon that object as follows
pi.setName("Alan");
or
pi.getName();
here's how you do it:
public class PersonInfo {
private String name;
private String sex;
private int age;
/** GETTERS **/
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public int getAge(){
return age;
}
/** SETTERS **/
public void setName(String name){
this.name = name;
}
public void setSex(String sex){
this.sex = sex;
}
public void setAge(int age){
this.age = age;
}
}
class Test{
public static void main(String[] args){
PersonInfo pinfo = new PersonInfo();
pinfo.setName("Johny");
pinfo.setSex("male");
pinfo.setAge(23);
//now print it
System.out.println("Name: " + pinfo.getName());
System.out.println("Sex: " + pinfo.getSex());
System.out.println("Age: " + pinfo.getAge());
}
}
Or you can add this as well:
#Override
public String toString(){
return "Name: " + this.name + "\n" +
"Sex: " + this.sex + "\n" +
"Age: " + this.age;
}
and then just to a .toString
EDIT:
Add this constructor in the class to initialize the object as well:
public PersonInfo(String name, String sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
In personInfo:
public Person(String n, int a, String s){
this.name=n;
this.age=a;
this.sex=s;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public String getSex(){
return this.sex;
}
public void setName(String n){
this.name = n;
}
public void setAge(int a){
this.age = a;
}
public void setSex(String s){
this.sex = s;
}
Then fix the print statement:
System.out.println("Your name is: " + myInfo.getName() + " and you are a " + myInfo.getSex());
I have created a (Person,Student,Employee,Faculty and Staff)classes. Person has to subclasses Student and Employee.
Employee has two subclasses Faculty and Staff. I have done all the codings an they are working fine except my driver class TestPerson program its giving compilation errors
Note: A test program that Creates a Person,Student,Employee,Faculty,Staff, and invokes their toString Method.
The errors of driver class TestPerson.java are below:-
error: constructor Student in class Student cannot be applied to given types;
error: no suitable constructor found for Employee(String,String,String,String)
error: constructor Faculty in class Faculty cannot be applied to given types;
error: no suitable constructor found for Staff(String,String,String,String)"
**I am just providing the codes of the driver class. If you need my other codings of the other classes, please state in the comment, and I will immediately post it.
Please see my codings below:-
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John Doe", "123 Somewhere", "415-555-1212", "johndoe#somewhere.com");
Person student = new Student("Mary Jane", "555 School Street", "650-555-1212", "mj#abc.com", "junior");
Person employee = new Employee("Tom Jones", "777 B Street", "40-88-889-999", "tj#xyz.com");
Person faculty = new Faculty("Jill Johnson", "999 Park Ave", "92-52-22-3-333", "jj#abcxyz.com");
Person staff = new Staff("Jack Box", "21 Jump Street", "707-21-2112", "jib#jack.com");
System.out.println(person.toString() + "\n");
System.out.println(student.toString() + "\n");
System.out.println(employee.toString() + "\n");
System.out.println(faculty.toString() + "\n");
System.out.println(staff.toString() + "\n");
}
}
//Person Class
public class Person {
private String name;
private String address;
private String phone_number;
private String email_address;
public Person() {
}
public Person(String newName, String newAddress, String newPhone_number, String newEmail){
name = newName;
address = newAddress;
phone_number = newPhone_number;
email_address = newEmail;
}
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void setAddress(String newAddress){
address = newAddress;
}
public String getAddress(){
return address;
}
public void setPhone(String newPhone_number){
phone_number = newPhone_number;
}
public String getPhone(){
return phone_number;
}
public void setEmail(String newEmail){
email_address = newEmail;
}
public String getEmail(){
return email_address;
}
public String toString(){
return "Name :"+getName();
}
}
//Student class
public class Student extends Person {
public final String class_status;
public Student(String name, String address, int phone, String email, String classStatus) {
super(name, address, phone, email);
class_status = classStatus;
}
public String toString(){
return "Student Status: " + super.getName();
}
}
//Employee Class
import java.util.Date;
public class Employee extends Person{
private String office;
private double salary;
private Date hire;
public Employee() {
}
public Employee(String name, String address, int phone, String email){
super(name, address, phone, email);
}
public Employee(String office, double salary, Date hire){
this.office = office;
this.salary = salary;
this.hire = hire;
}
public void setOffice(String office){
this.office = office;
}
public String getOffice(){
return this.office;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return this.salary;
}
public void setHire(Date hire){
this.hire = hire;
}
public Date getHire(){
return this.hire;
}
public String toString(){
return "Office " + super.getName();
}
}
//Faculty Class
public class Faculty extends Employee {
private String officeHours;
private int rank;
public Faculty(String name, String address, int phone, String email) {
super(name, address, phone, email);
}
public String toString(){
return "Office " + super.getOffice();
}
}
//Staff Class
public class Staff extends Employee {
private String title;
public Staff(String name, String address, int phone, String email) {
super(name, address, phone, email);
}
public Staff(String title){
this.title = title;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public String toString(){
return "Title :" + super.getName();
}
}
The reason you are getting those errors is that the constructors don't exist.
error: constructor Student in class Student cannot be applied to given
types; error: no suitable constructor found for
Employee(String,String,String,String)
That means you will not get the code to compile until you have this:
Student(String name, String addr, String phone, String email) {
....
}
Assuming you have set the properties in the constructor, toString would look like this:
public String toString() {
return this.name + "\n" + this.addr + "\n" + this.phone + "\n" + this.email;
}
UPDATE
Your problem is that Student has only this constructor:
public Student(String name, String address, int phone, String email, String classStatus)
Student needs a constructor which takes only four strings as its parameters. Alternatively, you can make everything take the five parameters you specified.
It's perhaps not related to the question itself, but I think the design could be refined like this:
define abstract class Role with the name of role
define classes Student, Employee, Staff whatever inheriting Role
define class Person with common properties for all person type, names etc, and having property of type Role inside
then define toString on Person and in all Role implementation
in this way you will be able to extend or modify Persons independently from Roles, which makes the design more flexible
The constructor of Person requires a String as third argument, but you are trying to pass int phone to the super-constructor in your sub-classes. That won't work because it's the wrong type.
By the way: you should always represent phone numbers with strings, not with integers.
It doesn't make sense to add or subtract phone numbers.
Integers don't allow leading zeros, which are used for area codes in some
countries.
Integers can't be larger than 2147483648. When you
try to store very long telephone numbers, this seemingly arbitrary
limit will cause very unexpected bugs.
First Set the Phone Number datatype as integer in all the classes ..
main Function will be:
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John Doe", "123 Somewhere", "415-555-1212",
"johndoe#somewhere.com");
Person student = new Student("Mary Jane", "555 School Street",
650-555-1212, "mj#abc.com", "junior");
Person employee = new Employee("Tom Jones", "777 B Street",
40-88-889-999, "tj#xyz.com");
Person faculty = new Faculty("Jill Johnson", "999 Park Ave",
92-52-22-3-333, "jj#abcxyz.com");
Person staff = new Staff("Jack Box", "21 Jump Street", 707-21-2112,
"jib#jack.com");
System.out.println(person.toString() + "\n");
System.out.println(student.toString() + "\n");
System.out.println(employee.toString() + "\n");
System.out.println(faculty.toString() + "\n");
System.out.println(staff.toString() + "\n");
}
}