Question

in java • Create an interface called Person. In this interface create a method called printData()...

in java

• Create an interface called Person. In this interface create a method called printData()


• Implement interface Person by classes Student, Teacher, Admin. You need to think the relevant data attributes for each class. Define atleast 4 attributes for each class.


• In each class override a method toString() and create a string which holds all data


• In the method printData in each class print the data using toString() method.

Design the UML and write a code to solve the above problem.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
interface Person{
    void printData();
}

class Student implements Person{
    String name,course;
    int id,grade;
    @Override
    public void printData() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", course='" + course + '\'' +
                ", id=" + id +
                ", grade=" + grade +
                '}';
    }
}

class Teacher implements Person{
    String name,course;
    int sal,subjects;
    @Override
    public void printData() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", course='" + course + '\'' +
                ", sal=" + sal +
                ", subjects=" + subjects +
                '}';
    }
}

class Admin implements Person{
    String name,department;
    int sal,experience;
    @Override
    public void printData() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Admin{" +
                "name='" + name + '\'' +
                ", department='" + department + '\'' +
                ", sal=" + sal +
                ", experience=" + experience +
                '}';
    }
}

OUTPUT :

<<interface Person +printdata0: void Student Teacher Admin +name: String +course: String +id int +grade int +name: String +co

Add a comment
Know the answer?
Add Answer to:
in java • Create an interface called Person. In this interface create a method called printData()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • a c++ program (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person...

    a c++ program (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the MyDate classto create an object for date hired. A faculty...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance()...

    Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance() methods, and an interface called AccountReceivable with a void deposit(double) method. Save these two interfaces in separate Java files. Then code a class, BusinessAccount, that implements the two interfaces defined above. Define and code the necessary instance variables and constructors. Override toString() so it will return the amount of a business account. Apply the particular operations, such as withdraw and deposit, required in implementing...

  • In Java Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

  • Part I (20%) [File: Student.java] Create a class called Student that has the following stored properties:...

    Part I (20%) [File: Student.java] Create a class called Student that has the following stored properties: • StudentID • First Name • Last Name Class Student should have read/write properties, constructor(s) and should implement the Academic interface. For academic methods, return zero for average, zero for credits and false for graduate. Also implement the toString() method that returns the above information as a String. Part II (5%) [File: Academic.java] Create an interface Academic that declares three methods: 1. average -...

  • Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should...

    Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should define a way to establish numeric salience among a set of objects. Design and implement a class called Task that represents a task (such as on a to-do list) that implements the Salience interface. Create a driver class to exercise some Task objects.

  • Write the following in Java Design an enum called Rating, it has the following names: bad,...

    Write the following in Java Design an enum called Rating, it has the following names: bad, okay, good, better, best Design a class called Review (reviews might be for a restaurant, movie, etc), it has two attributes: a Rating type called rating, a String type called comment. Design getters and setters for the attributes. Override the toString() method to display both the rating and the comment.

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT