Question

1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...

1. Consider the following interface:
interface Duty
{
public String getDuty();
}
a. 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.
b. 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.
c. 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
}

Note:
 Put all 4 classes in the same file (Duty, Student, Professor, and DutyApp)
 You CANNOT modify the above main method.
Hand in:
 DutyApp.java.

Homework Answers

Answer #1

import java.util.Scanner;

interface Duty
{
    public String getDuty();
}

class Student implements Duty {
    String id;
    public Student(String id) {
        this.id = id;
    }
    String getId() { return id; }
    void setId(String id) { this.id=id; }
    
    public String getDuty() {
        return "study 40 hours a week";
    }
}

class Professor implements Duty {
    private String name;

    public Professor(String name){
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDuty() {
        return "Return homework on time";
    }
}


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(); // So that the new line gets cleared from input buffer
            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
    public static Student getStudent(Scanner sc) {
        System.out.println("Enter student id: ");
        String id = sc.nextLine();
        return new Student(id);
    }    
    
    //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
    public static Professor getProfessor(Scanner sc) {
        System.out.println("Enter Professor name: ");
        String name = sc.nextLine();
        return new Professor(name);
    }    
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Know the answer?
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
Similar Questions
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
1) Create an interface to the keyboard (Scanner). Scanner is described in Chapter 2. 2) Prompt...
1) Create an interface to the keyboard (Scanner). Scanner is described in Chapter 2. 2) Prompt the user to enter their first and last name.. 3) Read the name into a String. 4) Print a person greeting such as “Hello FirstName LastName”. Print the users name in the greeting. 5) Prompt the user to enter 2 integers. 6) Read the integers into 2 variables into your program. 7) Prompt the user to enter a math operation – one of +,-,*,/...
Use the class definition below to answer the following questions. public class TrafficLight { String stopLight...
Use the class definition below to answer the following questions. public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1. How many field attributes are there in the TrafficLight class? 2. Name a field attribute for this class. 3. What is the name of the method that is an accessor? 4. What is the name of the method that is...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions:...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions: 1. Fully parametrized Constructor (to initialize class variables) 2. Getter (getBalance()) and Setter (setBalance()) for the float balance, must be greater than zero. In Account.java Methods: checkBalance( ) to print CURRENT BALANCE. deposit(int added_amount) should add add_amount to balance withdraw(int withdraw_amount) should deduct withdraw_amount balance display( ) will print all the information (account number, name, balance). Class 2: AccountTest.java Create an object account1 and...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
Part 1 Given the following code: public class MyClass { private double score; private String studentID;...
Part 1 Given the following code: public class MyClass { private double score; private String studentID; //code of the class ..... } //end of MyClass Code a default constructor and an overloaded constructor of MyClass that will assign values to its two instance fields: Please write in JAVA Part 2 Continue from question about, code an mutator of instance field called studentID:
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...