In: Computer Science
Finish the following java question: Consider the following interface:
interface Duty
{ public String getDuty();
}
Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week.
Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty of a Professor is to return homework on time.
Complete the following program:
public class DutyApp
{
//Desc: A program which allows the user to create either a Student or a Professor.
// The program displays a menu asking for 1. Student, 2. Professor, 3. Quit. The
// user enters 1 or 2, followed by the id or name of the person. The program prints the duty
// of the person, and then asks the user to enter 1, 2, or 3 again. The program will continue
// until the user selects 3 to quit.
//Input: The user enters 1 or 2 followed by the id or name of the person via the keyboard.
//Output:For each person entered by the user, the duty of the person displayed on the screen
public static void main(String[] args)
{
Scanner f=new Scanner(System.in);
int response =0;
Duty person;
while (true)
{
System.out.print("1. Student, 2. Professor, 3. Quit: ");
response =f.nextInt();
f.nextLine(); //WHY????
if (response==3) break;
if (response==1) person =getStudent(f);
else person =getProfessor(f);
System.out.println(person.getDuty());
}
}
//Input: The user enters the id of the Student via the keyboard.
//Post: One line read from f
//Return: The Student with the input id.
// ** you need to write this method getStudent
//Input: The user enters the name of the Professor via the keyboard.
//Post: One line read from f
//Return: The Professor with the input name.
// ** you need to write this methof getProfessor
}
Here i write code according to your requirement, if you any issue or doubt in this code feel free to ask me or post the issue, i will update the code according to your need.
Note: here, you dont ask for print Student id or professor name, if you want post it.
Interface Duty
public interface Duty {
        
        public String getDuty();
}
Professor.java
public class Professor implements Duty {
        String name;
        
        public Professor(String name) {
                this.name = name;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        @Override
        public String getDuty() {
                return "The duty of a Professor is to return homework on time.";
        }
}
Student.java
public class Student implements Duty {
        int id;
        
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public Student(int id) {
                this.id = id;
        }
        @Override
        public String getDuty() {
                return "The duty of a Student is to study 40 hours a week.";
        }
}
DutyApp.java
import java.util.Scanner;
public class DutyApp {
        public static Duty getStudent(int id) {
                return new Student(id);
        }
        
        private static Duty getProfessor(String name) {
                return new Professor(name);
        }
        public static void main(String[] args) {
                Duty person = null;
                Scanner f = new Scanner(System.in);
                int response = 0;
                while (true) {
                        System.out.print("1. Student, 2. Professor, 3. Quit: \n");
                        response = f.nextInt();
                        if (response == 3)
                                break;
                        if (response == 1) {
                                System.out.println("Enter Student id : ");
                                int id = f.nextInt();
                                person = getStudent(id);
                                System.out.println(person.getDuty());
                        }
                        else if(response == 2) {
                            System.out.println("Enter Professor name : ");
                                String name = f.next();
                                person = getProfessor(name);
                                System.out.println(person.getDuty());
                        }
                        else
                                System.out.println("Wrong choice entered. Again try");
                        
                        System.out.println("");
                }
                f.close();
        }
}